skip to Main Content

is there a way to show certain order statuses for specific roles? E.g. Admin can see On-Hold, Processing, Completed, Trash, etc. But Shop Manager can only see On-Hold, Processing, and Completed.

See screenshot:
enter image description here

So far I’ve tried this code that I found on this thread here but it still showing all statuses for shop managers:

// 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;
}

2

Answers


  1. Are you sure you want to use "current_user_can"? try in_array(‘user_role") to specify as single user role.

    Login or Signup to reply.
  2. I have revisited your code in a better way and added the missing function that filter orders by allowed order statusses for specific user roles in admin orders list:

    // Custom conditional fuction to target specific user roles
    function user_roles_allowed_orders() {
        $targeted_roles = array('shop_manager'); // Here define your targeted user roles
        return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
    }
    
    // Admin orders list: bulk order status change dropdown
    add_filter( 'bulk_actions-edit-shop_order', 'filter_dropdown_bulk_actions_shop_order', 100 );
    function filter_dropdown_bulk_actions_shop_order( $actions ) {
        if ( user_roles_allowed_orders() ) {
            $allowed_actions = array('mark_on-hold', 'mark_processing');
    
            foreach( $actions as $key => $option ){
                if( ! in_array( $key, $allowed_actions ) ){
                    unset($actions[$key]);
                }
            }
        }
        return $actions;
    }
    
    // Admin order pages: Order status change dropdown
    add_filter('wc_order_statuses', 'filter_order_statuses', 100 );
    function filter_order_statuses( $statuses ) {
        global $pagenow, $typenow;
    
        if( in_array( $pagenow, array('post.php', 'post-new.php') )
        && 'shop_order' === $typenow && user_roles_allowed_orders() ) {
            $allowed_statusses = array('wc-on-hold', 'wc-processing');
    
            foreach ($statuses as $key => $option ) {
                if( ! in_array( $key, $allowed_statusses ) ){
                    unset($statuses[$key]);
                }
            }
        }
        return $statuses;
    }
    
    // Filter admin orders for shop managers based
    add_action( 'pre_get_posts', 'filter_shop_manager_orders', 100 );
    function filter_shop_manager_orders( $query ) {
        global $pagenow, $post_type;
        //'shop_manager'
        if( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $post_type
        && user_roles_allowed_orders() ){
            $allowed_statusses = array('wc-on-hold', 'wc-processing');
    
            $query->set( 'post_status', $allowed_statusses );
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search