在当前会话中保存购物车 [英] Save cart on the current session

查看:15
本文介绍了在当前会话中保存购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

What is going on here?. Is this a normal behavior of woocommerce?. Do I have to do something else? maybe a plugin?

谢谢.

推荐答案

我以为当用户退出时购物车就被清空了,我终于找到了.

I thought that the cart is emptied when a user logs out, and I finally tracked it down.

wp_logout() 上 WordPress 运行 wp_clear_auth_cookie() 函数.wp_clear_auth_cookie() 触发 do_action( 'clear_auth_cookie' ); 动作钩子.

On wp_logout() WordPress runs wp_clear_auth_cookie() function. wp_clear_auth_cookie() triggers the do_action( 'clear_auth_cookie' ); action hook.

WooCommerce 的会话处理程序类然后在这个钩子上运行它的销毁方法.

WooCommerce's Session handler class then runs it's destroy method on this hook.

add_action( 'clear_auth_cookie', array( $this, 'destroy_session' ) );

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

The destroy_session() method then calls the wc_empty_cart() function, which is a wrapper for the cart class's empty_cart() method.

WC()->cart->empty_cart( false ); 

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

But the key thing here is that the parameter is false. Because when we finally track down the empty_cart() method we see that the default is 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' );
    }

当传递 true 时,会调用 persistant_cart_destroy() 方法,并且正是 this 方法删除保存用户购物车的元数据.

When passing true the persistant_cart_destroy() method is called and it is this method that deletes the meta data where the user's cart is kept.

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

所以,所有这些都是说我认为当用户注销然后重新登录时应该清空购物车.更多的证据是 WooCommerce 尝试加载持久性用户重新登录后立即购物车.

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.

/**
 * 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 );

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

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.

这篇关于在当前会话中保存购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆