Viewing 5 replies - 1 through 5 (of 5 total)
  • Sean Cull

    (@seanreloaded)

    Automattic Happiness Engineer

    Hi there,

    This is more than we can help with here. For customizations we recommend one of the services listed on this page:

    https://woocommerce.com/customizations/

    Another option is to team up with a developer on our WooCommerce Developers Slack Channel:

    https://woocommerceslack.herokuapp.com/

    Thread Starter kingfisher64

    (@kingfisher64)

    Okay, thanks for taking the time to respond.

    Thread Starter kingfisher64

    (@kingfisher64)

    Hi Sean,

    Did as you asked went on Developers Slack Channel. The following works.

    * Note – change custom_user_role = ‘administrator’; to desired role.

    <?php
    
    /**
     * Limit "Add New Order" statuses dropdown for custom user role.
     *
     * @param array $statuses
     * @see wc_get_order_statuses()
     */
    add_filter( 'wc_order_statuses', function( $statuses ) {
    	# Custom user role (registered elsewhere).
    	$custom_user_role = 'administrator';
    	
    	# Status(es) of orders to show to custom user role.
    	$limit_to_order_statuses = array( 
    		'wc-processing' => 1,
    		'wc-on-hold' => 1,
    	);
    
    	# Make sure we're editing right query.
    	# If not, then nothing to change ("return early").
    	if (
    		!function_exists( 'get_current_screen' )
    		||         'add' !== get_current_screen()->action
    		||  'shop_order' !== get_current_screen()->post_type
    		|| 'woocommerce' !== get_current_screen()->parent_base
    	)
    		return $statuses;
    		
    	# Check if user has the specified custom user role.
    	# If they don't, then nothing to change ("return early").
    	if ( !in_array( $custom_user_role, wp_get_current_user()->roles ) )
    		return $statuses;
    	
    	return array_intersect_key( $statuses, $limit_to_order_statuses );
    } );
    
    ?>
    • This reply was modified 5 years, 10 months ago by kingfisher64.
    • This reply was modified 5 years, 10 months ago by kingfisher64.
    Thread Starter kingfisher64

    (@kingfisher64)

    That code above actually restricts orders placed by a certain role. Eg new orders only showing “On Hold” & “Processing”.

    Once code to restrict pre-existing order status’s for certain roles is obtained will post here.

    Thread Starter kingfisher64

    (@kingfisher64)

    This code restricts access to order statuses, put in functions.php file.

    
    // 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;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Order Status Visibility By Role’ is closed to new replies.