【发布时间】:2018-04-19 00:33:16
【问题描述】:
在 Woocommerce 中,我想将带有高级自定义字段插件的电子邮件自定义字段添加到产品帖子类型。
如果客户下订单,我想将每个订单项目的相应电子邮件地址添加到新订单电子邮件通知中。
如何将产品自定义字段中的收件人添加到 Woocommerce 新订单电子邮件通知?
【问题讨论】:
标签: php wordpress woocommerce advanced-custom-fields email-notifications
在 Woocommerce 中,我想将带有高级自定义字段插件的电子邮件自定义字段添加到产品帖子类型。
如果客户下订单,我想将每个订单项目的相应电子邮件地址添加到新订单电子邮件通知中。
如何将产品自定义字段中的收件人添加到 Woocommerce 新订单电子邮件通知?
【问题讨论】:
标签: php wordpress woocommerce advanced-custom-fields email-notifications
对于“新订单”电子邮件通知,这是从订单中的项目添加自定义电子邮件的方法(电子邮件自定义字段在产品中使用 ACF 设置)。
因此,您将首先在 ACF 中为“产品”帖子类型设置一个自定义字段:
然后您将在后端产品编辑页面:
完成后,当该产品自定义字段全部设置为电子邮件地址时,您将使用此代码将订单中每个相应项目的电子邮件添加到“新订单”通知中:
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;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。
相关:Send Woocommerce Order to email address listed on product page
【讨论】: