Reza
Reza

Reputation: 85

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

Upvotes: 3

Views: 484

Answers (1)

Vlad
Vlad

Reputation: 55

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).

Upvotes: 1

Related Questions