1

So , how do i expire & empty the cart in woocommerce , since the filters are not doing that at all , only increasing the session on some given minutes , which , honestly , i don't know where the hell is used or if is used in any way to clear the cart ?

There is this:

    add_filter( 'wc_session_expiring', 'wc_custom_session_expiring' );
    add_filter( 'wc_session_expiration', 'wc_custom_session_expiring' );

    function wc_custom_session_expiring( $expiry ) {
        return 300; // 5 mins in seconds
    }
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Alin Razvan
  • 1,451
  • 13
  • 18

1 Answers1

2

Below , you have my code i wrote for a client , just to save you guys some time , the code posted here does update on the expire time if the "delay" given time didn't passed , and the user is still active / buying things on the shop. If the user stops clicking page for more than 5 minutes (you can change in the code) , the next click he will have on the shop , or the next loaded page , the cart will be emptied.

I am using ReflectionClass to quickly read some protected values out of the session class , just right after updating the expire values with the help of the filters.

Please note this plugin is using $_SESSION , feel free to modd it and post your own version here ! Please do also note this : $minutes = get_option( 'expire_option', '5' ); , i used an admin form field (option) setup for this , feel free to add your own .

<?php 
/**
* Plugin Name: Woocommerce Expire Cart
* Plugin URI: http://webake.ro
* Description: Expire & clear cart after 5 minutes (default) , or use expire_option field if set !!
* Version: 2.0
* Author: Alin Razvan
* Author URI: https://webake.ro
*/

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

session_start();

# https://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php
function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

/* 
SET Cart expire after 5 minutes , 
*/ 
add_filter( 'wc_session_expiring', 'filter_wc_session_expiring', 998, 1 );
add_filter( 'wc_session_expiration', 'filter_wc_session_expiration', 999, 1 );

function filter_wc_session_expiring() {
// get expire_option minutes time ! 
$minutes = get_option( 'expire_option', '5' );
if(!$minutes){$expiring_time = "300"; /* 5 mins in seconds */ }
else { $expiring_time = ($minutes * 60); }

return $expiring_time; 
}

function filter_wc_session_expiration() {
// get expire_option minutes time !  
$minutes = get_option( 'expire_option', '5' );
if(!$minutes){$expiring_time = "300"; /* 5 mins in seconds */ }
else { $expiring_time = ($minutes * 60); }

return $expiring_time; 
}

/* 
Since the cart is not going to empty , 
because this is most likely a woocommerce bugg , we do this to fix it ! 
*/

add_action('wp_loaded','custom_expire_cart_loader'); 

function custom_expire_cart_loader(){

global $woocommerce; 

// if is not admin panel , execute only on frontend !
if(isset($woocommerce)&&!is_admin()){
    
    $current_user_id = get_current_user_id();
    
    $gtsession = $woocommerce->session;
    $info = $gtsession->get_session($current_user_id, $default = false );
    //-------------------------------------
    # dirty ,i know , but no other way for quick solution
    $session_expiring = accessProtected($gtsession, '_session_expiring');
    $session_expiration = accessProtected($gtsession, '_session_expiration');
    //-------------------------------------
    
    if(!isset($_SESSION['lucky_expire_cart'])){
     $_SESSION['lucky_expire_cart'] = $session_expiring; 
     $lucky_cart_session = $session_expiring;
    }else {
     $lucky_cart_session = $_SESSION['lucky_expire_cart'];
    }
    
    #echo "Now:".date('Y-m-d H:i:s')."\n<br/>";
    //echo "Session expiration:".date('Y-m-d H:i:s',$session_expiration)."\n<br/>";
    #echo "Expiring:".date('Y-m-d H:i:s',$session_expiring)."\n<br/>";
    #echo "Expire saved compare:".date('Y-m-d H:i:s',$lucky_cart_session)."\n<br/>";
    
    # Expired due to delay in action (page click ,page refresh etc) 
    # ex: 5 mins passed , expire session 
    if($lucky_cart_session<=time()){
    
         # -------- This empties the cart ! -----------
         WC()->cart->empty_cart(true);
         # --------------------------------------------
         
        #echo "Is expired now !";
     
        unset($_SESSION['lucky_expire_cart']);
        $_SESSION['lucky_expire_cart'] = $session_expiring; 
        #echo "Expire new set to:".date('Y-m-d H:i:s',$session_expiring)."\n<br/>";
    }
    # Not yet expired by delay ,
    # we do update the expire time to new one
    else {
        unset($_SESSION['lucky_expire_cart']);
        $_SESSION['lucky_expire_cart'] = $session_expiring; 
        #echo "Not yet expired by delay ! \n<br/>";
        #echo "Expire saved updated to:".date('Y-m-d H:i:s',$session_expiring)."\n<br/>";
    }

}

}


?>
Alin Razvan
  • 1,451
  • 13
  • 18