wordpress - Save cart on the current session -
i'm building wordpress ecommerce site woocommerce plugin, turns when user gets logged in , add products cart, user don't want proceed checkout process, user prefers logout , continue checkout process later... when user comes , gets logged in again cart empty.
what going on here?. normal behavior of woocommerce?. have else? maybe plugin?
thanks.
i thought cart emptied when user logs out, , tracked down.
on wp_logout()
wordpress runs wp_clear_auth_cookie()
function. wp_clear_auth_cookie()
triggers do_action( 'clear_auth_cookie' );
action hook.
woocommerce's session handler class runs it's destroy method on hook.
add_action( 'clear_auth_cookie', array( $this, 'destroy_session' ) );
the destroy_session()
method calls wc_empty_cart()
function, wrapper cart class's empty_cart()
method.
wc()->cart->empty_cart( false );
but key thing here parameter false
. because when track down empty_cart()
method see default true
.
/** * empties cart , optionally persistent cart too. * * @access public * @param bool $clear_persistent_cart (default: true) * @return void */ public function empty_cart( $clear_persistent_cart = true ) { $this->cart_contents = array(); $this->reset(); unset( wc()->session->order_awaiting_payment, wc()->session->applied_coupons, wc()->session->coupon_discount_amounts, wc()->session->cart ); if ( $clear_persistent_cart && get_current_user_id() ) { $this->persistent_cart_destroy(); } do_action( 'woocommerce_cart_emptied' ); }
when passing true
persistant_cart_destroy()
method called , this method deletes meta data user's cart kept.
/** * delete persistent cart permanently. * * @access public * @return void */ public function persistent_cart_destroy() { delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart' ); }
so, of not think cart should emptied when user logs out , in. little more evidence woocommerce attempts load persistent cart user logs in.
/** * load cart upon login * * @param mixed $user_login * @param integer $user * @return void */ function wc_load_persistent_cart( $user_login, $user = 0 ) { if ( ! $user ) return; $saved_cart = get_user_meta( $user->id, '_woocommerce_persistent_cart', true ); if ( $saved_cart ) if ( empty( wc()->session->cart ) || ! is_array( wc()->session->cart ) || sizeof( wc()->session->cart ) == 0 ) wc()->session->cart = $saved_cart['cart']; } add_action( 'wp_login', 'wc_load_persistent_cart', 1, 2 );
i try disabling other plugins see if behavior reverts think normal behavior. there, can re-enable them 1 @ time isolate culprit.
Comments
Post a Comment