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 'wholesale_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 "beauty experts" 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
  /**
   * Restrict add-to-cart on specific products to the 'doctors' wholesale role only.
   *
   * Usage:
   * 1. Set DOCTORS_ROLE_SLUG below to match the wholesale role slug you created in
   *    WooCommerce > Wholesale Roles.
   * 2. On each product's edit page, check "Restrict purchase to Doctors role only"
   *    under the General tab (or Simple product tab).
   * 3. Add this file as a mu-plugin or paste into your child theme's functions.php.
   */
  define( 'DOCTORS_ROLE_SLUG', 'wholesale_doctors' ); // ← change to your actual wholesale role slug
  // --- 1. Add checkbox to product General tab --- 
  add_action( 'woocommerce_product_options_general_product_data', function () {
      woocommerce_wp_checkbox( [
          'id'          => '_restrict_to_doctors',
          'label'       => __( 'Restrict purchase to Doctors role only', 'woocommerce' ),
          'description' => __( 'Product is visible to all, but only the Doctors wholesale
  role can add it to cart.', 'woocommerce' ),
      ] );
  } );
  add_action( 'woocommerce_process_product_meta', function ( $post_id ) {
      $value = isset( $_POST['_restrict_to_doctors'] ) ? 'yes' : 'no';
      update_post_meta( $post_id, '_restrict_to_doctors', $value );
  } );
  // --- 2. Block non-doctors from purchasing (hides add-to-cart button, shows "Read more") ---
  add_filter( 'woocommerce_is_purchasable', function ( $purchasable, $product ) {
      if ( 'yes' !== get_post_meta( $product->get_id(), '_restrict_to_doctors', true ) ) {
          return $purchasable;
      }
      $user = wp_get_current_user();
      if ( ! in_array( DOCTORS_ROLE_SLUG, (array) $user->roles, true ) ) {
          return false;
      }
      return $purchasable;
  }, 10, 2 );
  // --- 3. Show a notice on the product page for non-doctors ---
  add_action( 'woocommerce_single_product_summary', function () {
      global $product;
      if ( ! $product || 'yes' !== get_post_meta( $product->get_id(),
  '_restrict_to_doctors', true ) ) {
          return;
      }
      $user = wp_get_current_user();
      if ( in_array( DOCTORS_ROLE_SLUG, (array) $user->roles, true ) ) {
          return;
      }
      echo '<p class="woocommerce-info wwp-restricted">' .
          esc_html__( 'This product is available for purchase by registered Doctors only.',
   'woocommerce' ) .
          '</p>';
  }, 25 );
  // --- 4. Hard block at cart validation (safety net for direct add-to-cart URLs) ---
  add_filter( 'woocommerce_add_to_cart_validation', function ( $passed, $product_id ) {
      if ( 'yes' !== get_post_meta( $product_id, '_restrict_to_doctors', true ) ) {
          return $passed;
      }
      $user = wp_get_current_user();
      if ( ! in_array( DOCTORS_ROLE_SLUG, (array) $user->roles, true ) ) {
          wc_add_notice(
              __( 'This product is available for purchase by registered Doctors only.',
  'woocommerce' ),
              'error'
          );
          return false;
      }
      return $passed;
  }, 10, 2 );
  // --- 4. Hide Add to cart on variable products from non doctors --- 
  add_action( 'wp_head', function () {
      if ( ! is_product() ) {
          return;
      }
      $user = wp_get_current_user();
      if ( in_array( DOCTORS_ROLE_SLUG, (array) $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;
          }
      </style>
      <?php
  } );

Comments

Add a Comment