I think I need help in here, Im not really sure if I need to set the session manually or it will set automatically while adding items to woocommerce cart, lets consider the following:
if(is_array($args_n)){
foreach($args_n as $item){
$is_product_in_cart = false;
if(sizeof( WC()->instance()->cart->get_cart() > 0)){
foreach(WC()->instance()->cart->get_cart() as $cart_items_key => $cart_items_values){
$products = $cart_items_values['data'];
if($products->id == $item){
$is_product_in_cart = true;
}
}
if(!$is_product_in_cart){
WC()->cart->add_to_cart( $item );
}
}else{
WC()->cart->add_to_cart( $item );
}
}
}
So, when I do the following:
var_dump(sizeof(WC()->instance()->cart->get_cart()));
At the beginning of the call I get 0
after the code has been executed I get the right value == 3
(for this example);
The arguments being passed is just a simple array with products id’s fx:
$args_n = array(
2080,
720,
835,
);
This array is created with a parse url and implode, nothing from the other world
The issue is, even tho all looks good since I get the right number from the cart size when I do go to the cart page, it still empty!. Any ideas or hints?
Cheers
EDIT
Debug info:
Adding a simple eco to the the id being pass:
echo "---> {$item} n";
Before being redirect, I run this:
$cart_items = $woocommerce->cart->get_cart();
foreach($cart_items as $i => $x){
$p = $x['data']->post;
echo " in cart ---> {$p->ID} n";
}
The result is:
int(0) ---> 2080 ---> 720 ---> 835 int(3) in cart ---> 2080 in cart ---> 720 in cart ---> 835
So all items are in the cart! but still no there when the redirect is done, or just navigation away from that landing page.
EDIT:
When checking woocommerce WC()->session
I get the result saying the session is alive with the items added to the cart..
object(WC_Session_Handler)#173 (7) {
["_cookie":"WC_Session_Handler":private]=>
string(55) "wp_woocommerce_session_77f1e25389fdbfe3480cc6593d3838b0"
["_session_expiring":"WC_Session_Handler":private]=>
int(1440319051)
["_session_expiration":"WC_Session_Handler":private]=>
int(1440322651)
["_has_cookie":"WC_Session_Handler":private]=>
bool(true)
["_customer_id":protected]=>
string(32) "57cd99b8855e872adad62440a6c2fbe0"
["_data":protected]=>
array(20) {
["cart"]=>
string(904) "a:3:{s:32:"54ff9e9e3a2ec0300d4ce11261f5169f";a:9:{s:10:"product_id";i:2080;s:12:"variation_id";s:0:"";s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:1;s:8:"line_tax";i:0;s:13:"line_subtotal";i:1;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:"5f2c22cb4a5380af7ca75622a6426917";a:9:{s:10:"product_id";i:720;s:12:"variation_id";s:0:"";s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:1;s:8:"line_tax";i:0;s:13:"line_subtotal";i:1;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:"4d5b995358e7798bc7e9d9db83c612a5";a:9:{s:10:"product_id";i:835;s:12:"variation_id";s:0:"";s:9:"variation";a:0:{}s:8:"quantity";i:1;s:10:"line_total";d:1;s:8:"line_tax";i:0;s:13:"line_subtotal";i:1;s:17:"line_subtotal_tax";i:0;s:13:"line_tax_data";a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}}}"
["applied_coupons"]=>
string(6) "a:0:{}"
["coupon_discount_amounts"]=>
string(6) "a:0:{}"
["coupon_discount_tax_amounts"]=>
string(6) "a:0:{}"
["removed_cart_contents"]=>
string(6) "a:0:{}"
["cart_contents_total"]=>
float(3)
["cart_contents_weight"]=>
int(0)
["cart_contents_count"]=>
int(3)
["total"]=>
int(0)
["subtotal"]=>
int(3)
["subtotal_ex_tax"]=>
int(3)
["tax_total"]=>
int(0)
["taxes"]=>
string(6) "a:0:{}"
["shipping_taxes"]=>
string(6) "a:0:{}"
["discount_cart"]=>
int(0)
["discount_cart_tax"]=>
int(0)
["shipping_total"]=>
int(0)
["shipping_tax_total"]=>
int(0)
["fee_total"]=>
int(0)
["fees"]=>
string(6) "a:0:{}"
}
["_dirty":protected]=>
bool(true)
}
EDIT
If there is item at the cart already, and the function is triggered it will work as it is suppose to, so I guess is a session issue
I spun up a new WooCommerce and tried out your code – I was not able to replicate the issue. I think it’s possible that this is a load-order issue. WooCommerce creates a session and a cart for every visitor, so I don’t think it’s a matter of the cart not being “primed” with an existing session or anything like that. Often I find that you need to find the right hook, especially when dealing with the WooCommerce cart. WooCommerce actually keeps cart information in three spots: The cart object instance, the user’s session, and a persistent cart in the database. Getting any updates to work in harmony with the dance that WooCommerce is doing between those can be tricky. I have found that the most reliable time to add items to a cart before a full page load is in the ‘template_redirect’ hook. It’s after all session data and queries have run, but before the page is constructed. It’s good for manipulating things just before you want to display them to the user.
Here’s how I implemented your code, and it works on empty carts:
Try that and let us know if it works more reliably!
According to an article by Mike Jolley here:
” Problems with caching
Page cache added by plugins, and by hosts such as WPEngine, can break sessions. Well, not break, just show a page which is cached and as a result doesnât reflect the current users cart.
When a cart session is present some data needs to be un-cached, as do some pages such as cart, checkout and account.” -Mike Jolley
I think this is a strong possibility of what is going wrong, as the normal ways of starting sessions have been waived for WP_SESSION in the WC platform. Try calling your hosting provider to see if varnish caching is enabled on your server and see if they can provide a way around it.