您的代码中有一些错误WC_Order
自 WooCommerce 3 起不再可访问的属性并被 getter 和 setter 方法取代,请参阅这个线程 https://stackoverflow.com/a/44708344/3730754…
当您以编程方式创建一些用户时,要发送客户新帐户电子邮件通知,您可以使用WC_Email_Customer_New_Account trigger() https://docs.woocommerce.com/wc-apidocs/source-class-WC_Email_Customer_New_Account.html#89-114有 3 个参数变量的方法:
-
$user_id
-- 用户 ID(必填)
-
$user_pass
-- 用户密码(可选)- 根据您的情况需要。
-
$password_generated
-- 密码是否自动生成(true 或 false)。当设置为时,此可选参数在电子邮件通知上显示密码true
(默认为假).
要使生成的密码显示在“客户新帐户”电子邮件通知中,你还需要启用在 WooCommerce > 设置 > 帐户和隐私选项行中:
“创建账户时,自动生成账户密码”
完成后,您的工作代码将是:
// 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 );
现在,您将看到一条通知已发送给客户,其中包含其登录名和密码,以及登录区域的链接...
“客户新帐户电子邮件”的模板自定义:
相关模板路径为plugins/woocommerce/templates/emails/customer-new-account.php
并且可以通过将其复制到来覆盖yourtheme/woocommerce/emails/customer-new-account.php
传递给此模板的可用参数(变量)有:
- $email_heading -- 电子邮件标题,
- $user_login -- 用户登录,
- $user_pass -- 用户密码,
- $blogname -- 网站标题,
- $password_ generated -- 密码是否自动生成,
- $sent_to_admin(默认为 false),
- $plain_text(默认为 false),
- $email -- 电子邮件实例对象。