简体   繁体   English

订购特定产品后,发送带有用户凭据的自定义 WooCommerce 邮件

[英]Send custom WooCommerce mail with user credentials after order with certain product

I am trying to send a custom WooCommerce mail with user credentials after an order with certain product.我正在尝试在订购特定产品后发送带有用户凭据的自定义 WooCommerce 邮件。 To explain further: Someone buys a product进一步解释:有人购买产品

  1. I have to check if the order contains a certain product id我必须检查订单是否包含某个产品 ID
  2. I have to check if the customer is registered as a user already, if not it should create a user我必须检查客户是否已经注册为用户,如果没有,它应该创建一个用户
  3. Then it should send a custom mail with those credentials to the customer然后它应该将带有这些凭据的自定义邮件发送给客户

I am struggling to get the following code to work.我正在努力使以下代码正常工作。 Especially I dont know how I can add my custom email-message with the credentials and a custom login link.特别是我不知道如何添加带有凭据和自定义登录链接的自定义电子邮件消息。 Do you know a solution?你知道解决办法吗?

add_action( 'woocommerce_thankyou', 'check_order_product_id' );

function check_order_product_id( $order_id ){
    $order = wc_get_order( $order_id );
    $items = $order->get_items(); 
    $order_email = $order->billing_email;
    
    //create custom mail
    function get_custom_email_html( $order, $heading = false, $mailer ) {
        $template = 'emails/my-custom-email-i-want-to-send.php';
        return wc_get_template_html( $template, array(
            'order'         => $order,
            'email_heading' => $heading,
            'sent_to_admin' => false,
            'plain_text'    => false,
            'email'         => $mailer
        ) );
    }
    // load the mailer class
    $mailer = WC()->mailer();
    //format the email
    $recipient = $order_email;
    $subject = __("Deine Login-Daten für Geomap", 'theme_name');
    $content = get_custom_email_html( $order, $subject, $mailer );
    $headers = "Content-Type: text/html\r\n";
    //send the email through wordpress
    $mailer->send( $recipient, $subject, $content, $headers );
    
    foreach ( $items as $item_id => $item ) {
       $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
       if ( $product_id === XYZ ) {
            //get the user email from the order and check if registered already
            function email_exists( $order_email ) {
                $user = get_user_by( 'email', $order_email );
                if ( $user ) {
                    return $wp_new_user_notification_email;
                }
                else {
                // random password with 12 chars
                $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 );
                return $wp_new_user_notification_email;
              }
            }
       }
    }
}

Hey for the question "how I can add my custom email-message with the credentials and a custom login link", In your code, there is this line您好,对于“如何使用凭据和自定义登录链接添加自定义电子邮件消息”这个问题,您的代码中有这一行

$template = 'emails/my-custom-email-i-want-to-send.php';

you could add the credentials there, you may pass other arguments as the second parameter of this function wc_get_template_html second, the code order will not work for what you are trying to achieve您可以在那里添加凭据,您可以将其他 arguments 作为此 function wc_get_template_html第二个参数传递,代码顺序不适用于您要实现的目标

in this line, you are sending the email在此行中,您要发送 email

$mailer->send( $recipient, $subject, $content, $headers );

And after that, there is a foreach loop for all the items in the order and creates a function looking if the user exists.之后,有一个针对订单中所有项目的 foreach 循环,并创建一个 function 来查看用户是否存在。

foreach ( $items as $item_id => $item ) {
       $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
       if ( $product_id === XYZ ) {
            //get the user email from the order and check if registered already
            function email_exists( $order_email ) {

This function is not been called.这个 function 没有被调用。

I will suggest to take out that function from the foreach loop and call it inside if your goal is to check only if the product XYZ exists如果您的目标是仅检查产品 XYZ 是否存在,我建议从 foreach 循环中取出 function 并在内部调用它

and all of it before the email以及 email 之前的所有内容

    function check_order_product_id( $order_id ){
  $order = wc_get_order( $order_id );
  $items = $order->get_items();
  $order_email = $order->billing_email;

  //create custom mail
  function get_custom_email_html( $order, $heading = false, $mailer ) {
    $template = 'emails/my-custom-email-i-want-to-send.php';
    return wc_get_template_html( $template, array(
        'order'         => $order,
        'email_heading' => $heading,
        'sent_to_admin' => false,
        'plain_text'    => false,
        'email'         => $mailer
    ) );
  }
  function send_custom_email($order_email, $order){
    // load the mailer class
    $mailer = WC()->mailer();
    //format the email
    $recipient = $order_email;
    $subject = __("Deine Login-Daten für Geomap", 'theme_name');
    $content = get_custom_email_html( $order, $subject, $mailer );
    $headers = "Content-Type: text/html\r\n";
    //send the email through wordpress
    $mailer->send( $recipient, $subject, $content, $headers );
  }

  function email_exists( $order_email ) {
    $user = get_user_by( 'email', $order_email );
    if ( $user ) {
      send_custom_email($order_email, $order);
//not sure where this variable was comming from
      return $wp_new_user_notification_email;
    }
    else {

      // random password with 12 chars
      $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 );
//not sure where this variable was comming from
      return $wp_new_user_notification_email;
    }
  }



  foreach ( $items as $item_id => $item ) {
    $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
    if ( $product_id === XYZ ) {
      //get the user email from the order and check if registered already
      email_exists( $order_email );
    }
  }
}

Now as a conclusion, WP_user class doesn't give access to the password for security reasons, and they don't send it by email, you should not either is a very bad practice.现在作为结论,出于安全原因,WP_user class 不允许访问密码,并且他们不会通过 email 发送密码,你不应该是一个非常糟糕的做法。

I will suggest you create the account and let WP send the default email and then you send another email with the link to your custom page, but not with the credentials.我建议您创建帐户并让 WP 发送默认的 email,然后您发送另一个 email,其中包含指向您的自定义页面的链接,但不包含凭据。 That way they will receive two emails one secure and one with the special conditions.这样他们将收到两封电子邮件,一封是安全的,另一封是有特殊条件的。

Because you mentioned low barrier to entry being very important...因为你提到低进入门槛非常重要......

I'd recommend linking to your thank you page and having the registration link there too.我建议链接到您的感谢页面并在那里也有注册链接。 You can use SSO (Google/Amazon/etc.) options so that users don't need to register directly through WordPress.您可以使用 SSO (Google/Amazon/etc.) 选项,这样用户就不需要直接通过 WordPress 注册。

I'm assuming the product ID has some kind of activation code?我假设产品 ID 有某种激活码? You can send that, encrypted, as a parameter to the registration page so that it's registered in their session. Once logged in, you have attached their activation code to the user account, and you're done.您可以将其作为参数加密发送到注册页面,以便它在他们的 session 中注册。登录后,您已将他们的激活码附加到用户帐户,您就完成了。

Steps:脚步:

  1. Customer buys客户购买

  2. If they have an account: send them the link, and they're done.如果他们有帐户:将链接发送给他们,他们就完成了。

  3. If they don't:如果他们不这样做:

    a.一种。 Send them an email with a link with the encrypted activation code.向他们发送带有加密激活码链接的 email。

    b. b. They will have to register (through SSO or through WordPress directly).他们必须注册(通过 SSO 或直接通过 WordPress)。

    c. Once registered, you attach their product to their user account, and redirect them to the page they care about. c。注册后,您将他们的产品附加到他们的用户帐户,并将他们重定向到他们关心的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM