Reputation: 13
I'm trying to adapt "Completely hide products from unauthorized users in WooCommerce" answer code to also allow several custom user roles to view this products. I believe the best way to accomplish this is to expand the authorized user function to include this user roles.
This is the changes that I have tried to implement with no success. Can someone shine a light on how to proceed?
// Conditional function checking for authorized users
function is_authorized_user() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] ) ||
array( 'custom_user_role1', 'custom_user_role2', $user->roles ) )
return true;
} else
return false;
}
How to make it work for an array of user roles, instead of just one? Any help is appreciated.
Upvotes: 1
Views: 113
Reputation: 253869
As you have 2 arrays to compare:
you can use array_intersect()
php function to make it work this way:
// Conditional function checking for authorized users
function is_authorized_user(){
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$caps = $user->allcaps;
if ( ( isset($caps['edit_product']) && $caps['edit_product'] ) ||
array_intersect( ['custom_user_role1', 'custom_user_role2'], $user->roles ) ) {
return true;
}
return false;
}
else {
return false;
}
}
It should work now for multiple user roles.
Upvotes: 2