1
votes

In Woocommerce, I have a Course product that uses WC Fields to collect student[s] names and email addresses, and custom email template that sends an email when this course product is purchased. As of now, based on the answer in this thread, I am able to collect the email addresses added to those custom fields and send one email to each student as a BCC recipient.

I am now trying to have the emails include the student's name and email address in the body of the email, but I do not want all of the names to appear in the same email. For example, if Student A ([email protected]) and Student B ([email protected]) are both enrolled in the same purchase, the email received by Student A should just contain in the body something like "Dear Student A, Thank you for enrolling. Use your email address [email protected] to login." and the email received by Student B should say the same but with Student B's info, and they should not see the other one's.

So I would have to send multiple emails, based on the number of students being enrolled in that purchase (which is set in the order meta, added from the purchased product's meta).

I tried adding a while() outside of the for() to continue checking through the items until it had gone through all of the items, and sending again for each time it found, but I think the foreach($items as $item) ends up starting through the first item again, since that sends two emails to only the first recipient.

***UPDATED**** I changed the custom email code (which I've stored in my plugins) to:

    $order = wc_get_order( $order_id );
            $items = $order->get_items(); 
            $course_ordered = false;
            $cqt = 1;
            $emailnum = 0;
        foreach ( $items as $item ) {

            $product_id = $item['product_id'];

            if ( $item['product_id']== 1164 ) {
                $course_ordered = true;
                $emailnum++;
                continue;
            }

        }

    if ( ! $course_ordered )
        return;
    while(  $cqt <= $emailnum ){
        // replace variables in the subject/headings
        $this->find[] = '{order_date}';
        $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );
        $this->find[] = '{order_number}';
        $this->replace[] = $this->object->get_order_number();
        if ( ! $this->is_enabled() || ! $this->get_recipient() )
            return;
        // woohoo, send the email!
        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
        $cqt++;
    }

And the code in my functions.php file, which was from this answer, is:

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++;
    }*/
        // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - 1', true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - 1', true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - 1', 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


}

// 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
    $headers .= 'Cc: ' . implode(',', $student_emails) . "\r\n";
}
return $headers;
}

(If the section that is commented out is uncommented, it only sends to the first recipient, and not the second, if it is entered as two separate products, if I remove the while() and use the part below it instead, it sends one email to all recipients) It sends to all recipients (i.e. gets all of the emails from all of the custom fields and uses those as CC or BCC, however I set it) twice, ie sends the same email twice to everyone. How can I get it to send twice but each time to only one of the recipients? Thank you for any suggestions!

1

1 Answers

0
votes

I found a solution for the first half of my question - adding the user's names and emails from the product meta to the body of the email - though still have not been able to get it to send a unique email to each email address (email addresses are coming from this meta):

I updated my custom email template (this is only the email template, not the custom function that creates/sends the email) to the following:

<?php
$student_emails = array();
// Loop though  Order IDs
foreach( $order->get_items() as $item_id => $item_data ){
    // Get the student full Name
        $full_name     = wc_get_order_item_meta( $item_id, 'First Name - 1', true );
        $full_name    .= ' ' . wc_get_order_item_meta( $item_id, 'Last Name - 1', true );
        // Get the student email
        $student_email = wc_get_order_item_meta( $item_id, 'Student Email - 1', true );

        print_r('Name: ' . $full_name . ', Login Email: ');
        print_r($student_email . '<br/>');

}

?>

This prints the various users and their emails in the email body.