• Resolved abhisekpadhi

    (@abhisekpadhi)


    I’m using latest Woocommerce & facing the problem of cart not clearing after customer logout. Customer login, adds some products to cart, and then log out, products still show in cart. I found some github and StackOverflow threads.

    Solution 1 (Does not work)

    //Clear cart after logout
    add_action( 'wp_logout', 'test_destroy_persistent_cart' );
    function test_destroy_persistent_cart(){
        if(function_exists('wc_empty_cart')){
            wc_empty_cart();
        }
    }
    Solution 2 (Destroys cart completely, undesirable results)
    function your_function() {
        if( function_exists('WC') ){
            WC()->cart->empty_cart();
        }
    }
    add_action('wp_logout', 'your_function');

    I believe woocommerce does this by default out of the box. Unfortunately I am facing the issue of cart not clearing after customer logout. Any tips on how to troubleshoot ?

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Why do you want the cart to clear after they logout? Isn’t it a lot better for your store to keep products in the customer’s cart?

    Not sure I understand why you can’t use solution two though? Difference between clearing the cart and “destroying the cart”?

    Thread Starter abhisekpadhi

    (@abhisekpadhi)

    It’s better for customer to have products in the cart showing after they log out and I’m completely in favour of it.

    It’s just that some customers are complaining that they don’t want their cart to show their products after logout, because they use shared computers and it’s their privacy. So, it makes sense to have cart on my website clear after customer logout.

    Solution 2 Empties the cart after logout (good), but after login customer’s cart is also emptied (bad).

    So, what do you thing best approach would be ? Leave woocommerce at its default behaviour. Or clear cart after logout. If later, how do I do it ?

    Plugin Contributor Mike Jolley

    (@mikejolley)

    If the user has an account, the cart is persistent when they re-log in. It should still be cleared on logout as we have code in place to handle this.

    If its not clearing, you either have a plugin stoping this clear from taking place, or you’re seeing a cached cart.

    https://github.com/woothemes/woocommerce/blob/66c61eebc83348af4c42856539cd918a8e181fa9/includes/class-wc-session-handler.php#L71

    Thread Starter abhisekpadhi

    (@abhisekpadhi)

    Okay cool. No plugin was causing the issue. It was PHP5.6 Opcache which led the minicart and viewcart pages to produce cached content even after logout. So, I’ll be blacklisting them from Opcache and test.

    For cofnirmation, I did test with a fresh woocommerce installation and eveything worked in the fashion as Mike said. It’s probably caching that is causing the issue.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Cart not clearing after logout’ is closed to new replies.