php - 在创建 WooCommerce 用户时使用生成的密码发送电子邮件通知

标签 php wordpress woocommerce email-notifications user-data

在 WooCommerce 中,我使用以下代码创建新的 WP_User,并使用 随 secret 码 并将用户角色设置为“客户”(我想在购买时自动创建帐户)。然后我使用 WC_Emails 将登录详细信息发送给买家。在这种情况下,我需要纯密码,但我真的不知道为什么附加所有其他数据(用户名、姓名、...),但电子邮件通知中的密码仍然为空。

我的代码是:

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

非常感谢任何帮助或建议。

最佳答案

您的代码中有一些关于 WC_Order 属性的错误,这些错误自 WooCommerce 3 起不再可访问并被 getter 和 setter 方法取代,请参阅 this thread

当您以编程方式创建一些用户时,要发送客户新帐户电子邮件通知,您可以使用 WC_Email_Customer_New_Account trigger()具有 3 个参数变量的方法:

  • $user_id -- 用户 ID(必填)
  • $user_pass -- 用户密码(可选)- 在您的情况下是必需的。
  • $password_generated -- 密码是否自动生成(true 或 false)。当设置为 true (默认为 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"

完成后,您的工作代码将是:

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

现在您将看到一条通知已发送给客户,其中包含其登录名和密码,以及登录区域的链接...

enter image description here


“客户新帐户电子邮件”的模板自定义:

相关模板路径为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 -- 电子邮件实例对象。

关于php - 在创建 WooCommerce 用户时使用生成的密码发送电子邮件通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61576356/

相关文章:

PHP 替换数组中的数组值

php - 需要我的 HTML 搜索表单来搜索在搜索表单上选择的选项,然后显示我的结果?

php - 在子域之间分配和传递 session 变量

django - WordPress 漂亮的永久链接故障转移 SSL - 404 文件不存在

php - 使用 woocommerce hook 获取最近添加的产品

php - 在 woocommerce 中添加具有默认值的自定义隐藏结账字段

php - 是否可以在 Symfony2 中调试 php 代码?

php - 从数组输出 (WordPress PHP) 在纯 css 中创建 slider 图像和文本

javascript - 切换变量以更改 href

php - woocommerce 添加到单个产品页面上的购物车按钮