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

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

问题描述

我正在使用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的Session处理程序类随后在此钩子上运行它的destroy方法。

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()方法被调用,正是方法删除了保存用户购物车的元数据。

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' );
    }

SO,所有这些都是说我不做 认为当用户注销然后再登录时,应清空购物车。还有更多证据表明,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天全站免登陆