Set WooCommerce cart expiration

kuzditomi picture kuzditomi · Jul 23, 2013 · Viewed 12.5k times · Source

I would like to erase the cart content when the woocommerce session expires. I can see there's a variable setting the time in class WC_Session_Handler, however when it expires, products does not get removed from cart (i guess it behaves like this by design,it's not an error).

So please tell me how can i set the session expiration time for woocommerce cart so, that cart content gets removed when it expires?

Answer

Lance Cleveland picture Lance Cleveland · Dec 11, 2013

From what I can see, WooCommerce 2.0.20 has a scheduled maintenance job that runs twice/day that will remove any cart sessions from the WordPress options table. The default expiration time is set to 48 hours from the time the user first created the cart. I'm guessing your standard WordPress scheduling routines (and server cron/at jobs) will need to be running properly for this to execute.

AFAIK there is no way to adjust the 48 hour rule via settings. You could write a filter in your theme or in an "adjacent" plugin.

Here are some code fragments from a new "WooCommerce Extend Cart Timeout" plugin I built on my site:

Inside my WoocommerceLicenseAPI class:

if ( ! class_exists( 'WoocommerceLicenseAPI' ) ) {
add_filter('wc_session_expiring'   , array('WoocommerceLicenseAPI',       'filter_ExtendSessionExpiring') );

add_filter('wc_session_expiration' , array('WoocommerceLicenseAPI', 'filter_ExtendSessionExpired') );
{

static function filter_ExtendSessionExpiring($seconds) {
    return (60 * 60 * 24 * 8) - (60 * 60);
}
static function filter_ExtendSessionExpired($seconds) {
    return 60 * 60 * 24 * 8;
}

HTH