Change WordPress user roles and capabilities › Forums › New Feature Request › How can i hide woocommerce orders based on role?
Tagged: woocommerce
- This topic has 6 replies, 3 voices, and was last updated 2 years, 8 months ago by webiators.
-
AuthorPosts
-
27/09/2021 at 23:31 #7700liftedmadeParticipant
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
28/09/2021 at 01:50 #7702VladimirKeymasterAnswering 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.
28/09/2021 at 01:59 #7703VladimirKeymasterYou 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.
29/09/2021 at 00:28 #7704liftedmadeParticipantCould 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
29/09/2021 at 01:15 #7705liftedmadeParticipantedit access wont work for us because we have 100s of orders and tens of sales agents, we would have to manually update everything.
30/09/2021 at 05:31 #7710VladimirKeymasterI 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.
05/04/2022 at 05:26 #7884webiatorsParticipantYou can show orders that belong to a sales_agent (role) on the admin orders list page.
-
AuthorPosts
- You must be logged in to reply to this topic.