Restrict WooCommerce products to a user role
A wholesale range, products for members only, a private catalogue for one customer: the store has to hide some products from everyone else, in the shop, in search and on the product page itself. WooCommerce has no setting for it, but three hooks cover the cases.
Problem: Products marked for the role “wholesale_customer” must not appear in the shop or search for other visitors, their URL must not open, and they must not be purchasable through a direct add-to-cart link.
Solution: Store the required role in product meta, exclude those products in woocommerce_product_query for users without the role, redirect from the product page on template_redirect, and return false from woocommerce_is_purchasable.
Who has access
function ha_user_has_role( $role ) {
$user = wp_get_current_user();
return $user->exists() && ( in_array( $role, (array) $user->roles, true ) || user_can( $user, 'manage_woocommerce' ) );
}
function ha_product_allowed( $product_id ) {
$role = get_post_meta( $product_id, '_ha_required_role', true );
return ! $role || ha_user_has_role( $role );
}
The role is compared with $user->roles. current_user_can( 'wholesale_customer' ) also works by accident, because WordPress treats role names like capabilities, but the core documentation advises against relying on it. Shop managers always pass, so they can still see and edit the products.
Hide them in the shop and search
add_action( 'woocommerce_product_query', function ( $query ) {
if ( ha_user_has_role( 'wholesale_customer' ) ) {
return;
}
$meta_query = (array) $query->get( 'meta_query' );
$meta_query[] = [
'relation' => 'OR',
[ 'key' => '_ha_required_role', 'compare' => 'NOT EXISTS' ],
[ 'key' => '_ha_required_role', 'value' => '' ],
];
$query->set( 'meta_query', $meta_query );
} );
woocommerce_product_query runs for the main query on the shop, category, tag and product search pages. Related products, shortcodes and blocks use their own queries; hide the products there with the woocommerce_related_products filter or by putting them into a hidden category.
Block the product page
add_action( 'template_redirect', function () {
if ( ! is_singular( 'product' ) || ha_product_allowed( get_queried_object_id() ) ) {
return;
}
if ( ! is_user_logged_in() ) {
wp_safe_redirect( add_query_arg( 'redirect_to', rawurlencode( get_permalink() ), wc_get_page_permalink( 'myaccount' ) ) );
exit;
}
wc_add_notice( __( 'This product is not available for your account.', 'textdomain' ), 'error' );
wp_safe_redirect( wc_get_page_permalink( 'shop' ) );
exit;
} );
Block the purchase
add_filter( 'woocommerce_is_purchasable', function ( $purchasable, $product ) {
$id = $product->get_parent_id() ?: $product->get_id(); // variations: the meta is on the parent
return $purchasable && ha_product_allowed( $id );
}, 10, 2 );
This also stops ?add-to-cart=123 links and AJAX add-to-cart requests, because WooCommerce checks is_purchasable() before adding an item.
NOTE: The restriction is only as good as its coverage: the WooCommerce REST API (with keys), product feeds and the XML sitemap list products without these hooks. Remove restricted products from the sitemap with wp_sitemaps_posts_query_args, and keep the shop page caching in mind: logged-in users usually bypass the page cache, guests do not.