【问题标题】:Send the reset password email notifications programatically in Woocommerce在 Woocommerce 中以编程方式发送重置密码电子邮件通知
【发布时间】:2018-01-06 01:45:27
【问题描述】:

我想在自动注册的客人在我的 woocommerce 网站上首次下单后向他们发送重置密码电子邮件。

这是我的钩子函数:

function wc_register_guests($order_id) {
    // get all the order data
    $order = new WC_Order($order_id);

    //get the user email from the order
    $order_email = $order->billing_email;

    // check if there are any users with the billing email as user or email
    $email = email_exists($order_email);
    $user = username_exists($order_email);

    // if the UID is null, then it's a guest checkout
    if ($user == false && $email == false) {

        $random_password = wp_generate_password();

        // create new user with email as username & newly created pw
        $user_id = wp_create_user($order_email, $random_password, $order_email);

        //WC guest customer identification
        update_user_meta($user_id, 'guest', 'yes');

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

        wc_update_new_customer_past_orders($user_id);
    }
}
add_action('woocommerce_thankyou', 'wc_register_guests', 10, 1);

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    要通过用户 ID 发送客户重置密码电子邮件通知:

    $user = new WP_User( intval($user_id) );
    $reset_key = get_password_reset_key( $user );
    $wc_emails = WC()->mailer()->get_emails();
    $wc_emails['WC_Email_Customer_Reset_Password']->trigger( $user->user_login, $reset_key );
    

    this related source code function


    现在自从 Woocommerce 3+ 以来,您不能再直接获得 WC_Order 属性,而是应该使用 WC_Order available methods...
    您还应该设置“客户”用户角色...

    所以我重新审视了你的代码:

    add_action('woocommerce_thankyou', 'wc_register_new_customers', 10, 1);
    function wc_register_new_customers( $order_id ) {
        if(is_user_logged_in()) return; // Only for unlogged users
    
        // Get an instance of the WC_Order object
        $order = wc_get_order($order_id);
    
        // Get the customer billing email from the order
        $billing_email = $order->get_billing_email();
    
        // check if there are any users with the billing email as user or email
        $email = email_exists( $billing_email );
        $user = username_exists( $billing_email );
    
        // if the UID is null, then it's a guest checkout
        if ( $user == false && $email == false ) {
    
            $random_password = wp_generate_password();
    
            // Create new user with email as username & newly created pw
            $user_id = wp_create_user( $billing_email, $random_password, $billing_email );
    
            // WC guest customer identification
            update_user_meta( $user_id, 'guest', 'yes' );
    
            //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->get_billing_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() );
    
            // Update to "customer" user role
            update_user_meta( $user_id, 'wp_capabilities', array('customer' => true) );
    
            wc_update_new_customer_past_orders( $user_id );
    
            // Get WP_User
            $user = new WP_User( intval( $user_id ) );
    
            // Creates, stores, then returns a password reset key for user.
            $reset_key = get_password_reset_key( $user );
    
            // Send the WC_email Customer Reset Password
            $wc_emails = WC()->mailer()->get_emails();
            $wc_emails['WC_Email_Customer_Reset_Password']->trigger( $user->user_login, $reset_key );
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。

    经过测试并且有效。

    【讨论】:

    • 是的,用户角色已设置 :) 但我没有收到重置密码电子邮件通知。你知道为什么吗?谢谢
    • 我已经更新了代码,但我还没有收到邮件!一切似乎都是正确的,但没有触发电子邮件通知。我真的很感谢你的帮助:)
    • 好的 :)。仅供参考,我在这一行用 $billing_email 替换了变量 $order_email $user_id = wp_create_user( $order_email, $random_password, $order_email );
    • 很遗憾,不,结果是一样的
    猜你喜欢
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 2010-11-21
    • 2015-03-11
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多