Send New Order Emails to a Supplier by Category
Adds an extra recipient to the "New order" email when the order contains products from a category, so suppliers or warehouse teams only get the orders they fulfil.
Opens WPIDE on
Code
$wpide_category_recipients = array( // Category slug => email address.
'furniture' => '[email protected]',
'prints' => '[email protected]',
);
add_filter( 'woocommerce_email_recipient_new_order', function ( $recipient, $order ) use ( $wpide_category_recipients ) {
if ( ! $order instanceof WC_Order ) {
return $recipient;
}
$extra = array();
foreach ( $order->get_items() as $item ) {
foreach ( $wpide_category_recipients as $slug => $email ) {
if ( has_term( $slug, 'product_cat', $item->get_product_id() ) && is_email( $email ) ) {
$extra[] = $email;
}
}
}
return $extra ? implode( ',', array_unique( array_merge( array_filter( array_map( 'trim', explode( ',', (string) $recipient ) ) ), $extra ) ) ) : $recipient;
}, 10, 2 );
Notes
Map category slugs to email addresses; the usual recipients still receive every new order email.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.