问题描述
在带有以下代码的 WooCommerce 中,我使用随机密码创建新的 WP_User 并将用户角色设置为客户"(我想在购买时自动创建帐户).然后我使用 WC_Emails
将登录详细信息发送给买家.在这种情况下,我需要简单的密码,但我真的不知道为什么附加所有其他数据(用户名、姓名等),但电子邮件通知中的密码仍为空.
我的代码是:
//12 个字符的随机密码$user_pass = wp_generate_password();//用电子邮件作为用户名创建新用户 &新创建的密码$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 = 新的 WC_Emails();$wc->customer_new_account($user_id, $user_pass);//WC客人客户标识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 );//用户的账单数据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 );//用户发货数据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 );//将过去的订单链接到这个新创建的客户wc_update_new_customer_past_orders( $user_id );
非常感谢任何帮助或建议.
您的代码中存在一些关于 WC_Order
属性的错误,这些属性自 WooCommerce 3 以来不再可访问,并被 getter 和 setter 方法取代,请参阅
客户新帐户电子邮件"的模板自定义:
相关模板路径为plugins/woocommerce/templates/emails/customer-new-account.php
并且可以通过将其复制到 yourtheme/woocommerce/emails/customer-new-account.php
传递给该模板的可用参数(变量)是:
- $email_heading -- email_header,
- $user_login -- 用户登录,
- $user_pass -- 用户密码,
- $blogname -- 网站的标题,
- $password_generated -- 密码是否自动生成,
- $sent_to_admin(默认为 false),
- $plain_text(默认为 false),
- $email -- 电子邮件实例对象.
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.
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:
// 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...
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.
这篇关于在 WooCommerce 用户创建时使用生成的密码发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!