Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce cart session not expiring for logged in users

I have a woocommerce website. I need to exipre and remove cart items after a while. But the session not exipire for logged-in users.

I used the below code to achive this but its not working for logged in users:

add_filter('wc_session_expiring', 'filter_ExtendSessionExpiring');
add_filter('wc_session_expiration' , 'filter_ExtendSessionExpired');

function filter_ExtendSessionExpiring($seconds) {
    return 60*60*24;
}
function filter_ExtendSessionExpired($seconds) {
   return 60*60*25;
}

The above code works fine for non logged-in users, but I need it work for logged-in users too. Please advise

like image 874
Reza Avatar asked Jan 28 '26 16:01

Reza


1 Answers

This is because WooCommerce handles cart expiration differently for guests and logged-in users. Out of the box, the persistent cart feature is enabled for all logged-in customers and allows them to keep the same cart contents even if they use different devices.

For this purpose, the plugin adds the _woocommerce_persistent_cart_BLOG_ID parameter containing all cart items to the user metadata (wp_usermeta table). You can disable this functionality by adding the following code:

add_filter('woocommerce_persistent_cart_enabled', '__return_false');

After that, your code would work well for all new sessions and force the cart to be emptied after expiration and running the woocommerce_cleanup_sessions cron job (by default, twice a day).

like image 108
Vlad Avatar answered Jan 30 '26 04:01

Vlad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!