Change the default cart session length

Note: We are unable to provide support for customizations under our Support Policy. If you need to customize a snippet or extend its functionality, we recommend working with a Woo Agency Partner or finding a WooCommerce developer on Codeable.

When a visitor first adds a product to their cart a session is started that, by default, lasts for 48 hours. When it expires, the items added to the cart are forgotten, meaning the next time that visitor comes by, they’ll find their cart to be empty.

The cart session is not related to the Inventory’s Hold Stock setting. The Hold Stock setting applies to orders of the Pending Payment status where inventory is removed, but payment is not yet confirmed. This snippet does not impact that setting nor vice versa.

Only new visitors are instantly affected by this snippet, as recent visitors could still see the contents in their cart until their existing session expires. It’s possible to hurry along this process with a tool under WooCommerce > Status > Tools; click the Clear button next to the Clear customer sessions tool to expire everyone’s carts immediately.

The wc_session_expiring precedes the actual expiration set by session_expiration an hour and is used to indicate when a visitor’s session is about to expire. It should be changed relative to the session_expiration setting.

Code Snippet

↑ Back to top

Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added. Avoid adding custom code directly to your parent theme’s functions.php file as this will be overwritten entirely everytime you update the theme.

The value for this setting needs to be returned in seconds, which is why 47 hours is expressed as 60 * 60 * 47 (seconds x minutes x hours). To test this, for example, you might want to temporarily set it to 5 minutes, in which case 60 * 5 can be used instead. It’s best to not use such a short window permanently though as it would hamper the shopping experience and performance of your site.

<?php 

add_filter( 'wc_session_expiration', 'set_my_cookie'); 

function set_my_cookie($cookie_sesh) {

  // Default value is 48
  return 60 * 60 * 48; 

}
View on Github

Caching

↑ Back to top

To make sure the setting behaves as intended, refer to our Configuring caching plugins guide. If you encounter issues with lingering carts, clear plugin caches, and caching that might happen at the server level (you may need to reach out to your host for help with the latter).

Extensions

↑ Back to top

If you prefer an extension to do the heavy lifting for you, have a look at these results from the WooCommerce Marketplace.

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.