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;
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.
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.
The data is not stored in the cookie herself but in a corresponding reference located in DB WooCommerce session (see below for details)...
You have also to note that WordPress enable some local data storage.
The data below is taken from a real example case, where a non logged user add 2 products to cart.
1) Woocommerce set 3 cookies when a non logged user add something to cart:
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) 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:"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
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
(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 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).
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).
With Class WC_Cart you will be able to get this data.
You should also be able to use WC()->cart
syntax in your php code…
With Class WC_Session_Handler you will be able to manipulate sessions herself.
And to finish, Class WC_Ajax is used for cart changes.
References:
- Class WC_Cart
- Class WC_Session_Handler
- Class WC_Ajax