I'm running into a little problem with woocommerce's cart.
Description of the issue:
Say I am logged in, I have 3 products in the cart. If I log out, I have 0 products in the cart. I add a product and log back in. I'm now still having that product I added while being logged out. That is fine, as I suppose when you navigate and log in just at the checkout, you don't want old cart items to pop up suddenly. Now, if I log out, I have 0 products, which is also fine, as suddenly you become nobody and you can assume a new user will take on. But, if, with an empty cart I log back in, I now have the 3 products from the beginning, instead of having the 1 I had during my previous visit.
What I'd like to achieve. By default, in the above story, woocommerce would show a cart with 1 item like expected. But in the theme I build, I load the whole cart via ajax. I have a little function that basically just does this :
function sp_get_cart() {
$cart = WC()->cart->get_cart();
wp_send_json($cart);
}
add_action('wp_ajax_get_cart', 'sp_get_cart');
add_action('wp_ajax_nopriv_get_cart', 'sp_get_cart');
I also have the whole signon/signout part working with ajax. I tried to find in the woocommerce code things I may have missed, but I couldn't find anything. Should I do something specific when processing the signon or signout ? I thought I could just rely on the default behavior of woocommerce's session, but something seems broken, and when I log in with items in the cart, it seems these new cart items don't get added to the session stored in the database and when the next signon happens, the previous version is being loaded. Any help would be much appreciated.
edit: here is the logout function
function sp_logout() {
WC()->session->destroy_session();
wp_logout();
ob_clean();
// $_SESSION = array();
http_response_code(200);
wp_send_json(array('status' => 'OK'));
die();
}
add_action('wp_ajax_logout', 'sp_logout');
add_action('wp_ajax_nopriv_logout', 'sp_logout');
Try clearing $_SESSION global variable, on your log out function $_SESSION = array();