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 <[email protected]>' . '\r\n'; // del if not needed
}
return $headers;
}
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 <[email protected]>' . '\r\n'; // del if not needed
}
return $headers;
}
Share
Improve this question
edited Apr 7, 2020 at 14:57
DEM
asked Apr 6, 2020 at 20:47
DEMDEM
1811 silver badge9 bronze badges
5
- you go from double quotes to single at $headers, There is the mistake – 7uc1f3r Commented Apr 6, 2020 at 21:36
- I tried that, but still no go. – DEM Commented Apr 7, 2020 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 Commented Apr 7, 2020 at 16:27
- yes i see that now, but regardless shouldn't it pass the last product variable which is an email – DEM Commented Apr 7, 2020 at 17:55
- Go through your code step by step, rebuild this step by step and test every intermediate step, see my answer – 7uc1f3r Commented Apr 7, 2020 at 18:12
1 Answer
Reset to default 0How 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 = '[email protected]';
$headers .= 'Cc: Name <'.$partnerEmail.'>' . '\r\n'; // del if not needed
$headers .= 'Bcc: Name <[email protected]>' . '\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 <[email protected]>' . '\r\n'; // del if not needed
return $headers;
}
Step 3, etc..