沃梦达 / 编程问答 / php问题 / 正文

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

Automatically create an user account after WooCommerce checkout and auto login(在WooCommerce结帐和自动登录后自动创建用户帐户)

本文介绍了在WooCommerce结帐和自动登录后自动创建用户帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的目标是在结账和自动登录后直接自动创建客户帐户。

这是我使用的代码:

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

没有错误消息,但也没有创建用户帐户。有什么建议吗?

推荐答案

使用此答案时,请确保在WooCommerce&>设置&>帐户&;隐私中:

  • 禁用:允许客户在结账过程中创建帐户

  • 启用:创建帐户时,自动生成帐户密码


此答案中使用:

  • How to automatically create an account if a product in the order belongs to a certain category in WooCommerce
  • Send an email notification with the generated password on WooCommerce user creation

因此您得到:

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->get_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->get_billing_address_1() );
        update_user_meta( $user_id, 'billing_address_2', $order->get_billing_address_2() );
        update_user_meta( $user_id, 'billing_city', $order->get_billing_city() );
        update_user_meta( $user_id, 'billing_company', $order->get_billing_company() );
        update_user_meta( $user_id, 'billing_country', $order->get_billing_country() );
        update_user_meta( $user_id, 'billing_email', $order_email );
        update_user_meta( $user_id, 'billing_first_name', $order->get_billing_first_name() );
        update_user_meta( $user_id, 'billing_last_name',  $order->get_billing_last_name() );
        update_user_meta( $user_id, 'billing_phone', $order->get_billing_phone() );
        update_user_meta( $user_id, 'billing_postcode', $order->get_billing_postcode() );
        update_user_meta( $user_id, 'billing_state', $order->get_billing_state() );

        // User's shipping data
        update_user_meta( $user_id, 'shipping_address_1', $order->get_shipping_address_1() );
        update_user_meta( $user_id, 'shipping_address_2', $order->get_shipping_address_2() );
        update_user_meta( $user_id, 'shipping_city', $order->get_shipping_city() );
        update_user_meta( $user_id, 'shipping_company', $order->get_shipping_company() );
        update_user_meta( $user_id, 'shipping_country', $order->get_shipping_country() );
        update_user_meta( $user_id, 'shipping_first_name', $order->get_shipping_first_name() );
        update_user_meta( $user_id, 'shipping_last_name', $order->get_shipping_last_name() );
        update_user_meta( $user_id, 'shipping_method', $order->get_shipping_method() );
        update_user_meta( $user_id, 'shipping_postcode', $order->get_shipping_postcode() );
        update_user_meta( $user_id, 'shipping_state', $order->get_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->get_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 );

这篇关于在WooCommerce结帐和自动登录后自动创建用户帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在WooCommerce结帐和自动登录后自动创建用户帐户

基础教程推荐