Automatically Empty WooCommerce Cart and Delete Session after 30 minutes

Viewed 2266

I am struggling to find a solution as to how I can automatically, after 30 minutes, empty the cart and delete the session.

add_filter('wc_session_expiring', 'filter_ExtendSessionExpiring' );
add_filter('wc_session_expiration' , 'filter_ExtendSessionExpired' );

function filter_ExtendSessionExpiring($seconds) {
    global $woocommerce;

    $woocommerce->session->set_customer_session_cookie(true);
        return 60 * 30;
        WC()->cart->empty_cart();
        WC()->session->destroy_session();
}

function filter_ExtendSessionExpired($seconds) {
    global $woocommerce;

    $woocommerce->session->set_customer_session_cookie(true);
        return 60 * 30;
        WC()->cart->empty_cart();
        WC()->session->destroy_session();
}
1 Answers
/**
 * Set session expiration.
 */
public function set_session_expiration() {
    $this->_session_expiring   = time() + intval( apply_filters( 'wc_session_expiring', 60 * 60 * 47 ) ); // 47 Hours.
    $this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 48 ) ); // 48 Hours.
}

Above is the function that sets session expiry for WC. You may use the filter to change the value like below.

    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 60 * 30;
    }
Related