如何将来自get_users的匹配客户的集合存储到WordPress中的数组中?

时间:2018-10-03 10:47:25

标签: asp.net-mvc wordpress woocommerce-rest-api

就我而言,我必须使管理员用户能够查看属于特定位置的客户的订单和订阅。我的主要目标是允许管理员用户A(属于特定位置,例如位置A)查看从该特定位置(即位置A)发送了订单/订阅的客户的订单/订阅。我使用了一个自定义字段,并在用户端将其命名为Company,以将基于位置的公司分配给Admin Users。

客户根据其地理位置发送服务的订单/订阅,并将其订单/订阅分配到特定分支(在此情况下为“管理员用户”),并具有管理该特定位置的订单/订阅的权限。没有其他管理员用户可以查看其他位置的订单/订阅。也就是说,位置A订单仅对用户A可见,而位置B订单仅对用户B可见。管理员可以查看所有订单。我已经使用了以下代码,并且运行正常。

    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
            //echo 'user meta has the 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如何将管理员用户保存在数据库中的多个公司/位置。如果要另存为JSON数组,那么我如何从两个不同的位置/公司中选择订单/订阅并将其显示给登录用户。我尝试了不同的技术,但对我而言无济于事。请指教。

0 个答案:

没有答案