将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中 [英] Add recipients from product custom fields to Woocommerce new order email notification
问题描述
在Woocommerce中,我想将带有高级自定义字段插件的电子邮件自定义字段添加到产品发布类型。
如果客户下订单,我想将每个订单项的相应电子邮件地址添加到新订单电子邮件通知中。
如何将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中?
对于新订单电子邮件通知,这是从订单项中添加自定义电子邮件的方法(电子邮件自定义字段在产品中设置了ACF)。
因此,您将首先在ACF中为产品帖子类型设置一个自定义字段:
然后您将在后端产品编辑页面中找到
In Woocommerce, I would like to add a email custom field with the Advanced Custom Fields plugin to products post type.
If customer place an order I would like to add the corresponding email adresses for each order items to new order email notification .
How to add recipients from product custom fields to Woocommerce new order email notification?
For "New order" email notification, here is the way to add custom emails from items in the order (The email custom field is set with ACF in the products).
So you will set first a custom field in ACF for "product" post type:
Then you will have in backend product edit pages:
Once done and when that product custom-fields will be all set with an email adress, you will use this code that will add to "New order" notification the emails for each corresponding item in the order:
add_filter( 'woocommerce_email_recipient_new_order', 'add_item_email_to_recipient', 10, 2 );
function add_item_email_to_recipient( $recipient, $order ) {
if( is_admin() ) return $recipient;
$emails = array();
// Loop though Order IDs
foreach( $order->get_items() as $item_id => $item ){
// Get the student email
$email = get_field( 'product_email', $item->get_product_id() );
if( ! empty($email) )
$emails[] = $email; // Add email to the array
}
// If any student email exist we add it
if( count($emails) > 0 ){
// Remove duplicates (if there is any)
$emails = array_unique($emails);
// Add the emails to existing recipients
$recipient .= ',' . implode( ',', $emails );
}
return $recipient;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related: Send Woocommerce Order to email address listed on product page
这篇关于将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!