I'm building a wordpress ecommerce site with the woocommerce plugin, it turns that when an user gets logged in and add products to his cart, but the user don't want to proceed to the checkout process, the user prefers to logout and continue with the checkout process later... when the user comes back and gets logged in again the cart is empty.
What is going on here?. Is this a normal behavior of woocommerce?. Do I have to do something else? maybe a plugin?
Thanks.
I thought that the cart is emptied when a user logs out, and I finally tracked it down.
On
wp_logout()
WordPress runswp_clear_auth_cookie()
function.wp_clear_auth_cookie()
triggers thedo_action( 'clear_auth_cookie' );
action hook.WooCommerce's Session handler class then runs it's destroy method on this hook.
The
destroy_session()
method then calls thewc_empty_cart()
function, which is a wrapper for the cart class'sempty_cart()
method.But the key thing here is that the parameter is
false
. Because when we finally track down theempty_cart()
method we see that the default istrue
.When passing
true
thepersistant_cart_destroy()
method is called and it is this method that deletes the meta data where the user's cart is kept.SO, all of that is to say that I do not think the cart should be emptied when a user logs out and then back in. A little more evidence is that WooCommerce attempts to load the persistent cart as soon as a user logs back in.
I would try disabling all other plugins to see if the behavior reverts back to what I think is the normal behavior. From there, you can re-enable them one at a time to isolate the culprit.
I faced same problem and solved it by placing following function to my functions.php file:
Function checks if the user has persistent cart saved and replaces session cart with it.