0

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.'>' . '\r\n'; // del if not needed
    $headers .= 'Bcc: Name <dave@eternal.nyc>' . '\r\n'; // del if not needed
    }   


    return $headers;
}
5
  • you go from double quotes to single at $headers, There is the mistake
    – 7uc1f3r
    Apr 6 '20 at 21:36
  • I tried that, but still no go.
    – DEM
    Apr 7 '20 at 14:58
  • Okay, maybe this? do you know that in your foreach loop, your variables are overwritten with each loop? so that they are only set from the last item (product) in the last loop?
    – 7uc1f3r
    Apr 7 '20 at 16:27
  • yes i see that now, but regardless shouldn't it pass the last product variable which is an email
    – DEM
    Apr 7 '20 at 17:55
  • Go through your code step by step, rebuild this step by step and test every intermediate step, see my answer
    – 7uc1f3r
    Apr 7 '20 at 18:12
0

How to debug

Step 1

add_filter( 'woocommerce_email_headers', 'enyc_order_completed_email_add_cc_bcc', 10, 3 );
function enyc_order_completed_email_add_cc_bcc( $headers, $email_id, $order ) { 
    // CHANGE EMAIL
    $partnerEmail = 'some@email.com';

    $headers .= 'Cc: Name <'.$partnerEmail.'>' . '\r\n'; // del if not needed
    $headers .= 'Bcc: Name <dave@eternal.nyc>' . '\r\n'; // del if not needed

    return $headers;
}

Step 1 works? -> step 2

add_filter( 'woocommerce_email_headers', 'enyc_order_completed_email_add_cc_bcc', 10, 3 );
function enyc_order_completed_email_add_cc_bcc( $headers, $email_id, $order ) {
    // CHANGE ID
    $product_id = 100;

    //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);

    $headers .= 'Cc: Name <'.$partnerEmail.'>' . '\r\n'; // del if not needed
    $headers .= 'Bcc: Name <dave@eternal.nyc>' . '\r\n'; // del if not needed

    return $headers;
}

Step 3, etc..

1
  • yes, i did those processes initally to get the email variable. the only thing that seems to not happen is getting it into the CC field
    – DEM
    Apr 9 '20 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.