4

I'm using WordPress and the Easy Digital Downloads plugin but the items in the cart are expiring after 30 minutes of inactivity, I want to increase this to something like 48 hours because 30 minutes is way too short for items in the cart to be removed.

I've ask on the EDD forums how to increase this expiration time but I'm just given links to add_filter tutorials that are not really helping me with this specific filter.

This is the filter I was told to tap into to change the expiration time: https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/class-edd-session.php#L93

So I've tried:

add_filter( 'wp_session_expiration_variant', array( 'WP_Session', '86400 * 60' ), 99999 );
add_filter( 'wp_session_expiration', array( 'WP_Session', '86400 * 60' ), 99999 );

and

add_filter( 'wp_session_expiration_variant', array( 'EDD_Session', '86400 * 60' ), 99999 );
add_filter( 'wp_session_expiration', array( 'EDD_Session', '86400 * 60' ), 99999 );

Neither works. Any ideas how to increase the session timeout?

Nate M.
  • 111
  • 2
  • 12

4 Answers4

2

Searched on net and going through the link which you gave. there is a function which is creating a cookie for a user for 30mins you need to reconfigure this cookie as per your need.

The following code might help you to achieve this purpose.

function change_expiration_cookie() {
    $cart = edd_get_cart_contents();
    if ( isset( $_COOKIE['edd_items_in_cart'] ) ) {
        $items = $_COOKIE['edd_items_in_cart'];
        @setcookie( 'edd_items_in_cart', $items, time() + 2880 * 60, COOKIEPATH, COOKIE_DOMAIN, false );
    } elseif ( $cart != false ) {
        $items = count( $cart );
        @setcookie( 'edd_items_in_cart', $items, time() + 2880 * 60, COOKIEPATH, COOKIE_DOMAIN, false );
    }
}
add_action( 'init', 'change_expiration_cookie' );

This will set that cookie to expire after 48 Hours. Tested the same. Lemme know if it works for you too.

Shahjahan Jewel
  • 2,301
  • 24
  • 23
Domain
  • 11,562
  • 3
  • 23
  • 44
1

Here is another way you can do it:

function nate_modify_cart_cookie() {
    if ( isset( $_COOKIE['edd_items_in_cart'] ) ) {
        $items = $_COOKIE['edd_items_in_cart'];
        @setcookie( 'edd_items_in_cart', $items, time() + 120 * 60, COOKIEPATH, COOKIE_DOMAIN, false );
    }
}
add_action('init', 'nate_modify_cart_cookie');

Basically it checks to see if the EDD cart cookie is already set, then sets it again with an increased expiration time. I’m sure there’s a better way to do this but it should prevent your carts from expiring every 30 minutes.

Shahjahan Jewel
  • 2,301
  • 24
  • 23
0

Nothing was working for me so I hired someone to do it. This function increases the Easy Digital Downloads cart expiration to 48 hours:

add_filter( 'wp_session_expiration_variant', function() { return 46 * 60 * 60; }, 999999 );
add_filter( 'wp_session_expiration', function() { return 48 * 60 * 60; }, 999999 );
add_filter( 'edd_use_php_sessions', function() { return false; }, 999999 );
Nate M.
  • 111
  • 2
  • 12
0

It's because the session is created in the plugin_loaded action which happens before any theme code can run. So adding the filters has no effect as the thing that runs the filters has already executed before your code can add them to the queue.

I worked around this by creating a small custom plugin that can apply the custom filters before the WP Session plugin initialises for the first time.

Add something like the following to a custom plugin file:

add_action('plugins_loaded', function () {
    add_filter('wp_session_expiration_variant', function () {
        return 20 * 60 * 60;
    });

    add_filter('wp_session_expiration', function () {
        return 24 * 60 * 60;
    });
}, 1);

source

Nils R.
  • 1
  • 3