I have this function that sends an additional email to different email adresses based on which product that is purchased. It works well but it still sends an New order email to the default adress given in Woo. Is there a way to prevent this, so that the new order mail only ends up at the adresses given in the function below?
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(
'[email protected]' => array(23),
'[email protected]' => array(24),
'[email protected]' => 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;
}
Use the following revised code version, avoiding sending an email to the default recipient when there is at least a matching product (custom recipient):
Code goes in functions.php file of your child theme (or in a plugin). It should work.
Related: Different recipients based on products sold in WooCommerce email notification