Set WooCommerce cart expiration
From what I can see, WooCommerce 2.0.20 has a scheduled maintenance job that runs twice/day that will remove any cart sessions from the WordPress options table. The default expiration time is set to 48 hours from the time the user first created the cart. I'm guessing your standard WordPress scheduling routines (and server cron/at jobs) will need to be running properly for this to execute.
AFAIK there is no way to adjust the 48 hour rule via settings. You could write a filter in your theme or in an "adjacent" plugin.
Here are some code fragments from a new "WooCommerce Extend Cart Timeout" plugin I built on my site:
Inside my WoocommerceLicenseAPI class:
if ( ! class_exists( 'WoocommerceLicenseAPI' ) ) {
add_filter('wc_session_expiring' , array('WoocommerceLicenseAPI', 'filter_ExtendSessionExpiring') );
add_filter('wc_session_expiration' , array('WoocommerceLicenseAPI', 'filter_ExtendSessionExpired') );
{
static function filter_ExtendSessionExpiring($seconds) {
return (60 * 60 * 24 * 8) - (60 * 60);
}
static function filter_ExtendSessionExpired($seconds) {
return 60 * 60 * 24 * 8;
}
HTH
How to set cart expiration in woocommerce within 15 minutes?
If this code you linked is working just change the time calculation: 60 * 15.
Woocommerce cart session expiration
you can try this -
// Start session if not started
add_action('init', 'register_my_session');
function register_my_session(){
if( !session_id() ) {
session_start();
}
}
// Clear cart after 30 minutes
add_action( 'init', 'woocommerce_clear_cart' );
function woocommerce_clear_cart()
{
global $woocommerce;
$current_time = date( 'Y-m-d H:i:s');
$expiry_in_seconds = 1800; // 30 minutes
if (!isset($_SESSION['active_time']))
{
$_SESSION['active_time'] = date('Y-m-d H:i:s');
}
else{
// add 30 minutes
$cart_expiry_time = strtotime($_SESSION['active_time']) + $expiry_in_seconds;
// calculate seconds left
$diff = $cart_expiry_time - strtotime($current_time);
// round to minutes
$remaining_minutes = floor($diff/60);
// if time less than or equal to 1 minutes
if($remaining_minutes<=1)
{
//if (isset($_GET['clear-cart']) && $_GET['clear-cart'] == 1)
{
$woocommerce->cart->empty_cart();
//WC()->session->set('cart', array());
//}
}
}
}
Best Method: You should set up a cron job and run accordingly like every 10 minutes. if you want the same function for cronjob kindly uncomment if condition and increase the remaining time like this:
if($remaining_minutes<=15){
if (isset($_GET['clear-cart']) && $_GET['clear-cart'] == 1) {
$woocommerce->cart->empty_cart();
//WC()->session->set('cart', array());
}
}
and add URL on cronejon like this:
https://example.com?clear-cart=1 // your full site domain with param ?clear-cart=1
Woocommerce Set Cart Expiration Interval
The filter must return 72 hours, in seconds.
add_filter('wc_session_expiring', 'filter_ExtendSessionExpiring' );
add_filter('wc_session_expiration' , 'filter_ExtendSessionExpired' );
function filter_ExtendSessionExpiring($seconds) {
return 60 * 60 * 71;
}
function filter_ExtendSessionExpired($seconds) {
return 60 * 60 * 72;
}
Empty Cart Expiration Time For WooCommerce?
here is the plugin https://github.com/liquidweb/woo-cart-expiration
or even you can set in WooCommerce->settings->Products then click on inventory and in Hold stock (minutes) set minutes
Related Topics
Sharing PHP Session ($_Session) Across Multiple Domain
How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem
PHP Fopen() Error: Failed to Open Stream: Permission Denied
PHP - Override Existing Function
How to Select a Result from the Select2 Search Results
Selecting Rows Where a Field Is Null Using PHP Pdo Prepared Statements and MySQL
Php: How to Check If the Client Is Local
Namevaluepair Error "Namevaluepair Cannot Be Resolved to a Type"
Session Share Across Multiple Domains on Same Server
Key of Null Variable Equals Null Not Error
PHP - How to Know If Server Allows Shell_Exec