在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”等。
我意识到可以将其分为两个问题:
我正在使用的完整功能:
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) . "\r\n";
}
return $header;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试并可以工作
(我真的不能100%地测试您的代码,但是我希望它应该可以工作)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句