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
Try Enabling the below option and check if the mail contains the password.
This is not tested OK
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 totrue
(default is false).Once done, your working code is going to be:
Now as you will see a notification is sent to the customer, with its login and password, with the link to the login area…
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:
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.
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.