为特定的Woocommerce电子邮件通知向BCC添加自定义电子邮件 [英] Adding custom emails to BCC for specific Woocommerce email notifications

查看:174
本文介绍了为特定的Woocommerce电子邮件通知向BCC添加自定义电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我有一个自定义电子邮件模板(id ='wc_course_order'),该模板在购买特定产品(在线课程)时发送.

In Woocommerce, I have a custom email template (id = 'wc_course_order') that sends when a specific product (an online course) is purchased.

下面,我使用一个挂钩函数,该函数基于来自自定义字段(即学生电子邮件")的订单元数据添加收件人.它基于此主题答案.

Below I use a hooked function that adds recipients based on order metadata from custom fields (i.e. "Student Email"). It's based on this thread answer.

我如何才能将这些收件人添加为密件抄送,并抓住他们的名字"字段并将其添加到电子邮件的正文中,尤其是考虑到可以同时购买两门课程产品和两个不同的学生姓名/电子邮件?

How can I add these recipients as BCC and grab their "First Name" field and add that to the body of the email, especially given that two quantities of the course product may be purchased together with two different student names/emails?

行输出为:

"meta_data": [{"id": 652,
   "key": "First Name - 1",
    "value": "John"},
    {"id": 653,
    "key": "Last Name - 1",
    "value": "Doe"},
    {"id": 654,
    "key": "Student Email - 1",
    "value": "johndoe@example.com"}]

然后对于在同一笔交易中注册的其他学生,输出将为键":名字-2",值":简"和"...-3"等.

And then for an additional student registered in the same purchase, output would be "key": "First Name - 2", "value": "Jane", and "... - 3" etc.

我意识到它可以分为两个问题:

  1. 除了电子邮件(我已经抓过,请参阅下面的全部功能)之外,我如何我抓起他们的名字?
  2. 如何将其电子邮件地址添加为密件抄送,而不是将$收件人添加为常规的收件人:"?
  1. How do I grab their names in addition to the emails (which I already grabbed, see full function below)?
  2. How do I add their email addresses as BCC instead of appending the $recipients as regular "TO:"?

我正在使用的完整功能:

The full function that I am using:

add_filter( 'woocommerce_email_recipient_wc_course_order', 'student_email_notification', 10, 2 );
function student_email_notification( $recipient, $order) {
    $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){
            // Get the student email
            $enroll_num++;
            $student_email = wc_get_order_item_meta( $item_id, 'Student Email - '.$enroll_num, true );
            if( ! empty($student_email) )
                $student_emails[] = $student_email; // Add email to the array
            $q++;
        }
    }

    // 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
    $recipient .= ',' . implode( ',', $student_emails );
    }
    return $recipient;
}

这一切可以在functions.php中完成吗?还是应该在我拥有的单独的电子邮件模板文件中完成?

Can this all be done within functions.php or should this be done in the separate email template file I have?

推荐答案

由于'wc_course_order'是一个自定义电子邮件通知ID,因此我无法对其进行真正的测试(因此,出于测试目的,我在测试自己发挥作用)…

As 'wc_course_order' is a custom email notification ID that I can't really test it with it (so for testing purpose I have commented it, when testing the function myself)…

使用与您获取电子邮件相同的方式,我想我在下面的代码中得到了名和姓(但我不确定). > ...

Using the same way than you to get the email, I suppose that I am getting the first and last name in the code below (but I am not absolutely sure)

现在要将此电子邮件添加为密件抄送,您将不得不更改钩子:

Now to add this emails as BCC, you will have to change of hook:

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++;
        }
    }

    // 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
        $header .= 'Bcc: ' . implode(',', $student_emails) . "\r\n";
    }
    return $header;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常使用
(我真的不能100%地测试您的代码,但是我希望它可以正常工作).

Code goes in function.php file of your active child theme (or active theme). Tested and works
(I can't really test your code at 100%, but it should work, I hope).

相关:如何获取woocommerce_email_headers挂钩中的订单ID

这篇关于为特定的Woocommerce电子邮件通知向BCC添加自定义电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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