Php 将自定义电子邮件添加到密件抄送以获得特定的电子商务电子邮件通知

Php 将自定义电子邮件添加到密件抄送以获得特定的电子商务电子邮件通知,php,woocommerce,custom-fields,email-notifications,bcc,Php,Woocommerce,Custom Fields,Email Notifications,Bcc,在Woocommerce中,我有一个定制的电子邮件模板(id='wc\U课程\订单'),在购买特定产品(在线课程)时发送 下面我使用一个钩子函数,该函数根据自定义字段(即“学生电子邮件”)中的订单元数据添加收件人。这是基于 我如何才能将这些收件人添加为密件抄送,并抓取他们的“名字”字段并将其添加到电子邮件正文中,特别是考虑到两个数量的课程产品可能与两个不同的学生姓名/电子邮件一起购买 行输出为: "meta_data": [{"id": 652, "key": "First Name -

在Woocommerce中,我有一个定制的电子邮件模板(id='wc\U课程\订单'),在购买特定产品(在线课程)时发送

下面我使用一个钩子函数,该函数根据自定义字段(即“学生电子邮件”)中的订单元数据添加收件人。这是基于

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

行输出为:

"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"}]
然后,对于在同一次购买中注册的其他学生,输出将是“key”:“First Name-2”、“value”:“Jane”和“…-3”等

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

  • 除了电子邮件(我已经抓取了,请参见下面的完整功能),我如何抓取他们的姓名
  • 如何将他们的电子邮件地址添加为密件抄送,而不是将$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;
    }
    
    add_filter('woocommerce_email_receiver_wc_course_order'、'student_email_notification',10,2);
    功能学生电子邮件通知($recipient,$order){
    $student_电子邮件=数组();
    $enroll\u num=0;
    //循环顺序ID
    foreach($order->get\u items()作为$item\u id=>$item\u数据){
    $course\u quantity=$item\u data->get\u quantity();
    $q=1;
    而($q 0){
    //删除重复项(如果有)
    $student\u emails=array\u unique($student\u emails);
    //将电子邮件添加到现有收件人
    $recipient.=','.内爆(',',$student_电子邮件);
    }
    返回$recipient;
    }
    

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

    因为“wc\u course\u 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;
    }
    
    add_filter('woocommerce_email_headers','student_email_notification',20,3);
    函数学生电子邮件通知($header、$email\u id、$order){
    //仅适用于“wc\U课程订单”通知
    如果('wc\u课程\u订单'!=$email\u id)返回$header;
    $student_电子邮件=数组();
    $enroll\u num=0;
    //循环顺序ID
    foreach($order->get\u items()作为$item\u id=>$item\u数据){
    $course\u quantity=$item\u data->get\u quantity();
    $q=1;
    而($q 0){
    //删除重复项(如果有)
    $student\u emails=array\u unique($student\u emails);
    //将电子邮件添加到现有收件人
    $header.=“密件抄送:”。内爆(“,”,$student_电子邮件)。“\r\n”;
    }
    返回$header;
    }
    
    代码放在活动子主题(或活动主题)的function.php文件中。已测试且有效
    (我无法100%测试您的代码,但我希望它能正常工作)



    相关信息:

    一旦我确保在“收件人”字段中也有电子邮件地址(如果我只在密件抄送中有,就不会发送)。但是否还有一种方法可以将
    $full\u name
    发送到电子邮件模板,以便在电子邮件正文中使用,例如“亲爱的
    $full\u name
    ”?我使用

    获取并打印买方名称。@calwex718 Woocommerce默认情况下向客户发送电子邮件(“收件人”)…您必须删除其他相关代码并仅使用此应答代码…这样它将按预期工作…是否有方法将这些电子邮件地址添加到电子邮件正文中?我有一个模板,并尝试在我的电子邮件模板中使用$get_recipient和$recipient,但没有传递它们。我在functions.php和cu中使用此函数stom电子邮件插件,用于在输入特定课程产品时生成电子邮件。(我还提出了另一个问题,不过这仍然是这个问题的后续:)