Hide Add to Cart for Certain User Roles

You can suppress the add to cart on both the listing and detail page in this fashion:

Listing Page:

In your theme’s functions.php file add the following code.  Where you see ‘customer’, ‘administrator’; you can add more like ‘, ‘subscriber’, ‘editor’, … etc.  That will suppress the button from any logged in user NOT assigned one of those roles.


add_filter('wpim_cart_hide_button_in_listing','wpim_button_by_role');
function wpim_button_by_role( $hide ) {
$current_user = new WP_User( get_current_user_id() );
$user_role = ( ! empty( $current_user->roles ) ) ? array_shift( $current_user->roles ) : NULL;

if ( ! $user_role || ! in_array( $user_role, array( 'customer', 'administrator' ) ) ) {
$hide = TRUE;
}

return $hide;
}

 

Detail Page:

The detail page can suppress the add to cart in one of the following two ways:

1.  Template Override: –  copy the reserve_cart/views/add-to-cart.php template to the theme’s wpinventory/views/add-to-cart.php folder, then at the top add:


$current_user = new WP_User( get_current_user_id() );
$user_role    = ( ! empty( $current_user->roles ) ) ? array_shift( $current_user->roles ) : NULL;

if ( ! $user_role || ! in_array( $user_role, array( 'customer', 'administrator' ) ) ) {
return;
}

2.  Filter:   Hook into this filter and add this function which leverages the already-existing function used to hide the button on the listing page under the “Listing Page” heading above  (this is the “recommended” way, but either will work).


add_filter( 'wpim_get_template_part_path_<wbr />reserve-form.php', 'wpim_hide_button_in_detail', 99999, 1 );

function wpim_hide_button_in_detail( $template ) {
if ( wpim_button_by_role( FALSE ) {
// return nothing, which will cause the system to NOT render the template.
return '';
}

return $template;
}