基于 WooCommerce 电子邮件通知中销售的产品的不同收件人 [英] Different recipients based on products sold in WooCommerce email notification
问题描述
我希望在 WooCommerce 上为不同的企业销售一些虚拟物品.因此,当客户结帐时,我希望将电子邮件发送给相关企业(而不是管理员).
I’m looking to sell a number of virtual items on WooCommerce for different businesses. So when the customer has checked out, I'd like the email to be sent to the relevant business (not to admin).
因此,当产品 A 售出时,电子邮件将发送至 a@email.com.产品 B 售出后,电子邮件将发送至 b@email.com.当产品 C 售出时,电子邮件将发送至 c@email.com...等等.
So when Product A is sold, an email will go to a@email.com. When Product B is sold, the email will be sent to b@email.com. When Product C is sold, the email will be sent to c@email.com...and so forth.
是否有一些代码可以添加到functions.php 中以实现此目的?
Is there some code I can add to functions.php to achieve this?
推荐答案
基于"根据 WooCommerce 电子邮件通知中的产品类别不同的收件人" 答案代码,以下将允许根据产品 ID:
Based on "Different recipients based on product category in WooCommerce email notification" answer code, the following will allow a add a different email recipient based on the product Id:
add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
function custom_email_recipient_new_order( $recipient, $order ) {
// Not in backend when using $order (avoiding an error)
if( ! is_a($order, 'WC_Order') ) return $recipient;
// Define the email recipients / product Ids pairs
$recipients_product_ids = array(
'product.one@email.com' => array(37),
'product.two@email.com' => array(40),
'product.three@email.com' => array(53, 57),
);
// Loop through order items
foreach ( $order->get_items() as $item ) {
// Loop through defined product categories
foreach ( $recipients_product_ids as $email => $product_ids ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
if( array_intersect([$product_id, $variation_id], $product_ids) && strpos($recipient, $email) === false ) {
$recipient .= ',' . $email;
}
}
}
return $recipient;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于基于 WooCommerce 电子邮件通知中销售的产品的不同收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!