Search code examples
phpwordpresswoocommerceuser-rolesemail-notifications

Send Woocommerce new order email notification from specific user role to specific user


We have a "main wholesaler" who is in charge of other wholesalers. This wholesaler is assigned to user role: wholesale_customer just as the other wholesalers.
This "main wholesaler" places orders for his stores, and the other wholesalers place orders for their own stores as normal.

I would like that the "main wholesaler" receive an email notification when the other wholesalers place their orders, so he will get notified when other wholesalers are ordering because he gets a commission.

So is there a way that we can make the "main wholesaler" to receive an email notification when the other wholesalers place their orders?

Maybe this can be done with a function somewhat like this: WooCommerce email notifications: different email recipient for different cities

Any help is appreciated.


Solution

  • Updated (solved a bug in backend email settings)

    The following function will send an additional "new order" email notidfication to the "Wholesale manager" when others wholesale_customer user roles will make a purchase.

    You will just need to set in this function the User ID for the "Wholesale manager"…

    add_filter( 'woocommerce_email_recipient_new_order', 'wholesale_manager_email_recipient', 20, 2 );
    function wholesale_manager_email_recipient( $recipient, $order ) {
        if( is_admin() ) return $recipient;
    
        // Only for 'wholesale_customer' user role
        if( is_admin() || ! user_can( $order->get_user_id(), 'wholesale_customer' ) )
            return $recipient; // Exit if not a 'wholesale_customer'
    
        // HERE Set the main wholesale manager user ID
        $manager_id    = 5;
    
        $manager_user  = new WP_User( $manager_id );
        $manager_email = $manager_user->user_email;
    
        // Add manager email when other 'wholesale_customer' place orders
        if ( $order->get_user_id() != $manager_id )
            $recipient .= ',' . $manager_email; // coma separate emails
    
        return $recipient;
    }
    

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