防止用户角色更改woocommerce订单状态 [英] Prevent User Role from changing woocommerce order status

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

问题描述

我们想防止商店经理更改订单状态,我们在下面的链接中找到了帮助

We would like to prevent shop manager from changing order status, we found a help here in the link below Restrict user role to change only some order statuses in Woocommerce

但是这里的问题是将某些角色(商店经理)限制为某些订单状态,我们需要拒绝商店经理完全更改订单状态,而不是将其限制为某些订单状态.

But the issue here that it limits certain role ( shop Manager ) to some order statuses, we need to deny the shop manager from changing the order status completely not limit it to some order statuses.

我们提到的代码段也从批量操作下拉列表&中删除了订单状态此处的订单详细信息: https://prnt.sc/mpfl3b ,我们也需要从快速操作列此处 https://snipboard.io/B6SYHb.jpg

Also the snippet we mentioned remove the the order statuses from the bulk action drop down & the order details here: https://prnt.sc/mpfl3b, we need to remove the statuses too from the quick action column here https://snipboard.io/B6SYHb.jpg

当我们尝试从批量,订单详细信息页面或操作列中更改订单状态时,我们试图让商店经理来查找没有订单状态可供选择更改或完全禁用.

Simply we try to have the shop manager to when he try to change order status from bulk, order details page, or actions column to find there is no order statuses to select to change it or disable it completely.

最好的问候

推荐答案

在示例代码中可以看到,状态条件是在if语句中确定的,因为您希望无限制地应用它,因此删除if语句并返回空数组的问题

As you can see in the example code the conditions of the statuses are determined in the if statement, because you want to apply this without a limit, it's just a matter of removing that if statement and returning empty arrays

p.s;如果您将我的答案标记为解决方案,则还可以投票给 @LoicTheAztec 原始答案,因为他的代码即将包含解决方案.

p.s; if you mark my answer as the solution, then also vote for @LoicTheAztec original answer if you have not already done this, since his code just about contained the solution.

// Admin orders list: bulk order status change dropdown
function filter_dropdown_bulk_actions_shop_order( $actions ) {
    // Targeting shop_manager
    if( current_user_can( 'shop_manager' ) ) {
        $actions = (array) null;
    }

    return $actions;
}
add_filter( 'bulk_actions-edit-shop_order', 'filter_dropdown_bulk_actions_shop_order', 20, 1 );

// Admin orders list: quick action
function filter_order_actions( $actions, $order ) {
    // Targeting shop_manager
    if( current_user_can( 'shop_manager' ) ) {
        $actions = (array) null;
    }

    return $actions;
}
add_filter( 'woocommerce_admin_order_actions', 'filter_order_actions', 10, 2 );

// Admin order pages: order status dropdown
function filter_order_statuses( $order_statuses ) { 
    global $post, $pagenow;

    if( $pagenow === 'post.php' || $pagenow === 'post-new.php' ) {
        // Get ID
        $order_id = $post->ID;

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // TRUE
        if ( $order ) { 
            // Get current order status
            $order_status = 'wc-' . $order->get_status();

            // New order status
            $new_order_statuses = array();

            foreach ($order_statuses as $key => $option ) {
                // Targeting "shop_manager"
                if( current_user_can('shop_manager') && $key == $order_status ) {
                    $new_order_statuses[$key] = $option;
                }
            }

            if( sizeof($new_order_statuses) > 0 ) {
                return $new_order_statuses;
            }
        }
    }
    return $order_statuses;
}
add_filter('wc_order_statuses', 'filter_order_statuses', 10, 1 );

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

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