基于WooCommerce电子邮件通知中出售的产品的不同收件人

时间:2019-04-06 06:22:38

标签: php wordpress woocommerce product email-notifications

我希望在WooCommerce上为不同的企业出售许多虚拟物品。因此,当客户结帐后,我希望将电子邮件发送给相关企业(而不是管理员)。

因此,当销售产品A时,电子邮件将发送到a@email.com。产品B出售后,电子邮件将发送到b@email.com。出售产品C后,电子邮件将发送到c@email.com ...,依此类推。

是否可以将一些代码添加到functions.php中以实现此目的?

1 个答案:

答案 0 :(得分:0)

基于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文件中。经过测试,可以正常工作。