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.
Use the WC()->cart method to get woocommerce cart item data. Cart page use this method to load all the values on the cart page e.g. woocommerce get cart item totals, subtotal, get cart items to count, get items price, etc.
The WooCommerce Cart Hash cookie contains information about the shopping cart. In other words, it memorizes what you added to your cart. Also, it helps WooCommerce know when the cart data changes.
WooCommerce uses a combination of both WordPress database tables and its own custom tables to store its data. However, WooCommerce doesn't have a dedicated table to store the customer data. The customer data is stored in different database tables, which sometimes might make retrieval of this data challenging.
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 to1
when something is in cart)wp_woocommerce_session_3413758cad2ada414127ffee7347e40f
=>ac5f880c99503169574ad996f35f85c5%7C%7C1469492696%7C%7C1469489096%7C%7C17e565032403642121f5ad12cfa72c41
(completely related towp_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 DBsession_key
andsession_value
of tablewp_woocommerce_sessions
).1469492696
(the DBsession_expiry
of tablewp_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 changeswoocommerce_items_in_cart
cookie value don't changes as an item remains in cart.wp_woocommerce_session_3413758cad2ada414127ffee7347e40f
cookie value don't changes.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 asession_value
serialized multidimensional array or object).With Class WC_Cart you will be able to get this data.
You should also be able to useWC()->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:
Just to follow up on this in case anyone is interested. The selected answer is extremely helpful in breaking things down. The OP was asking how to get the products from the session cookies so I thought I would follow up.
Here is how I was able to extract the cart data from the wc_woocommerce_session_
cookie:
$session_id = null;
$values = null;
foreach( $_COOKIE as $key => $value ) {
if( stripos( $key, 'wp_woocommerce_session_' ) === false ) {
continue;
}
$values = explode( '||', $value );
}
$session_id = $values[0];
$session = new WC_Session_Handler();
$session_data = $session->get_session( $session_id );
// Contains array of items in cart including product ids, quantities, totals, etc.
$cart_data = unserialize( $session_data['cart'] );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With