WooCommerce cookies和会话 - 获取当前产品的购物车 [英] WooCommerce cookies and sessions - Get the current products in cart

查看:289
本文介绍了WooCommerce cookies和会话 - 获取当前产品的购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试学习WooCommerce的cookies,用于执行添加到购物车中的产品操作的任何功能。我在控制台中写道 document.cookie
我得到这些数据:

I try to learn the cookies of WooCommerce, for do any function operating by products the added to cart. I wrote in console document.cookie And I get this data:

woocommerce_items_in_cart=1;
woocommerce_cart_hash=500d17f6c010b62b25c3f52893be945d;

我知道cookie woocommerce_cart_hash 包含购物车中的产品,但我不明白如何获得产品的名称。这是什么 500d17f6c010b62b25c3f52893be945d

I understand that the cookie "woocommerce_cart_hash" contains the products in cart, but I don't understand how to get the name of the product. What is it 500d17f6c010b62b25c3f52893be945d

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

正如您将在下面看到的, woocommerce_cart_hash 不包含购物车中的产品,因为与 WooCommerce会话数据库表相关的第三个更复杂的coockie

As you will see below, woocommerce_cart_hash doesn't contains the products in cart, because there is a third more complex coockie related to WooCommerce sessions DB table.

数据不会存储在Cookie中,而是存储在DB WooCommerce会话中的相应参考中(详见下文) ...

The data is not stored in the cookie herself but in a corresponding reference located in DB WooCommerce session (see below for details)...

您还需要注意,WordPress会启用一些本地数据存储。

You have also to note that WordPress enable some local data storage.


以下数据来自一个真实的案例,非登录用户将2个产品添加到购物车。

1)当非登录用户向购物车添加内容时,Woocommerce设置3个coockies:

1) Woocommerce set 3 coockies when a non logged user add something to cart:



  • woocommerce_cart_hash => 44ffcb32800e5b20cfbb080753d48408 (安全/完整性哈希)

  • woocommerce_items_in_cart => 1 (设为 1 什么东西在购物车中)

  • wp_woocommerce_session_3413758cad2ada414127ffee7347e40f => ac5f880c99503169574ad996f35f85c5%7C%7C1469492696%7C%7C1469489096 %7C%7C17e565032403642121f5ad12cfa72c41 (与 wp_woocommerce_sessions 完全相关的DB表创建的会话与此cookie同时进行)

  • woocommerce_cart_hash => 44ffcb32800e5b20cfbb080753d48408 (security/integrity hash)
  • woocommerce_items_in_cart => 1 (set to 1 when something is in cart)
  • wp_woocommerce_session_3413758cad2ada414127ffee7347e40f => ac5f880c99503169574ad996f35f85c5%7C%7C1469492696%7C%7C1469489096%7C%7C17e565032403642121f5ad12cfa72c41 (completely related to wp_woocommerce_sessions DB table created session at the same time than this cookie)

2)在数据库表中 wp_woocommerce_sessions 会话同时生成:

2) And in DB table wp_woocommerce_sessions a session is generated at the same time:


session_id => 6

session_key => ac5f880c99503169574ad996f35f85c5

session_value => a:18:{s:4:cart; s:600:a:2:{s:32:d82c8d1619ad8176d665453cfb2e55f0; a:9:{s:10 : PRODUCT_ID; I:53; S:12: variation_id; I:0; S:9: 变异;一个:0:{} S:8: 量; I:1; S:10 : line_total; d:35; S:8: line_tax; I:0; S:13: line_subtotal; I:35; S:17: line_subtotal_tax; I:0; S:13: line_tax_data ;一个:2:{S:5: 总 ;一个:0:{} S:8: 小计 ;一个:0:{}}} S:32: a5bfc9e07964f8dddeb95fc584c d965d ;一个:9:{S:10: PRODUCT_ID ; I:37; S:12: variation_id ; I:0; S:9: 变异;一个:0:{} S:8: 量; I:1; S:10: line_total; d:18; S:8: line_tax; I:0; S:13: line_subtotal; I:18; S:17:line_subtotal_tax 我:0; S:13: line_tax_data ;一个:2:{S:5: 总 ;一个:0:{} S:8: 小计;一个:0:{}}}} ; S:15: applied_coupons ; S:6: A:0:{} ; S:23: coupon_discount_amounts ; S:6: A:0:{} ; S:27: coupon_discount_tax_amounts ; S:6: A:0:{} ; S:21: removed_cart_contents ; S:6: A:0:{} ; S:19: cart_contents_total; d:53; S: 5: 总; d:53; S:8: 小计; I:53; S:15: subtotal_ex_tax; I:53; S:9: TAX_TOTAL; I:0; S:5: 税; S:6: A:0:{}; S:14: shipping_taxes; S:6: A:0:{}; S:13: discount_cart; I:0; S:17: discount_cart_tax; I:0; S:14: shipping_total; N; S:18: shipping_tax_total; I:0; S:9: fee_total; I:0; S:4: 费用; s:6:a:0:{};}

session_expiry => 1469492696

session_id => 6
session_key => ac5f880c99503169574ad996f35f85c5
session_value => a:18:{s:4:"cart";s:600:"a:2:{s:32:"d82c8d1619ad8176d665453cfb2e55f0";a:9:{s:10:"product_id";i:53;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:35;s:8:"line_tax";i:0;s:13:"line_subtotal";i:35;s:17:"line_subtotal_tax";i:0;s:13:"line_tax_data";a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}}s:32:"a5bfc9e07964f8dddeb95fc584cd965d";a:9:{s:10:"product_id";i:37;s:12:"variation_id";i:0;s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:18;s:8:"line_tax";i:0;s:13:"line_subtotal";i:18;s:17:"line_subtotal_tax";i:0;s:13:"line_tax_data";a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}}}";s:15:"applied_coupons";s:6:"a:0:{}";s:23:"coupon_discount_amounts";s:6:"a:0:{}";s:27:"coupon_discount_tax_amounts";s:6:"a:0:{}";s:21:"removed_cart_contents";s:6:"a:0:{}";s:19:"cart_contents_total";d:53;s:5:"total";d:53;s:8:"subtotal";i:53;s:15:"subtotal_ex_tax";i:53;s:9:"tax_total";i:0;s:5:"taxes";s:6:"a:0:{}";s:14:"shipping_taxes";s:6:"a:0:{}";s:13:"discount_cart";i:0;s:17:"discount_cart_tax";i:0;s:14:"shipping_total";N;s:18:"shipping_tax_total";i:0;s:9:"fee_total";i:0;s:4:"fees";s:6:"a:0:{}";}
session_expiry => 1469492696

我无法解释所有核心流程(为此你将需要查看php核心文件)。

但是如果你想看 wp_woocommerce_session_ ... cookie值,你会看到你可以用%7C%7C substring,所以我们得到这个cookie值的4个子串:

I can't explain all core processes (for that you will need to look in php core files).
But if you look to wp_woocommerce_session_… cookie value you will see that you can cut it with %7C%7C substring, so we get here 4 substrings for this cookie value:



  • ac5f880c99503169574ad996f35f85c5 (数据库中的相同参考 session_key wp_woocommerce_sessions )的 session_value

  • 1469492696 (表 <$ c>的数据库 session_expiry $ c> wp_woocommerce_sessions )。

  • 1469489096 (同上)

  • 17e565032403642121f5ad12cfa72c41 (??? - 我真的不知道这是为了什么...)

  • ac5f880c99503169574ad996f35f85c5 (same reference that in the DB session_key and session_value of table wp_woocommerce_sessions).
  • 1469492696 (the DB session_expiry of table wp_woocommerce_sessions).
  • 1469489096 (same thing above).
  • 17e565032403642121f5ad12cfa72c41 (??? - I don't really know what is this for…)






如果用户回来并删除他的一项购物车:


If the user come back and delete one item in his cart:


  • woocommerce_cart_hash cookie 价值变动

  • woocommerce_items_in_cart Cookie 值不会随着商品保留在购物车中而发生变化。

  • wp_woocommerce_session_3413758cad2ada414127ffee7347e40f Cookie 值不会发生变化。

  • 数据库 wp_woocommerce_sessions 会话ID <$ c 6 已被生成的会话ID 删除/替换购物车更改(仅限一种产品)

  • woocommerce_cart_hash cookie value changes
  • woocommerce_items_in_cart cookie value don't changes as an item remains in cart.
  • wp_woocommerce_session_3413758cad2ada414127ffee7347e40f cookie value don't changes.
  • DB wp_woocommerce_sessions TABLE, the session ID 6 has been deleted/replaced by a new generated session ID is that reflects the cart changes (only one product).

因此,此时您可以看到Cookie和会话数据库表 wp_woocommerce_sessions 之间的关系,其中包含与未登录用户相关的所有购物车数据

(在 session_value 序列化多维数组或对象中)

So at this point you can see the relation between cookies and session DB table wp_woocommerce_sessions that contains all cart data related to non logged users
(in a session_value serialized multidimensional array or object).

使用 类WC_Cart 您将能够获得此数据。
您还应该能够在PHP代码中使用 WC() - > cart 语法...

使用 Class WC_Session_Handler ,您将能够自己操纵会话。

With Class WC_Session_Handler you will be able to manipulate sessions herself.

要完成,请 类WC_Ajax 用于购物车更改。

And to finish, Class WC_Ajax is used for cart changes.






参考文献:


References:

  • Class WC_Cart
  • Class WC_Session_Handler
  • Class WC_Ajax

这篇关于WooCommerce cookies和会话 - 获取当前产品的购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆