为特定的Woocommerce电子邮件通知向BCC添加自定义电子邮件

calwex718

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

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

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

该行的输出为:

"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": "[email protected]"}]

然后,对于在同一购买中注册的其他学生,输出将为“键”:“名字-2”,“值”:“简”和“ ...-3”等。

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

  1. 除了电子邮件(我已经抓过,请参见下面的全部功能)之外,如何抓取他们的名字
  2. 如何将他们的电子邮件地址添加为密件抄送,而不是将$ recipients附加为常规的“ TO:”?

我正在使用的完整功能:

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完成吗?还是应该在我拥有的单独电子邮件模板文件中完成?

LoicTheAztec

由于“ wc_course_order”是一个自定义电子邮件通知ID,因此我无法对其进行真正的测试(因此,出于测试目的,我在自己测试该功能时已对其进行了评论)…

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

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

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%地测试您的代码,但是我希望它应该可以工作)


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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

仅对WooCommerce管理员电子邮件通知显示自定义字段

在WooCommerce中自定义客户处理电子邮件通知

Woocommerce-自定义电子邮件类创建重复的电子邮件

根据运输方式自定义Woocommerce新订单电子邮件通知

Woocommerce新订单电子邮件通知中的自定义“回复”电子邮件标题

在Woocommerce管理员电子邮件通知中显示产品自定义字段值

在WooCommerce中自定义订单状态更改时发送电子邮件通知

在Woocommerce中的特定电子邮件通知上为特定产品添加自定义文本

根据Woocommerce电子邮件通知中的送货方式显示自定义内容

根据Woocommerce中的用户角色自定义页脚文本电子邮件通知

在Woocommerce中销售产品时向卖家发送自定义电子邮件

Laravel自定义电子邮件通知表

在WooCommerce订单和电子邮件通知中显示自定义字段值

产品标签的WooCommerce自定义电子邮件

通过添加自定义表来自定义WooCommerce电子邮件

订单自定义字段未显示在WooCommerce电子邮件通知中

在特定的WooCommerce电子邮件通知上显示自定义字段

在WooCommerce电子邮件通知上显示第二个自定义字段

在WooCommerce电子邮件通知上显示自定义产品图片

在WooCommerce电子邮件通知中为管理员显示自定义字段

在WooCommerce订单和电子邮件通知上显示自定义交货通知

WooCommerce:使用自定义电子邮件通知添加自定义订单状态

自定义电子邮件通知到不同的电子邮件ID Magento

Woocommerce的自定义电子邮件(Wordpress)

Woocommerce自定义电子邮件结帐

WooCommerce-每个产品的自定义电子邮件

将自定义文本添加到本地取货 Woocommerce 订单的特定电子邮件通知

在 WooCommerce 客户处理订单电子邮件通知中根据付款方式添加自定义消息

根据 WooCommerce 电子邮件通知中的产品类别添加自定义文本“每个项目”