0

I'm trying to extend the lifetime of WooCommerce shopping cart for users with accounts. The suggested solutions that I've found (and tried) involve extending the session expiration.

Woocommerce Set Cart Expiration Interval

However, this is not what I'm after. I would like the session expiration to stay the same, but the next time the user logs in I would like their cart to remain for up to six months.

Can someone point me in the right direction?

dubloons
  • 1,136
  • 2
  • 12
  • 25
  • Then you will have to store the cart on your database. I would expect that WooCommerce would have an option for that – RiggsFolly Jun 12 '19 at 15:33

1 Answers1

0

The session expiration and the cart expiration are linked: they both use the same internal variable (a protected property of class WC_Session_Handler, $_session_expiration).

You can’t change one and not the other.

Removing the cart from the database is done by WC_Session_Handler->cleanup_sessions(). This is invoked by the function wc_cleanup_session_data() which is triggered by the woocommerce_cleanup_sessions action .

You might try to remove the default action and provide your own.

remove_action( 'woocommerce_cleanup_sessions', 'wc_cleanup_session_data' );

function my_cleanup_sessions() 
{
    global $wpdb;

    $wpdb->query( $wpdb->prepare( "DELETE FROM $this->_table WHERE session_expiry < %d", time() + 180 * 24 * 60 * 60 ) );

    if ( class_exists( 'WC_Cache_Helper' ) ) {
        WC_Cache_Helper::incr_cache_prefix( WC_SESSION_CACHE_GROUP );
    }
}
add_action( 'woocommerce_cleanup_sessions', 'my_cleanup_sessions' );

I have not tested this. It might not work and there might be side effects.

Christian Lescuyer
  • 18,893
  • 5
  • 49
  • 45
  • It's amazing to me that WooCommerce doesn't have an option to maintain carts beyond the current session. All the big vendors do this. Nobody on WC wants to? – dubloons Jun 13 '19 at 23:12