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');
  • Are you using any caching plugin in your site? – Tejas Gajjar Jan 8 at 9:17
  • no I am not using any caching plugins – beginner Jan 8 at 9:22
2

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

  • 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 at 9:29
  • Thanks for responding.But how to set user id? – beginner Jan 8 at 9:33
  • move the add_action below the function – Tejas Gajjar Jan 8 at 9:33
  • and check it is working or not – Tejas Gajjar Jan 8 at 9:34
  • That works.Thank you so much. – beginner Jan 8 at 9:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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