Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

php - Adding custom emails to BCC for specific Woocommerce email notifications

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?

The row output is:

"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]"}]

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

I realize that it can be split in two questions:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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).


Related: How to get order ID in woocommerce_email_headers hook


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...