skip to Main Content

In WooCommerce with code below I create new WP_User with a random password and set user role to “customer” (I want to create account on purchase automatically). Then I use WC_Emails to send login details to buyer. In that case I need plain password, but I really don’t know why all other data are attached (username, name,…), but the password remains empty on the email notification.

My code is:

  // random password with 12 chars
$user_pass = wp_generate_password();

// create new user with email as username & newly created pw
$user_id = wp_create_user( $order_email, $user_pass, $order_email );
$user_id_role = new WP_User($user_id);
$user_id_role->set_role('customer');


$wc = new WC_Emails();
$wc->customer_new_account($user_id, $user_pass);




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

update_user_meta( $user_id, 'first_name', $order->billing_first_name );
update_user_meta( $user_id, 'last_name', $order->billing_last_name );


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

Any help or suggestions are highly appreciated.

3

Answers


  1. Try Enabling the below option and check if the mail contains the password.

    This is not tested OK

    enter image description here

    Login or Signup to reply.
  2. There are some mistakes in your code regarding WC_Order properties that are not accessible anymore since WooCommerce 3 and replaced by getter and setter methods, see this thread

    As you are creating some users programmatically, to send the Customer New Account email notification, you can use WC_Email_Customer_New_Account trigger() method that has 3 arguments variables:

    • $user_id — The User ID (required)
    • $user_pass — The User password (optional) – Required in your case.
    • $password_generated — Whether the password was generated automatically or not (true or false). This optional argument displays the password on the email notification when is set to true (default is false).

    To make the generated password displayed in "Customer New Account" email notification, you also need to enable in WooCommerce > Settings > Accounts & Privacy the option line:

    "When creating an account, automatically generate an account password"

    Once done, your working code is going to be:

    // Customer Billing Email
    $order_email = $order->get_billing_email();
    
    // Generate a random password with 12 chars
    $user_pass = wp_generate_password();
    
    // Create new user with email as username & newly generated password
    $user_id = wp_create_user( $order_email, $user_pass, $order_email );
    
    $user = new WP_User($user_id); // Get the WP_User Object instance from user ID
    $user->set_role('customer');   // Set the WooCommerce "customer" user 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, $user_pass, true );
    
    //WC guest customer identification
    update_user_meta( $user_id, 'guest', 'yes' );
    
    update_user_meta( $user_id, 'first_name', $order->$order->get_billing_first_name() );
    update_user_meta( $user_id, 'last_name', $order->get_billing_last_name() );
    
    
    //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 );
    

    Now as you will see a notification is sent to the customer, with its login and password, with the link to the login area…

    enter image description here


    Template customizations for "Customer new account email":

    The related template path is plugins/woocommerce/templates/emails/customer-new-account.php
    and can be overridden by copying it to yourtheme/woocommerce/emails/customer-new-account.php

    The available arguments (variables) passed to this template are:

    • $email_heading — The email_header,
    • $user_login — User Login,
    • $user_pass — User password,
    • $blogname — Title of the site,
    • $password_generated — Whether the password was generated automatically or not,
    • $sent_to_admin (false by default),
    • $plain_text (false by default),
    • $email — The email instance Object.
    Login or Signup to reply.
  3. There’s a much more straightforward way to do it that will spare you from a lot of boilerplate code: the WooCommerce wc_create_new_customer function.

    If you only pass it the user email, it will take care of generating a password and a username and automatically send the new account email.

    wc_create_new_customer($order_email);
    

    Just make sure that you have checked "automatically generate user account" and "automatically generate username" in WooCommerce > Settings > Accounts.

    Also make sure you have enabled the account creation email in WooCommerce > Settings > Emails.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search