0

I am implementing an LMS using one plus theme,learndash and woocommerce plugins. Currently when a user log out, his cart persist and the item in his cart can be edited by a guest user.I want to clear the cart after a user logout from his account.I tried the answer, given in this link for fixing the issue https://stackoverflow.com/a/32785631/6270441. I tried putting this code in my themes function.php,but it is not working.

function your_function() {
    if( function_exists('WC') ){
        WC()->cart->empty_cart();
    }
}
add_action('wp_logout', 'your_function');
2
  • Are you using any caching plugin in your site? Commented Jan 8, 2019 at 9:17
  • no I am not using any caching plugins
    – beginner
    Commented Jan 8, 2019 at 9:22

1 Answer 1

1

Try this code by using global $woocommerce

add_action( 'wp_logout', 'force_clear_woocommerce_cart' );
function force_clear_woocommerce_cart() 
{

    error_log("Clearing cart");
    global $woocommerce;
    $woocommerce->cart->empty_cart();

}

hope this will help you

5
  • well I tried. no changes.In localhost no issues like this.when I hosted the site this problem appeared, and no additional codes i added in functions.php is working.
    – beginner
    Commented Jan 8, 2019 at 9:29
  • Thanks for responding.But how to set user id?
    – beginner
    Commented Jan 8, 2019 at 9:33
  • move the add_action below the function Commented Jan 8, 2019 at 9:33
  • and check it is working or not Commented Jan 8, 2019 at 9:34
  • i've tried to implement this solution on my website, but it doesn't work and I really can't understand the reason. Do you have any suggestion? Commented Oct 2, 2019 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.