【问题标题】:Send an email notification with the generated password on WooCommerce user creation在创建 WooCommerce 用户时使用生成的密码发送电子邮件通知
【发布时间】:2020-05-03 14:44:32
【问题描述】:

在 WooCommerce 中,我使用以下代码创建新的 WP_User,并使用 随机密码 并将用户角色设置为“客户”(我想在购买时自动创建帐户)。然后我使用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 );

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

【问题讨论】:

    标签: php wordpress woocommerce email-notifications user-data


    【解决方案1】:

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

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

    • $user_id -- 用户 ID(必填)
    • $user_pass -- 用户密码(可选)- 在您的情况下是必需的。
    • $password_generated -- 密码是否自动生成(真或假)。当设置为 true (默认为 false) 时,此可选参数会在电子邮件通知中显示密码。

    要使生成的密码显示在“客户新帐户”电子邮件通知中,您还需要在 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 -- email_header,
    • $user_login -- 用户登录,
    • $user_pass -- 用户密码,
    • $blogname -- 网站标题,
    • $password_generated -- 密码是否自动生成,
    • $sent_to_admin(默认为 false),
    • $plain_text(默认为 false),
    • $email -- 电子邮件实例对象。

    【讨论】:

    • 天哪,非常感谢。奇迹般有效。刚学到新东西!非常感谢
    • 洛伊克,你是最棒的!
    • 完美答案。这是否可以在自定义插件中复制 woocommerce 电子邮件模板并对其进行编辑。默认情况下,电子邮件从 Hi {username} 开始,我想将其更改为 Hi {first_name}。
    【解决方案2】:

    尝试启用以下选项并检查邮件是否包含密码。

    这个没测试OK

    【讨论】:

    • 我从一开始就启用了此功能,但不起作用:(
    • @Karnak - 你是如何创建用户帐户的。?在结帐时,有输入密码的选项。在哪个钩子上/?
    • 不,用户没有选项在结帐时创建帐户,因为在我们的例子中,每次使用都必须在结帐后有帐户,所以我们在上面的代码中在这一行 $user_id = wp_create_user( $order_email , $user_pass, $order_email );
    • 如果您禁用访客结帐,每个用户都必须创建一个帐户,并且可以选择在结帐时输入密码。这是默认的 WC 功能
    【解决方案3】:

    有一种更直接的方法可以让您免于编写大量样板代码:WooCommerce wc_create_new_customer 函数。

    如果您只向它传递用户电子邮件,它将负责生成密码和用户名并自动发送新帐户电子邮件。

    wc_create_new_customer($order_email);
    

    只需确保您已在 WooCommerce > 设置 > 帐户 中选中“自动生成用户帐户”和“自动生成用户名”。

    还要确保您已在 WooCommerce > 设置 > 电子邮件 中启用帐户创建电子邮件。

    【讨论】:

      猜你喜欢
      • 2012-05-07
      • 2012-01-07
      • 2019-07-26
      • 2013-10-29
      • 1970-01-01
      • 2018-12-05
      • 2015-06-10
      • 1970-01-01
      相关资源
      最近更新 更多