Woocommerce cart is not clear after logout

admin2025-06-04  54

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 . 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');

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/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');
Share Improve this question edited Jan 8, 2019 at 9:33 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 8, 2019 at 9:05 beginnerbeginner 1672 silver badges10 bronze badges 2
  • Are you using any caching plugin in your site? – Tejas Gajjar Commented Jan 8, 2019 at 9:17
  • no I am not using any caching plugins – beginner Commented Jan 8, 2019 at 9:22
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749033550a315795.html

最新回复(0)