Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.