$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Woocommerce cart is not clear after logout|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Woocommerce cart is not clear after logout

matteradmin8PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far