【问题标题】:Show only local orders in WooCommerce admin page在 WooCommerce 管理页面中仅显示本地订单
【发布时间】:2019-04-08 04:21:35
【问题描述】:

在我的商店中,客户可以从多个地点购买相同的产品。每个位置都有自己的 shop_manager,所以我希望每个 shop_manager 只能看到所选变体与其位置匹配的订单(例如,来自明尼苏达州的“shop_manager_min”只能看到选择了变体“min”的订单;“shop_manager_tex”,来自德克萨斯,只能看到带有“tex”变化的订单等等)。

为此,我尝试了下一个代码:

    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; }}

但是,我没有从这个查询中得到任何回复,没有显示任何订单。我做错了什么?


我尝试了下一个按位置订购订单的查询,它工作得非常完美:

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; }}

与以前的一样,我看到 attribute_pa_location 存在并且可以通过查询访问。我也回显了attribute_pa_location的值和$location中的值,两者完全一样!!!但是,查询没有返回任何内容,并且 shop_manager 管理订单页面中没有显示任何订单。


我使用_customer_user 而不是attribute_pa_sede 执行了相同的第一个查询,并且运行良好。如果查询过程没问题,元键也没问题,为什么我一起用就不行???

【问题讨论】:

    标签: php wordpress function woocommerce


    【解决方案1】:

    我终于发现问题在于属性位置不是订单属性的一部分(而是产品属性的一部分)。作为周转,我在结帐期间使用functions.php中的下一个操作将位置复制到事务ID中:

    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); }
    

    之后,我只是查询:

    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; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 2019-04-14
      • 2021-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多