dousa1630 2019-03-30 09:56
浏览 63
已采纳

在WooCommerce管理页面中仅显示本地订单

In my shop, customers can buy the same product from several locations. Each location has its own shop_manager, so I want every shop_manager can only see the orders where the selected variation matches their location (e.g. "shop_manager_min", from Minnesota, can only see orders where variation "min" has been selected; "shop_manager_tex", from Texas, can only see orders with "tex" variation and so on).

To do so, I tried the next code:

    add_filter( 'pre_get_posts', 'orders_by_location' );
    function orders_by_location( $query ) {
//check if user role is shop_manager to continue
        if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop_manager'){
//gets the 3 character string code for the shop manager location
            $location = substr(wp_get_current_user()->user_login, -3);
//query for all the orders where the variation matches the shop manager location
            $query->set( 'meta_query', array( 
                          array( 
                                  'key' => 'attribute_pa_location', 
                                  'value' => $location, 
                                  'compare' => '=' 
            ) ) );  
        return $query; }}

However, I got nothing back from this query, no orders are shown. What I am doing wrong?


I have tried the next query to order orders by location, and it works just perfectly:

add_filter( 'pre_get_posts', 'orders_by_location' );
function orders_by_location( $query ) {
    if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop_manager'){
        $query->set('orderby', 'attribute_pa_location' );
        $query->set('order', 'DESC' );
        return $query; }}

With the previous, I see attribute_pa_location exists and it is accessible by the query. I also echoed the attribute_pa_location value and the value in $location, and both are the exactly same!!! However, the query is not giving anything back and no orders are shown in the shop_manager admin order page.


I did the same first query using _customer_user instead of attribute_pa_sede and worked perfectly. If the query procedure is fine and the meta key is also fine, why is not working when I use them together???

  • 写回答

1条回答 默认 最新

  • dqpkea9486 2019-04-07 20:21
    关注

    I finally found that the problem is that the attribute location is not part of the order attributes (but part of the product attributes). As a turnaround, I copied the location into the transaction id during checkout using the next action in functions.php:

    add_action( 'woocommerce_checkout_create_order', 'update_order_id', 30, 2 );
    function update_order_id( $order, $posted_data ) {
        foreach ( $order->get_items() as $item ) {$location = $item->get_meta("pa_location");}
        $order->set_transaction_id($location); }
    

    After that, I just query:

    add_action( 'pre_get_posts', 'orders_by_location', 100 );
    function orders_by_location( $query ) {
        if(isset(wp_get_current_user()->roles[0]) && wp_get_current_user()->roles[0] == 'shop_manager'){
            $location = substr(wp_get_current_user()->user_login, -3);
            $query->set('post_type', 'shop_order' );
            $query->set('post_status', array('wc-on-hold', 'wc-processing') );
            $query->set('meta_key', '_transaction_id' );
            $query->set('meta_value', $location ); } 
        return $query; }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算