Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Restrict Certain User Role From Changing WooCommerce Order Status?

When you run an online store with a team managing order processing, then this customization will help you maintain control over order status changes. This can be done by restricting certain user roles from making unauthorized changes. For example, you can restrict shop managers from changing from/ to a specific order status.

Solution: Restrict Certain User role From Changing WooCommerce Order Status

This code snippet below doesn’t allow a specific user role such as ‘shop manager’ to change the status of orders from ‘processing’ to ‘on-hold’.

add_filter( 'woocommerce_before_order_object_save', 'ts_prevent_order_status_change', 10, 2 );
function ts_prevent_order_status_change( $order, $data_store ) {
    // Below define the disallowed user roles
    $disallowed_user_roles = array( 'shop_manager');

    $changes = $order->get_changes();

    if( ! empty($changes) && isset($changes['status']) ) {
        $old_status    = $order->get_data()['status'];
        $new_status    = $changes['status'];
        $user          = wp_get_current_user();
        $matched_roles = array_intersect($user->roles, $disallowed_user_roles);

        // Avoid status change from "processing" to "on-hold"
        if ( 'processing' === $old_status && 'on-hold' === $new_status && ! empty($matched_roles) ) {
            throw new Exception( sprintf( __("You are not allowed to change order from %s to %s.", "woocommerce" ), $old_status, $new_status ) );
            return false;
        }
    }
    return $order;
}

Output

When the shop manager tries to change the order status manually from ‘processing’ to ‘on hold’ they will prevented from making such a change.

How to Restrict Certain User Role From Changing WooCommerce Order Status? - Tyche Softwares

While this restriction is managed on the admin end, you can also limit WooCommerce custom order status changes to specific user roles. It’s super handy! For example, a custom order status such as ‘Awaiting shipping’ will be set to the order status workflow only when the customer has logged in as a subscriber user role.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x