我想在 WooCommerce 上为不同的企业销售一些虚拟元素。因此,当客户结账时,我希望将电子邮件发送给相关业务(而不是管理员)。
因此,当产品 A 售出时,电子邮件将发送至 a@email.com。当产品 B 售出时,电子邮件将发送至 b@email.com。当产品C售出后,邮件将发送至c@email.com...等等。
我可以在 functions.php 中添加一些代码来实现这一点吗?
最佳答案
基于 "Different recipients based on product category in WooCommerce email notification " 答案代码,以下将允许根据产品 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 文件。经过测试并有效。
关于php - 基于 WooCommerce 电子邮件通知中销售的产品的不同收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546448/