在当前会话中保存购物车

use*_*385 1 wordpress cart woocommerce

我正在使用woocommerce插件构建一个wordpress电子商务网站,它转变为当用户登录并将产品添加到他的购物车时,但是用户不想进行结帐流程,用户更喜欢注销并继续稍后结账时...当用户回来并再次登录时,购物车为空.

这里发生了什么?.这是woocommerce的正常行为吗?我还需要做别的吗?也许是一个插件?

谢谢.

hel*_*ing 7

当用户退出时,我认为购物车已清空,我最终将其追踪.

在wp_logout()WordPress上运行wp_clear_auth_cookie()功能.wp_clear_auth_cookie()触发do_action( 'clear_auth_cookie' );动作钩子.

WooCommerce的Session处理程序类然后在这个钩子上运行它的destroy方法.

add_action( 'clear_auth_cookie', array( $this, 'destroy_session' ) );
Run Code Online (Sandbox Code Playgroud)

destroy_session()然后该方法调用该wc_empty_cart()函数,该函数是cart类的empty_cart()方法的包装器.

WC()->cart->empty_cart( false ); 
Run Code Online (Sandbox Code Playgroud)

但关键是参数是false.因为当我们最终追踪该empty_cart()方法时,我们会看到默认值为true.

    /**
     * Empties the cart and optionally the 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' );
    }
Run Code Online (Sandbox Code Playgroud)

当通过true该persistant_cart_destroy()方法被调用,它是这个方法删除该用户的车保持的元数据.

    /**
     * Delete the persistent cart permanently.
     *
     * @access public
     * @return void
     */
    public function persistent_cart_destroy() {
        delete_user_meta( get_current_user_id(), '_woocommerce_persistent_cart' );
    }
Run Code Online (Sandbox Code Playgroud)

所以,所有这一切都是说我不认为当用户退出然后重新登录时应该清空购物车.更多证据表明,WooCommerce会在用户重新登录时尝试加载持久购物车.

/**
 * Load the 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 );
Run Code Online (Sandbox Code Playgroud)

我会尝试禁用所有其他插件,看看行为是否恢复到我认为是正常行为.从那里,您可以一次重新启用它们以隔离罪魁祸首.