Skip to content
Advertisement

Sending WooCommerce new order email notification to related managers

So, the website is set up something like this:
There are 2 types of roles, 1 who place orders (client), 1 who process orders (manager).
These can be divided into groups, each with their own clients and managers (multiple of each possible).
The managers need to get a notification of new orders within their group and to cover for holidays and sick days, every manager gets every order within their group.
I’ve gotten to the point where the managers get the new order email, however, they get the mail multiple times. The amount of duplicate mails sent is the same as the amount of emails added to the recipient list and I can’t, for the life of me, figure out why.

Code:

// Send mail to Manager on new order
add_filter('woocommerce_email_recipient_new_order', 'my_new_order_email_recipient', 10, 2);
function my_new_order_email_recipient($recipient, $order) {
    $find_manager_args = array(
        'role' => 'manager',
    );

    $find_manager_query = new WP_User_Query($find_manager_args);

    $users = $find_manager_query->get_results();

    $new_recipient = '';

    if (!empty($users)) {
        foreach ($users as $user1) {
            if (get_user_meta(get_current_user_id(), 'group_meta_key', TRUE) === get_user_meta($user1->id, 'group_meta_key', TRUE)) {
                if (isset($new_recipient) && !empty($new_recipient)) {
                    $new_recipient = "$new_recipient,";
                }
                $user_info = get_userdata($user1->ID);
                $new_recipient .= $user_info->user_email;
            }
        }
    }
    else {
        $new_recipient = get_option('admin_email');
    }

    return $new_recipient;
}

Everything I find is the exact same as what I have. Comma separated list of addresses should work. There’s no payment system, so no external triggers. Tested by adding and removing managers from a group and the amount of duplicates changes with that.

Help would be appreciated as it’s stumped me for days now.

Advertisement

Answer

First you can’t get the current user ID on email notifications, as it’s a background process. What you can get is the customer ID that belongs to the order using the WC_Order method get_customer_id().

Now you get duplicated emails because there are some mistakes in your code, that can be simplified.

I suppose that “manager” is a custom user role as WooCommerce uses “shop_manager”

So try the following instead:

// Send mail to Manager on new order
add_filter('woocommerce_email_recipient_new_order', 'my_new_order_email_recipient', 10, 2);
function my_new_order_email_recipient( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) 
        return $recipient;

    $customer_id    = $order->get_customer_id();
    $customer_group = get_user_meta( $customer_id, 'group_meta_key', true );
    $manager_emails = [];

    // Get an array of WP_User from "manager" user role
    $users = get_users(['role' => 'manager']);

    if ( count($users) > 0 ) {
        foreach ( $users as $user ) {
            if ( $customer_group === get_user_meta( $user->ID, 'group_meta_key', true ) ) {
                $manager_emails[] = $user->data->user_email;
            }
        }
        if( count($manager_emails) > 0 ) {
            $recipient = implode(',', $manager_emails);
        }
    }
    return $recipient;
}

Code goes in functions.php file of your active child theme (or active theme). It should works (untested).

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement