Change WordPress user roles and capabilities › Forums › New Feature Request › How can i hide woocommerce orders based on role? › Reply To: How can i hide woocommerce orders based on role?
30/09/2021 at 05:31
#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.