将自定义电子邮件添加到密件抄送,以获取特定的Woocommerce电子邮件通知



在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": "johndoe@example.com"}]

然后,对于在同一购买中注册的其他学生,输出将是"密钥":"名字 - 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内完成,还是应该在我拥有的单独电子邮件模板文件中完成?

由于"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) . "rn";
}
return $header;
}

代码进入函数.php活动子主题(或活动主题)的文件。经过测试并工作
(我无法真正以 100% 测试您的代码,但我希望它应该有效)。


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

最新更新