Different recipients based on products sold in WooCommerce email notification

只谈情不闲聊 提交于 2019-12-06 09:30:08

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;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!