For eCommerce store owners, understanding and managing cart sessions effectively is crucial. In WooCommerce, the default cart session, which retains the customer’s cart data in a cookie, expires after 48 hours of inactivity. However, there may be instances where you want to extend or reduce this expiry time to better suit the shopping experience, encourage quicker purchasing decisions, or manage server resources more efficiently.

This article will guide you on how to control the cart expiry time in WooCommerce using the wc_session_expiration and wc_session_expiring hooks.

What are ‘wc_session_expiration’ and ‘wc_session_expiring’ Hooks?

In WooCommerce, wc_session_expiration and wc_session_expiring are filters that allow you to control the cart session’s expiry time.

  • wc_session_expiration is the filter that controls the duration until a customer’s session data expires completely. By default, this is set to 48 hours.
  • wc_session_expiring is the filter that controls the duration until a customer’s session data is marked as “expiring”. This is set to trigger an hour before the wc_session_expiration time, effectively extending the session life by renewing the wc_session_expiration session.

The wc_session_expiring hook being set one hour before the wc_session_expiration allows the system to renew the session before it completely expires. This creates a seamless shopping experience for customers who might be slowly browsing your store.

How to Modify the Cart Session Expiry Time

You can add the following code to your website child theme’s functions.php file or in a custom plugin to modify the cart session expiry time:

add_filter('wc_session_expiring', 'custom_wc_session_expiring');

function custom_wc_session_expiring($seconds) {
    return 60 * 60 * 24 * 3; // 3 days until the session is marked as "expiring"

add_filter('wc_session_expiration', 'custom_wc_session_expiration');

function custom_wc_session_expiration($seconds) {
    return 60 * 60 * 24 * 4; // 4 days until the session expires

In the above code, wc_session_expiring is set to 3 days (72 hours), and wc_session_expiration is set to 4 days (96 hours). You can modify these values as per your requirements.

How to Use the Code snippet

You can implement this snippet by following on of these steps:

  1. Child Theme: If you’re using a custom child theme, you can add the snippet to the functions.php file of your child theme. This ensures that the functionality remains intact even when you update your theme.
  2. Custom Plugin: A better way, you can create a custom plugin to add the snippet. Creating a custom plugin allows you to keep your code separate from the theme files, making it easier to manage and maintain. You can use our online tool WP Plugin Creator to generate a custom plugin with the snippet included. Simply paste the snippet into the plugin code editor, specify the plugin name, and download the generated plugin zip file.

Final Thoughts

Understanding how to control cart expiry time in WooCommerce can play a crucial role in shaping your customers’ shopping experience. By adjusting the wc_session_expiration and wc_session_expiring hooks, you can tailor the cart sessions to meet your business needs and customer expectations.

Remember, while extending the cart expiry time can potentially increase sales by giving customers more time to finalize their purchases, it can also consume more server resources. Hence, it’s important to find a balance that works best for your WooCommerce store.

We hope this guide has helped you understand how to control the cart expiry time in WooCommerce. If you have any questions, feel free to reach out to us in the comments section below. Happy selling!