简体   繁体   English

向相关经理发送 WooCommerce 新订单 email 通知

[英]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).有2种角色,1种下订单(客户),1种处理订单(经理)。
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.我已经到了经理收到新订单 email 的地步,但是,他们多次收到邮件。 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.帮助将不胜感激,因为它已经困扰了我好几天了。

First you can't get the current user ID on email notifications, as it's a background process.首先,您无法在 email 通知上获取当前用户 ID,因为它是一个后台进程。 What you can get is the customer ID that belongs to the order using the WC_Order method get_customer_id() .您可以使用WC_Order方法get_customer_id()获得属于订单的客户 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"我想“经理”是一个自定义用户角色,因为 WooCommerce 使用“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).代码进入您的活动子主题(或活动主题)的functions.php 文件。 It should works (untested).它应该可以工作(未经测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce 中新订单电子邮件通知的自定义主题 - Custom subject for New Order Email notification in WooCommerce 在 Woocommerce 中手动发送新订单电子邮件 - Manually Sending A New Order Email In Woocommerce 更改WooCommerce订单状态更改而无需发送电子邮件通知 - Change WooCommerce order status change without sending email notification 将订单总重量添加到WooCommerce新订单电子邮件通知中 - Add the order total weight to WooCommerce new order email notification 将订单总重量(吨)转换为 WooCommerce 新订单 email 通知 - Convert the order total weight in tons to WooCommerce new order email notification Woocommerce:如何调试与新订单 email 相关的问题? - Woocommerce: How to debug problem related to new order email? 允许在 WooCommerce 5+ 中重新发送新订单通知 - Allow re-sending New Order Notification in WooCommerce 5+ Woocommerce 向管理员发送“暂停”订单的新订单电子邮件通知 - Woocommerce new order email notification to admin for "On Hold" orders 如何在新订单 WooCommerce 电子邮件通知中将“产品”更改为“服务” - How to change “Product” to “Service” in new order WooCommerce email notification Woocommerce将新订单通知发送到不同的电子邮件(基于运送国家) - Woocommerce new order notification to different email based on shipping country
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM