You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义登录插件导致WooCommerce购物车覆盖问题排查求助

自定义登录插件导致WooCommerce购物车覆盖问题排查求助

Hey there, let's dig into this cart overwriting issue you're facing with your custom login plugin and WooCommerce. I totally get how frustrating it is when user carts get lost or overwritten—trust me, that's a big conversion killer!

First, let's break down why this is happening:
WooCommerce handles guest carts and logged-in carts differently by default. When a guest adds items to their cart, it's stored in a browser cookie. When they log in through the standard WordPress/WooCommerce flow, WooCommerce automatically merges the guest cart with the user's saved logged-in cart. But your custom login code is skipping that critical merge step.

The problem with using only wp_set_current_user() is that it just sets the current user in the session, but doesn't trigger the authentication hooks that WooCommerce relies on to sync carts. WooCommerce listens to hooks like wp_login and woocommerce_login to fire its cart merging logic—if those hooks don't run, it doesn't know to combine the guest cart with the user's existing cart, so it ends up overwriting one with the other.

Here's how to fix your code:
Instead of only setting the current user, you need to either use WordPress's full authentication functions or manually trigger the necessary hooks after setting the user.

This function handles the complete auth flow, including setting cookies, firing hooks, and letting WooCommerce know to merge carts:

add_action('wp_ajax_SubmitPass', 'SubmitPass');
add_action('wp_ajax_nopriv_SubmitPass', 'SubmitPass');
function SubmitPass()
{
    global $wpdb;
    $UserID = intval($_POST['UserID']);
    $UserPass = htmlspecialchars($_POST['UserPass']);
    
    // Fetch the user object to verify credentials
    $user = get_user_by('id', $UserID);
    
    if ($user && wp_check_password($UserPass, $user->user_pass, $user->ID)) {
        // Use wp_signon to handle full authentication flow
        $creds = array(
            'user_login'    => $user->user_login,
            'user_password' => $UserPass,
            'remember'      => true // Adjust based on your site's "remember me" setting
        );
        $logged_in_user = wp_signon($creds, false);
        
        if (is_wp_error($logged_in_user)) {
            wp_send_json_error($logged_in_user->get_error_message());
            wp_die();
        }
        
        // Success response
        wp_send_json_success('Logged in successfully! Your cart has been merged.');
    } else {
        wp_send_json_error('Invalid user ID or password.');
    }
    wp_die();
}

Alternative: If you must use wp_set_current_user()

If you have a specific reason to stick with wp_set_current_user(), manually trigger the hooks that WooCommerce needs:

// After calling wp_set_current_user($UserID);
do_action('wp_login', $user->user_login, $user);
do_action('woocommerce_login', $UserID, $user);

This will tell WooCommerce to run its cart merging logic, combining the guest cart items with the user's existing saved cart instead of overwriting it.

The core issue was that your custom login wasn't signaling to WooCommerce that a login had occurred—without those hooks firing, WooCommerce had no idea to merge the two carts.

备注:内容来源于stack exchange,提问作者MahmoodZamaniNemati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 10:13:02