【问题标题】:Restrict user role to change only some order statuses in Woocommerce [closed]限制用户角色仅更改 Woocommerce 中的某些订单状态 [关闭]
【发布时间】:2019-02-24 15:14:05
【问题描述】:

我想限制按角色访问 woocommerce 下拉列表中的某些订单状态。我已经在 functions.php 子主题中尝试了 Restrict woocommerce order status by role 上的代码,但无法使其工作并且没有足够的代表发表评论。

https://prnt.sc/mpfl3b 是所显示内容的屏幕截图 - 我希望商店经理(或创建的自定义角色)只能将订单标记为处理中或暂停,而下拉菜单中看不到其他选项。

我们将不胜感激。

【问题讨论】:

  • 请删除这个过于宽泛的内容,因为它不是。这是非常具体的,并且给出了非常具体的答案,这是可行的。这个答案对我非常有帮助,仅仅将这样的东西(不是第一次)标记得太宽泛并不能帮助任何寻找这个答案的人。谢谢。

标签: php wordpress woocommerce hook-woocommerce orders


【解决方案1】:

链接的答案代码不适用于您想要的。请尝试以下方法:

// Admin orders list: bulk order status change dropdown
add_filter( 'bulk_actions-edit-shop_order', 'filter_dropdown_bulk_actions_shop_order', 20, 1 );
function filter_dropdown_bulk_actions_shop_order( $actions ) {
    $new_actions = [];
    foreach( $actions as $key => $option ){
        // Targeting "shop_manager" | order statuses "on-hold" and "processing"
        if( current_user_can('shop_manager') && in_array( $key, array('mark_on-hold', 'mark_processing') ) ){
            $new_actions[$key] = $option;
        }
    }
    if( sizeof($new_actions) > 0 ) {
        return $new_actions;
    }
    return $actions;
}

// Admin order pages: Order status change dropdown
add_filter('wc_order_statuses', 'filter_order_statuses');
function filter_order_statuses($order_statuses) {
    global $pagenow;

    if( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
        $new_order_statuses = array();

        foreach ($order_statuses as $key => $option ) {
            // Targeting "shop_manager" | order statuses "on-hold" and "processing"
            if( current_user_can('shop_manager') && in_array( $key, array('wc-on-hold', 'wc-processing') ) ){
                $new_order_statuses[$key] = $option;
            }
        }
        if( sizeof($new_order_statuses) > 0 ) {
            return $new_order_statuses;
        }
    }
    return $order_statuses;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。它应该可以工作。

【讨论】:

  • 感谢 Loic,他通过将“管理员”更改为需要限制的任何角色来工作,需要将限制应用于下拉菜单。我将第 7 行和第 27 行更改为相关角色。我假设这没问题?在我的例子中是“医生”,但可以是任何角色。
  • 感谢 Loic,它确实回答了它。因为当其他人查看此内容时,您只需将管理员更改为您希望应用这些权限的角色或自行更改状态。灵活的。还没有足够的代表投票。
  • 类似问题:你能帮忙吗? stackoverflow.com/questions/60904370/…
猜你喜欢
  • 2018-03-04
  • 1970-01-01
  • 2021-01-30
  • 2021-07-22
  • 2018-11-22
  • 2017-05-24
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多