Change WordPress user roles and capabilities Forums New Feature Request How can i hide woocommerce orders based on role?

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #7700
    liftedmade
    Participant

    Im trying to only display orders that belong to a sales_agent (role), on the admin orders list page.

    I posted a question on Stackoverflow about it: https://stackoverflow.com/questions/69323214/i-want-to-only-display-orders-that-belong-to-a-certain-role-in-user-role-editor

    #7702
    Vladimir
    Keymaster

    Answering on your question at Stackoverflow:

    WooCommerce order is a custom post type ‘shop_order’. Its main record is stored at wp_posts database table (‘wp_’ is db prefix). This record does not store the information about user role. It has field ‘post_author’, which contains user ID, under which this order was created. In order to find a role you need to load user by ID using get_user_by(), and check role(s) of that user.

    #7703
    Vladimir
    Keymaster

    You may try to use URE Pro edit access add-on for this purpose. It allows to show posts(orders) by the list of author (comma separated user IDs). URE applies custom filter “ure_post_edit_access_authors_list“. Just extract the list of users who has role sales_agent, and return through this filter this list if current user has this role too.

    #7704
    liftedmade
    Participant

    Could you provide a code example of getting orders by a specific user role in URE?
    I may want to add additional logic later.

    I can’t seem to get this to work

    #7705
    liftedmade
    Participant

    edit access wont work for us because we have 100s of orders and tens of sales agents, we would have to manually update everything.

    #7710
    Vladimir
    Keymaster

    I successfully tested custom code for your scenario with URE Pro edit restrictions add-on activated together:
    1) Activate edit restrictions add-on.
    2) Install code below as a Must Use plugin or add it to the active theme functions.php file:

    
    add_filter('ure_post_edit_access_authors_list', 'ure_modify_authors_list', 10, 1);
    
    function get_orders_manager_users() {
        global $wpdb;
        
        $role = 'sales_agent';
        $where = $wpdb->prepare( 'meta_value LIKE %s', '%'. $wpdb->esc_like( '"' . $role . '"' ) .'%' );
        $query = "SELECT user_id FROM {$wpdb->usermeta} WHERE $where";
        $result = $wpdb->get_results( $query ); 
        if ( empty( $result ) ) {
            return '';
        }
        
        $users_list = array();
        foreach( $result as $user ) {
            $users_list[] = $user->user_id;
        }    
        
        return $users_list;
    }
    
    function ure_modify_authors_list( $authors ) {
        
        $user = wp_get_current_user();
        if ( !in_array( 'sales_agent', $user->roles ) ) {
            return $authors;
        }
        
        $users_list = get_orders_manager_users();
        $authors1 = implode(',', $users_list );
        if ( !empty( $authors ) ) {
            $authors .= ','. $authors;
        } else {
            $authors = $authors1;
        }    
            
        return $authors;
    }
    

    As a result any user with sales_agent role will be restricted in orders editing by authors list equal the list of users with the sales_agent role.

    #7884
    webiators
    Participant

    You can show orders that belong to a sales_agent (role) on the admin orders list page.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.