admin管理员组文章数量:1394123
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..
本文标签: woocommerce offtopicSend Email to Custom Field in Custom Post Type when order is Completed
版权声明:本文标题:woocommerce offtopic - Send Email to Custom Field in Custom Post Type when order is Completed 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744588953a2614364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论