限制用户角色以仅更改Woocommerce中的某些订单状态 [英] Restrict user role to change only some order statuses in Woocommerce

查看:179
本文介绍了限制用户角色以仅更改Woocommerce中的某些订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过woocommerce的下拉列表中的角色将访问权限限制为某些订单状态.我已尝试在functions.php子主题中尝试按角色限制woocommerce订单状态,但无法正常工作,并且没有足够的代表来发表评论.

I would like to restrict access by role to certain order statuses from the dropdown list in woocommerce. I have tried in functions.php child theme the code on Restrict woocommerce order status by role but cannot get it to work and do not have enough rep to post a comment.

https://prnt.sc/mpfl3b 是显示内容的屏幕截图-我想购买经理(或创建的自定义角色),只能将订单标记为正在处理或处于暂挂状态,而下拉菜单中没有其他可见的选项.

https://prnt.sc/mpfl3b is the screenshot of what is showing - I would like shop manager (or a custom role created) to only be able to mark an order as processing or on-hold with no other options visible from the dropdown.

任何建议将不胜感激.

推荐答案

链接的答案代码无法满足您的需求.相反,请尝试以下操作:

The linked answer code will not work for what you want. Instead try the following:

// 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文件中.应该可以.

Code goes in function.php file of your active child theme (or active theme). It should works.

这篇关于限制用户角色以仅更改Woocommerce中的某些订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆