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

How to Limit WooCommerce Order Status Changes to Specific User Roles?

An online store might hold different user roles and updating the custom order status to a specific user role may help admin to manage orders easily.

In this post, we will create a custom order status such as ‘Awaiting shipping’ and whenever a ‘subscriber’ user places an order, then the order status skips the default statuses and will be set to a new status ‘Awaiting shipping’ as defined in the code.

Solution: Set a New Order Status For Subscriber User Role

In the below code snippet, by assigning a distinct order status to subscriber orders, the admin can efficiently manage subscription-based orders separately from regular orders.

function ts_register_awaiting_shipment_order_status() {
    register_post_status( 'wc-pendingwholesale', array(
        'label'                     => 'Awaiting shipment',
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Awaiting shipment <span class="count">(%s)</span>', 'Awaiting shipment <span class="count">(%s)</span>' ),
    ) );
}
add_action( 'init', 'ts_register_awaiting_shipment_order_status' );

// Add to list of WC Order statuses
function ts_add_awaiting_shipment_to_order_statuses( $order_statuses ) {
    $new_order_statuses = array();
    // add new order status after processing
    foreach ( $order_statuses as $key => $status ) {
        $new_order_statuses[ $key ] = $status;
        $new_order_statuses['wc-pendingwholesale'] = 'Awaiting shipment';
    }
    return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'ts_add_awaiting_shipment_to_order_statuses' );

add_action( 'woocommerce_order_status_changed', 'ts_status_changed_processsing_weighted' );
function ts_status_changed_processsing_weighted( $order_id, $checkout = null ) {
    global $woocommerce;
    $order = new WC_Order( $order_id );

    $user_roles = wp_get_current_user()->roles;
    if ( in_array( 'subscriber', $user_roles ) ) {
        $order->update_status( 'pendingwholesale', 'order_note' );
    }
}

Output

As we have created a new custom order status such as ‘Awaiting Shipping’ this status will now be included to the default order statuses and shown in the status dropdown of edit order page.

How to Limit WooCommerce Order Status Changes to Specific User Roles? - Tyche Softwares

Whenever a subscriber logs in and completes an order, the order status will be automatically updated to ‘Awaiting Shipping’, as shown below.

How to Limit WooCommerce Order Status Changes to Specific User Roles? - Tyche Softwares

Addionally, you can incorporate colors and icons to these statuses giving a visual appeal and distinguish the order statuses. Check out this post that will guide you to change WooCommerce order status colors.

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