如何将基于位置的订单分配给 WordPress (WooCommerce) 上的特定用户?

How to assign location based orders to specific users on WordPress (WooCommerce)?

我有一个 WooCommerce 商店,它通过 Woo API 连接到 ASP.NET MVC。所有订单处理都是通过 ASP.NET 网站完成的,我使用 Woo 仅用于管理订阅。

大约有几百个 subscriptions/orders 我在想是否有一种方法可以根据 Address/City/Community 等对订单(以及所有其他数据)进行分组

我尝试了一些插件,包括用于将公司分配给每个用户的高级自定义字段插件(附加字段,例如公司名称)和用于添加分配给用户的组的用户组插件(基于公司的组用户)这让我可以将公司分配给用户,然后在 Woo Orders 页面上我可以 select 一家公司来过滤与该公司相关的所有订单。

但我真正需要的是一种在用户登录时执行此操作的方法,因此当管理员用户 A 登录时,他们只会在各自的组 A 中看到 orders/subscriptions/customers。这可以通过插件完成,还是我需要添加某种定制,它会登录到管理员组中,然后使用该值仅为该组获取数据?请告知任何可能的选择。

我认为您必须向用户 table 添加三个字段,它们将包含地址、城市和社区。或者如果你很难使用更大面积的标签。

由于订单与具有客户 ID 的客户相关联,您可以在数据库中创建一个视图来显示订单和位置信息,以创建一个 php 文件模板名称 page-myorders.php 和标题为 myorders 的页面,然后可以检索您询问的信息。该页面将使用视图查询数据库。如果您使用标签,则需要将标签作为参数传递给 wp_query:

显示不具有标签 ID 37 和 47 中任何一个的帖子:

$query = new WP_Query( array( 'tag__not_in' => array( 37, 47 ) ) );

https://codex.wordpress.org/Class_Reference/WP_Query

编码愉快

根据我的要求,目标是显示基于特定位置的订单(为与特定区域相关的服务下达的订单,例如位置 A)。现在,我需要一个管理员用户 A 来查看 orders/subscriptions 位属于位置 A 的客户。我确实想阻止管理员用户 A 查看客户放置的属于位置 B 的 orders/subscriptions。 Admin User-B 等也是如此。

所以我将以下自定义添加到我的活动主题 functions.php 并且我的问题得到了解决。

function company_filter_orders($query) {
global $post_type;
$customers = array();
if($post_type === 'shop_order' || $post_type === 'shop_subscription')
{
    $curr_user_id = get_current_user_id();
    // the value is 0 if the user isn't logged-in
    if ( $curr_user_id != 0 ) {
        // we know now the user is logged-in, so we can use his/her ID to get the user meta
        $um_value = get_user_meta( $curr_user_id, 'company', true );
        // now we check if the value is correct
        if ( ! empty( $um_value ) ){
            // now we get all the users whose company matches meta_value

            $members = get_users(array(
                    'meta_key' => 'company', 
                    'meta_value' => $um_value,
                )
            );
            //build an array of company users/customers only
            foreach( $members as $usr ) {
                $customers[] = $usr->ID;
            }
        }
    }

    if(count($customers) > 0)
    {   //set meta query to match customer id with above setup array of customers
        $query->set( 'meta_query', array(        
            array(
                  'key' => '_customer_user',
                  'value' => $customers, //pass array of customers to fetch orders/subscriptions for those customers only
                  'compare' => 'IN',
                  'type' => 'numeric'
            )
        ));
    }
}

return $query;
}
add_filter('pre_get_posts', 'company_filter_orders');

我希望这能帮助在 WordPress woocommerce 中遇到同样问题的人。