Woocommerce电子邮件-根据产品数量发送多封电子邮件 [英] Woocommerce Emails - Sending multiple emails based on product count

查看:101
本文介绍了Woocommerce电子邮件-根据产品数量发送多封电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我有一个课程产品,该产品使用WC Fields来收集学生的姓名和电子邮件地址,以及自定义电子邮件模板,该模板在购买此课程产品时发送电子邮件。到目前为止,根据此线程中的答案,我能够收集添加到这些自定义字段中的电子邮件地址,并以密件抄送收件人的身份向每位学生发送一封电子邮件。

In Woocommerce, I have a Course product that uses WC Fields to collect student[s] names and email addresses, and custom email template that sends an email when this course product is purchased. As of now, based on the answer in this thread, I am able to collect the email addresses added to those custom fields and send one email to each student as a BCC recipient.

我现在想拥有电子邮件在邮件正文中包含学生的姓名和电子邮件地址,但是我不希望所有名称都出现在同一封电子邮件中。例如,如果学生A(studenta@example.com)和学生B(studentb@example.com)都参加了同一笔交易,则学生A收到的电子邮件的正文中应仅包含亲爱的学生A,感谢您的注册。请使用您的电子邮件地址Studenta@example.com登录。并且学生B收到的电子邮件应该说相同,但是带有学生B的信息,并且他们应该看不到其他人的信息。

I am now trying to have the emails include the student's name and email address in the body of the email, but I do not want all of the names to appear in the same email. For example, if Student A (studenta@example.com) and Student B (studentb@example.com) are both enrolled in the same purchase, the email received by Student A should just contain in the body something like "Dear Student A, Thank you for enrolling. Use your email address studenta@example.com to login." and the email received by Student B should say the same but with Student B's info, and they should not see the other one's.

所以我必须发送多封电子邮件,

So I would have to send multiple emails, based on the number of students being enrolled in that purchase (which is set in the order meta, added from the purchased product's meta).

我尝试添加while(),具体取决于在该购买中注册的学生人数(在订购的meta中设置,从购买的产品meta添加)。在for()之外继续检查所有项目,直到遍历所有项目,并在每次找到它时再次发送,但我认为 foreach($ items as $ item)最终再次从第一个项目开始,因为它仅向第一个收件人发送两封电子邮件。

I tried adding a while() outside of the for() to continue checking through the items until it had gone through all of the items, and sending again for each time it found, but I think the foreach($items as $item) ends up starting through the first item again, since that sends two emails to only the first recipient.

*** UPDATED ****
我将自定义电子邮件代码(已存储在插件中)更改为:

***UPDATED**** I changed the custom email code (which I've stored in my plugins) to:

    $order = wc_get_order( $order_id );
            $items = $order->get_items(); 
            $course_ordered = false;
            $cqt = 1;
            $emailnum = 0;
        foreach ( $items as $item ) {

            $product_id = $item['product_id'];

            if ( $item['product_id']== 1164 ) {
                $course_ordered = true;
                $emailnum++;
                continue;
            }

        }

    if ( ! $course_ordered )
        return;
    while(  $cqt <= $emailnum ){
        // replace variables in the subject/headings
        $this->find[] = '{order_date}';
        $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );
        $this->find[] = '{order_number}';
        $this->replace[] = $this->object->get_order_number();
        if ( ! $this->is_enabled() || ! $this->get_recipient() )
            return;
        // woohoo, send the email!
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        $cqt++;
    }

还有我的functions.php文件中的代码,来自此答案为:

And the code in my functions.php file, which was from this answer, is:

add_filter( 'woocommerce_email_headers', 'student_email_notification', 20, 3 );
function student_email_notification( $header, $email_id, $order ) {
// Only for 'wc_course_order' notification
if( 'wc_course_order' != $email_id ) return $header; 

$student_emails = array();
//$enroll_num = 0;

// Loop though  Order IDs
foreach( $order->get_items() as $item_id => $item_data ){
    /*$course_qty = $item_data->get_quantity();
    $q = 1;
    while ( $q <= $course_qty){
        $enroll_num++;
        // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - '.$enroll_num, true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - '.$enroll_num, true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - '.$enroll_num, true );
        if( ! empty($student_email) && $full_name != ' ' )
            // Format the name and the email and set it in an array
            $student_emails[] = utf8_decode($full_name . ' <' . $student_email . '>'); // Add name + email to the array
        $q++;
    }*/
        // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - 1', true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - 1', true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - 1', true );
        if( ! empty($student_email) && $full_name != ' ' )
            // Format the name and the email and set it in an array
            $student_emails[] = utf8_decode($full_name . ' <' . $student_email . '>'); // Add name + email to the array


}

// If any student email exist we add it
if( count($student_emails) > 0 ){
    // Remove duplicates (if there is any)
    $student_emails = array_unique($student_emails);
    // Add the emails to existing recipients
    $headers .= 'Cc: ' . implode(',', $student_emails) . "\r\n";
}
return $headers;
}

(如果注释掉的部分未注释,则仅发送到第一个收件人,而不是第二个收件人,如果它是作为两个单独的产品输入的,则如果我删除while()并使用其下面的部分,它将向所有收件人发送一封电子邮件)
它会向所有收件人发送(即从所有自定义字段获取所有电子邮件,并将它们用作抄送或密件抄送,但我将其设置为两次),即向所有人发送相同的电子邮件两次。如何获得两次发送但每次只发送给其中一个收件人的信息?谢谢您的任何建议!

(If the section that is commented out is uncommented, it only sends to the first recipient, and not the second, if it is entered as two separate products, if I remove the while() and use the part below it instead, it sends one email to all recipients) It sends to all recipients (i.e. gets all of the emails from all of the custom fields and uses those as CC or BCC, however I set it) twice, ie sends the same email twice to everyone. How can I get it to send twice but each time to only one of the recipients? Thank you for any suggestions!

推荐答案

我找到了问题上半部分的解决方案-添加用户的姓名和电子邮件产品meta到电子邮件的主体-尽管仍无法使它向每个电子邮件地址发送唯一的电子邮件(电子邮件地址来自此meta):

I found a solution for the first half of my question - adding the user's names and emails from the product meta to the body of the email - though still have not been able to get it to send a unique email to each email address (email addresses are coming from this meta):

我将自定义电子邮件模板(这只是电子邮件模板,而不是用于创建/发送电子邮件的自定义函数)更新为以下内容:

I updated my custom email template (this is only the email template, not the custom function that creates/sends the email) to the following:

<?php
$student_emails = array();
// Loop though  Order IDs
foreach( $order->get_items() as $item_id => $item_data ){
    // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - 1', true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - 1', true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - 1', true );

        print_r('Name: ' . $full_name . ', Login Email: ');
        print_r($student_email . '<br/>');

}

?>

这将在电子邮件正文中打印各种用户及其电子邮件。

This prints the various users and their emails in the email body.

这篇关于Woocommerce电子邮件-根据产品数量发送多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆