Send Email to Custom Field in Custom Post Type when order is Completed


So I have this function that should send the completed order email to a custom email address via a CC. If the function only contains the if statement (see below) it works correctly. However, when I add other custom code, the email is not triggered.

I have a Custom Post Type that stores the required email address.

The CC email is grabbed from an ACF Post Object field located in the product meta. From that ACF post object field, I grab the custom field (partner_email).

I know that $partnerEmail is valid because I see it logged in the error logs — however, when I input that variable into the headers, it does not work. Any help would be greatly appreciated. Thanks in advance.

add_filter( 'woocommerce_email_headers', 'enyc_order_completed_email_add_cc_bcc', 9999, 3 );

function enyc_order_completed_email_add_cc_bcc( $headers, $email_id, $order ) {

$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;  

$order = new WC_Order( $order_id );

$items = $order->get_items();

foreach ( $items as $item ) {

    //Get Product ID
    $product_id = $item['product_id'];

    //Get Product Data
    $product = wc_get_product( $product_id );

    //Get Partner Data from Product
    $partner = get_field( 'venue', $product_id);

    //Get Partner ID
    $partnerID = $partner->ID;

    //Get Partner Email
    $partnerEmail = get_field( 'partner_email', $partnerID);

    //error_log( $partnerEmail);


 if ( 'customer_completed_order' == $email_id ) {
        $headers .= "Cc: Name <'.$partnerEmail.'>" . "rn"; // del if not needed
        $headers .= "Bcc: Name <>" . "rn"; // del if not needed

    return $headers;
DEM 3 years 2020-04-06T16:52:26-05:00 0 Answers 86 views 0

Leave an answer