Home / eCommerce / WWPP – Restrict add-to-cart on specific products to a specific wholesale role only
Duplicate Snippet

Embed Snippet on Your Site

WWPP – Restrict add-to-cart on specific products to a specific wholesale role only

How to use and configure this snippet:
1. Set your exact Role Key: In the snippet, I used 'doctors'. Please ensure this matches the exact Role Key of your doctors role (you can find the exact key in WooCommerce → Wholesale Roles).
2. Set your Category Slug: I used 'doctors-only'. Create a category for these restricted products and place its slug here. Any product assigned to this category will be restricted.
3. Add the code to your site: We highly recommend adding this code using a free plugin like Code Snippets. This is much safer than editing your theme's functions.php file directly and ensures the code isn't lost during theme updates.

Once applied, regular users or other wholesale customers will still see the product, but the "Add to Cart" button will be removed, and they will not be able to purchase it.

Code Preview
php
<?php
// 1. Make the product unpurchasable for non-doctors
add_filter( 'woocommerce_is_purchasable', 'wws_restrict_purchase_for_doctors', 10, 2 );
function wws_restrict_purchase_for_doctors( $is_purchasable, $product ) {
    $allowed_role = 'doctors'; 
    $restricted_category = 'doctors-only'; 
    $product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();
    if ( has_term( $restricted_category, 'product_cat', $product_id ) ) {
        $current_user = wp_get_current_user();
        if ( ! in_array( $allowed_role, (array) $current_user->roles, true ) ) {
            return false;
        }
    }
    return $is_purchasable;
}
// 2. Display the custom message on the single product page for non-doctors
add_action( 'woocommerce_single_product_summary', 'wws_show_doctor_restriction_message', 31 );
function wws_show_doctor_restriction_message() {
    global $product;
    
    $allowed_role = 'doctors';
    $restricted_category = 'doctors-only';
    $product_id = $product->get_id();
    if ( has_term( $restricted_category, 'product_cat', $product_id ) ) {
        $current_user = wp_get_current_user();
        if ( ! in_array( $allowed_role, (array) $current_user->roles, true ) ) {
            echo '<p class="woocommerce-info wwp-restricted">' .
                 esc_html__( 'This product is available for purchase by registered Doctors only.', 'woocommerce' ) .
                 '</p>';
        }
    }
}
// 3. Hide the add to cart button on variable products using CSS for non-doctors
add_action( 'wp_head', 'wws_hide_variable_add_to_cart_for_non_doctors' );
function wws_hide_variable_add_to_cart_for_non_doctors() {
    if ( ! is_product() ) {
        return;
    }
    $allowed_role = 'doctors';
    $current_user = wp_get_current_user();
    
    if ( in_array( $allowed_role, (array) $current_user->roles, true ) ) {
        return;
    }
    ?>
    <style>
        .product:has(.summary .woocommerce-info.wwp-restricted) form.variations_form.cart .woocommerce-variation-add-to-cart.variations_button {
            display: none !important;
        }
    </style>
    <?php
}

Comments

Add a Comment