在Woocommerce中,我想添加一个带有高级自定义字段插件的电子邮件自定义字段到产品帖子类型。
如果客户下订单,我想添加每个订单项目的相应电子邮件地址到新订单电子邮件通知。
如何将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中?
发布于 2018-04-20 01:08:29
对于"New order“电子邮件通知,下面是从订单中的项目添加自定义电子邮件的方法(电子邮件自定义字段在产品中设置为ACF )。
因此,您将首先在ACF中为"product“帖子类型设置一个自定义字段:
然后,您将在后端产品编辑页面中:
完成后,当产品自定义字段都设置了电子邮件地址时,您将使用此代码将订单中每个相应项目的电子邮件添加到"New 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;
}
代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好。
相关:Send Woocommerce Order to email address listed on product page
https://stackoverflow.com/questions/49910924
复制相似问题