简体   繁体   English

WooCommerce 结帐并自动登录后自动创建用户帐户

[英]Automatically create an user account after WooCommerce checkout and auto login

The goal here is to automatically create a customer account directly after checkout and auto login.这里的目标是在结帐和自动登录后直接自动创建一个客户帐户。

This is the code I am using:这是我正在使用的代码:

add_action( 'woocommerce_thankyou', 'add_as_customer_after_checkout', 100, 1 );
function add_as_customer_after_checkout( $order_id ) {

    $order = new WC_Order($order_id);

    $user = $order->get_user();

        if ( false != $user && !user_can($user, 'administrator') ) {
        
            $role = 'customer';
        
            $user->add_role($role);
    }
}

This has no error messages, but the user account is not created either.这没有错误消息,但也没有创建用户帐户。 Any advice?有什么建议吗?

While using this answer, make sure in WooCommerce > Settings > Accounts & Privacy, you:使用此答案时,请确保在 WooCommerce > 设置 > 帐户和隐私中,您:

  • Disable: "Allow customers to create an account during checkout"禁用:“允许客户在结帐时创建帐户”

  • Enable: "When creating an account, automatically generate an account password"启用:“创建账号时,自动生成账号密码”


Used in this answer:在这个答案中使用:

So you get:所以你得到:

function action_woocommerce_thankyou( $order_id ) {
    // Determines whether the current visitor is a logged in user.
    if ( is_user_logged_in() ) return;
    
    // Get $order object
    $order = wc_get_order( $order_id );
    
    // Get the user email from the order
    $order_email = $order->billing_email;

    // Check if there are any users with the billing email as user or email
    $email = email_exists( $order_email );  
    $user = username_exists( $order_email );

    // If the UID is null, then it's a guest checkout (new user)
    if ( $user == false && $email == false ) {
        // Random password with 12 chars
        $random_password = wp_generate_password();
        
        // Firstname
        $first_name = $order->get_billing_first_name();
        
        // Lastname
        $last_name = $order->get_billing_last_name();
        
        // Role
        $role = 'customer';

        // Create new user with email as username, newly created password and userrole          
        $user_id = wp_insert_user(
            array(
                'user_email' => $order_email,
                'user_login' => $order_email,
                'user_pass'  => $random_password,
                'first_name' => $first_name,
                'last_name'  => $last_name,
                'role'       => $role,
            )
        );
        
        // Get all WooCommerce emails Objects from WC_Emails Object instance
        $emails = WC()->mailer()->get_emails();

        // Send WooCommerce "Customer New Account" email notification with the password
        $emails['WC_Email_Customer_New_Account']->trigger( $user_id, $random_password, true );

        // (Optional) WC guest customer identification
        //update_user_meta( $user_id, 'guest', 'yes' );

        // User's billing data
        update_user_meta( $user_id, 'billing_address_1', $order->billing_address_1 );
        update_user_meta( $user_id, 'billing_address_2', $order->billing_address_2 );
        update_user_meta( $user_id, 'billing_city', $order->billing_city );
        update_user_meta( $user_id, 'billing_company', $order->billing_company );
        update_user_meta( $user_id, 'billing_country', $order->billing_country );
        update_user_meta( $user_id, 'billing_email', $order->billing_email );
        update_user_meta( $user_id, 'billing_first_name', $order->billing_first_name );
        update_user_meta( $user_id, 'billing_last_name', $order->billing_last_name );
        update_user_meta( $user_id, 'billing_phone', $order->billing_phone );
        update_user_meta( $user_id, 'billing_postcode', $order->billing_postcode );
        update_user_meta( $user_id, 'billing_state', $order->billing_state );

        // User's shipping data
        update_user_meta( $user_id, 'shipping_address_1', $order->shipping_address_1 );
        update_user_meta( $user_id, 'shipping_address_2', $order->shipping_address_2 );
        update_user_meta( $user_id, 'shipping_city', $order->shipping_city );
        update_user_meta( $user_id, 'shipping_company', $order->shipping_company );
        update_user_meta( $user_id, 'shipping_country', $order->shipping_country );
        update_user_meta( $user_id, 'shipping_first_name', $order->shipping_first_name );
        update_user_meta( $user_id, 'shipping_last_name', $order->shipping_last_name );
        update_user_meta( $user_id, 'shipping_method', $order->shipping_method );
        update_user_meta( $user_id, 'shipping_postcode', $order->shipping_postcode );
        update_user_meta( $user_id, 'shipping_state', $order->shipping_state );

        // Link past orders to this newly created customer
        wc_update_new_customer_past_orders( $user_id );
        
        // Auto login
        wp_set_current_user( $user_id );
        wp_set_auth_cookie( $user_id );
    }  
}
add_action( 'woocommerce_thankyou', 'action_woocommerce_thankyou', 10, 1 ); 

function filter_woocommerce_thankyou_order_received_text( $str, $order ) {
    // Determines whether the current visitor is a logged in user.
    if ( is_user_logged_in() ) return $str;
    
    // Get the user email from the order
    $order_email = $order->billing_email;
    
    // Check if there are any users with the billing email as user or email
    $email = email_exists( $order_email );  
    $user = username_exists( $order_email );

    // If the UID is null, then it's a guest checkout (new user)
    if ( $user == false && $email == false ) {
        // Link
        $link = get_permalink( get_option( 'woocommerce_myaccount_page_id' ) );

        // Format
        $format_link = '<a href="' . $link . '">logged in</a>';

        // Append to orginal string
        $str .= sprintf( __( ' An account has been automatically created for you and you are now %s. You will receive an email about this.', 'woocommerce' ), $format_link ); 
    }       

    return $str;
}
add_filter( 'woocommerce_thankyou_order_received_text', 'filter_woocommerce_thankyou_order_received_text', 10, 2 );

@7uc1f3r this code works with the order status processing hook? @7uc1f3r 此代码适用于订单状态处理挂钩吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自动检查 Woocommerce 结帐页面中的创建帐户字段 - Auto check Create account field in Woocommerce Checkout Page 结帐前重定向到登录(我的帐户)并在登录后返回结帐 - Woocommerce - Redirect to login (my-account) before checkout and return to checkout after login - Woocommerce 将用户自定义帐户字段添加到 WooCommerce 结帐 - Add user custom account field to WooCommerce checkout Woocommerce Checkout后注销用户 - Logout user after Woocommerce Checkout WooCommerce结帐“创建帐户”复选框有条件的消息传递 - WooCommerce checkout “create an account” tickbox conditional messaging 创建帐户并登录后如何将用户重定向到原始URL? - How redirect user to original url after create account and login? 在 WooCommerce 3 中“创建帐户”上方移动结帐订单备注字段 - Move Checkout Order notes field above "create an account" in WooCommerce 3 如何移动 Woocommerce 结帐页面上的创建帐户复选框 - How to move the create account checkbox on Woocommerce checkout page 更新 WordPress 帐户 email 与 WooCommerce 结算 Z0C83F57C786A0B4A39EFAB23731C7EBC - Updating WordPress account email with WooCommerce billing email after checkout WooCommerce 在注册后重定向到我的帐户而不是结帐 - WooCommerce redirects to My Account not to Checkout after Sign Up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM