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

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

问题描述

我尝试学习 WooCommerce 的 cookie,以便执行添加到购物车的产品操作的任何功能.我在控制台中写了 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

任何帮助将不胜感激.

推荐答案

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

As you will see below, woocommerce_cart_hash doesn't contains the products in cart, because there is a third more complex cookie 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 个 cookie:

1) Woocommerce set 3 cookies 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 与此 cookie 同时创建的 DB 表会话)
  • 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) 并且在 DB 表 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:"variation";a: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";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:"数量";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:"税收";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

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 子字符串对其进行剪切,因此我们得到了 4此 cookie 值的子字符串:

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(与 DB session_keysession_value 中的引用相同表 wp_woocommerce_sessions).
  • 1469492696(表 wp_woocommerce_sessions 的 DB session_expiry)..莉>
  • 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 值不变.
  • DB wp_woocommerce_sessions TABLE会话 ID 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 语法...

使用 类 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:

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

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