Skip to content
Advertisement

Prevent User Role from changing woocommerce order status

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.

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.

Best Regards

Advertisement

Answer

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; 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 );
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement