The Problem
WooCommerce stores cart data in PHP sessions. When sessions expire prematurely due to server configuration, customers lose their cart contents, leading to abandoned checkouts and lost revenue.
Symptoms
- Cart empties randomly during browsing
- "Cart is empty" error after adding items
- Customers lose cart during checkout process
- Session expires after a few minutes instead of hours
- Works on desktop but not mobile
Real Error Message
Notice: woocommerce_cart_session_expired has been triggered.
The cart session has expired and cart contents have been cleared.Common Causes
Cause 1: PHP Session GC (Garbage Collection)
# php.ini default
session.gc_maxlifetime = 1440 # 24 minutes!Cause 2: Server-Level Session Cleanup
Some hosting providers run automated session cleanup scripts that delete sessions older than a certain threshold.
How to Fix It
Fix 1: Increase PHP Session Lifetime
# php.ini
session.gc_maxlifetime = 86400 # 24 hours
session.cookie_lifetime = 86400// wp-config.php
@ini_set('session.gc_maxlifetime', 86400);
@ini_set('session.cookie_lifetime', 86400);Fix 2: Configure WooCommerce Session Settings
```php // functions.php add_filter('wc_session_expiring', function($seconds) { return 86400 - 3600; // Expire warning at 23 hours });
add_filter('wc_session_expiration', function($seconds) { return 86400; // Session expires after 24 hours }); ```
Fix 3: Use Database Sessions Instead of Files
```php // functions.php - Store sessions in database function woocommerce_database_sessions() { // WooCommerce already uses transients for cart data // Ensure transients are not being cleaned too aggressively }
// If using Redis for sessions: define('WP_REDIS_SESSIONS', true); ```
Fix 4: Check Transient Cleanup
// Ensure WooCommerce transients are not deleted
// Check if a cleanup plugin is removing transients
add_filter('woocommerce_persistent_cart_enabled', '__return_true');Fix 5: Enable Persistent Cart for Logged-In Users
```php // functions.php add_filter('woocommerce_persistent_cart_enabled', '__return_true');
// This stores the cart in user meta, surviving session expiration ```
Fix 6: Debug Session Status
// Add to functions.php for debugging
add_action('wp_footer', function() {
if (is_user_logged_in() || is_admin()) {
echo '<!-- Session ID: ' . session_id() . ' -->';
echo '<!-- Session lifetime: ' . ini_get('session.gc_maxlifetime') . 's -->';
echo '<!-- Cart session key: ' . WC()->session->get_customer_id() . ' -->';
}
});Fix 7: Fix Nginx Session Cookie
# Ensure PHP-FPM passes session cookies correctly
fastcgi_param PHP_VALUE "session.gc_maxlifetime=86400";
fastcgi_param PHP_VALUE "session.cookie_lifetime=86400";