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') ){
add_action('wp_logout', 'your_function');
  • Are you using any caching plugin in your site? Jan 8, 2019 at 9:17
  • no I am not using any caching plugins
    – beginner
    Jan 8, 2019 at 9:22

1 Answer 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;


hope this will help you

  • 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
    Jan 8, 2019 at 9:29
  • Thanks for responding.But how to set user id?
    – beginner
    Jan 8, 2019 at 9:33
  • move the add_action below the function Jan 8, 2019 at 9:33
  • and check it is working or not 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? 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.