One of the most frustrating issues for online store customers is losing cart items during prolonged checkout sessions. Whether due to distractions, network issues, or reviewing product details, a customer might spend more than the default session time allotted by WooCommerce. When that happens, the cart might appear empty when they finally return. This not only hampers user experience but can also mean a lost sale.
TL;DR – Summary
WooCommerce uses WordPress’s native session handling to control how long a cart persists. The default cart session timeout might be too short for stores with complex products or long decision phases. You can address this by extending both the PHP session length and adjusting WooCommerce’s session settings. Additionally, custom code or plugins can ensure that carts are maintained for much longer periods, reducing cart abandonment risks.
Understanding WooCommerce Session Management
WooCommerce relies on a combination of cookies and server-side session handling to store cart data. When a user adds an item to their cart, WooCommerce assigns them a session and stores this data typically in a database. However, the default cart session expiration is 48 hours — and in some hosting environments, it may be affected by server limits or PHP configurations like session.gc_maxlifetime.
Key components that determine cart session expiration include:
- WooCommerce internal session settings – controlled by WooCommerce’s session class.
- PHP session lifetime – configured in the server’s php.ini file or via .htaccess or user.ini overrides.
- Browser cookies – which define how long the user’s browser will keep session identifiers.
Why Session Lifespan Matters
Consider customers who:
- Leave the checkout process open on one tab while comparison shopping.
- Pause to consult with others before purchasing high-ticket items.
- Get distracted, only to return hours later intending to complete the purchase.
If the session expires, the cart is cleared. The impact? Reduced conversions, frustrated customers, and increased abandonment rates.
How to Extend WooCommerce Cart Session Lifespan
1. Increase PHP Session Lifetime
First, check your server’s PHP configuration. Typically, the session timeout is governed by the following directives:
-
session.gc_maxlifetime: This controls how long server-side session data is allowed to persist. -
session.cookie_lifetime: Determines how long the session cookie stays in the user’s browser.
Recommended values:
-
session.gc_maxlifetime = 28800(8 hours) session.cookie_lifetime = 28800
Edit your php.ini (or use .htaccess / .user.ini if your hosting limits access) to set these values. After making changes, restart your web server or php-fpm service as needed.
Note: Be mindful of your hosting provider’s limitations—some shared hosts override or ignore custom session settings.
2. Modify WooCommerce Session Expiration
WooCommerce internally uses session expiration control. You can hook into the session expiration using a filter to increase the cart duration:
function extend_woocommerce_cart_session_expiry($expires) {
return 60 * 60 * 8; // 8 hours
}
add_filter('woocommerce_session_expiration', 'extend_woocommerce_cart_session_expiry');
This code should be placed in your theme’s functions.php file or a custom plugin. It tells WooCommerce to retain the session for 8 hours.
Additional filters:
-
woocommerce_session_expiring– Fires just before session expiry (default is 47 hours). -
woocommerce_session_expiration– Sets when a session truly expires (default is 48 hours).
3. Use Plugins to Manage Cart Persistence
For store owners uncomfortable editing code, certain plugins can help prolong cart or session lifespan every time a user interacts with the store:
These tools provide user interfaces to manage cart lifetime, auto-saving carts to user profiles, sending recovery emails, or tweaking session settings without touching code.
4. Periodically Refresh Sessions with Frontend Scripts
If your visitors remain inactive for a while, but the checkout page is still open, their cart session may still expire. One workaround is to refresh or ping the server in the background using JavaScript. This technique helps keep the session alive.
Here’s a basic example using AJAX and WordPress’s admin-ajax:
<script type="text/javascript">
setInterval(function() {
fetch('/wp-admin/admin-ajax.php?action=keep_session_alive');
}, 600000); // every 10 minutes
</script>
Combine this with a simple AJAX handler in your theme:
add_action('wp_ajax_keep_session_alive', 'keep_session_alive');
add_action('wp_ajax_nopriv_keep_session_alive', 'keep_session_alive');
function keep_session_alive() {
// Just touch the session
WC()->session->set('ping', time());
wp_die();
}
This strategy ensures background session updates whenever a customer keeps the tab open, even passively.
5. Enable Persistent Cart for Logged-in Users
If your store requires or encourages user registration, WooCommerce automatically saves each user’s cart to their profile. The cart contents will persist across devices and sessions as long as the user is logged in.
To enhance this feature:
- Ensure customers are prompted to log in before or during checkout.
- Test cart persistence between devices and sessions.
- Install cart recovery plugins if needed for more granularity.
Testing Your Changes
Once you’ve applied any of these changes, simulate a long checkout session:
- Add products to your cart.
- Wait without refreshing or interacting for several hours.
- Return to see whether the cart contents remain intact.
Also test from incognito/private windows, and logged-out states to ensure consistency. Invalid settings or caching may sometimes appear to “save” the cart when it’s not active upon a backend check.
Common Pitfalls to Watch
- Host-level session cleanup: Some shared servers truncate sessions regardless of the time you define.
- Cache conflicts: Aggressive page caching can conflict with dynamic session data. Always exclude your cart, checkout, and “my account” pages from caching plugins.
- Browser restrictions: Incognito modes may delete cookies or prevent long client-side persistence.
Best Practices
- Encourage account creation during or before checkout for longer session control.
- Use a session keep-alive script for extended user sessions.
- Regularly test your cart flow on various devices and network conditions.
- Utilize analytics to determine average time to conversion and adjust your session limits accordingly.
Conclusion
Cart abandonment due to session expiration is a silent profit killer in WooCommerce. By extending session durations and using smart techniques like session keep-alive scripts or persistent cart plugins, you ensure a smoother shopping experience for your customers. These changes can lead to increased conversions, happier users, and fewer disgruntled emails complaining about “lost” carts. A longer session equals a higher chance of keeping that sale alive.