in

Easily Disable Add to Cart in WooCommerce for Specific User Roles

Control purchasing by role with a simple WooCommerce snippet. Learn how to choose which users to restrict, hide the Add to Cart button or block purchases, and test your setup. This guide shows how to disable add to cart for user roles while avoiding common issues with caching and direct checkout.

Need to limit who can purchase products in your WooCommerce store? A small code snippet can help you disable the Add to Cart button for selected user roles, such as guests, subscribers, or wholesale customers.

In this guide, you’ll learn how to use WooCommerce disable add to cart for user roles settings through a simple snippet, so you can tailor the shopping experience to your store’s needs. It’s a practical way to manage access without changing how other customers browse and buy.

WooCommerce Disable Add to Cart for User Roles: How It Works

A store may need to show the same products to everyone while reserving purchases for a particular group. Before adding a snippet, decide who should be restricted and whether the change is only visual or should also prevent checkout.

Those choices determine how the rule behaves across product pages, the cart, and checkout. A little planning helps you avoid surprising customers or leaving an alternate purchase route open.

Identify which user roles should be restricted

Start by listing the customer groups that should not be able to purchase. WooCommerce uses WordPress user roles to organize account permissions, so the snippet can target one or more roles rather than individual customers.

Common examples include:

  • Guests who need to create an account or sign in before buying.
  • Subscribers who can browse but are not approved as customers.
  • Wholesale or trade accounts that require a separate ordering process.

Check the role’s actual name or slug before configuring the rule. A custom role added by a membership or wholesale plugin may have a different identifier than its label in the dashboard. Also, guests are not logged-in users with a role, so a restriction for them may need a separate check for whether the visitor is logged in.

Choose whether to hide the button or block purchasing

Once you know which visitors to target, consider what they should experience on the store. Hiding the button keeps product pages cleaner, while a purchase restriction is important when those users must not place orders.

A button-only change is mainly a front-end adjustment: it removes the visible Add to Cart option, but it may not stop someone from reaching the cart through a direct add-to-cart URL or another store feature. For stronger control, the rule should also prevent the restricted role from adding products and completing checkout.

For example, a wholesale store might hide purchase controls from retail visitors and display a note directing them to apply for an account. In contrast, a catalog that simply requires login may show the products but block purchasing until the visitor signs in. In either case, test as both a restricted and an allowed account, including a direct cart attempt, to confirm the behavior matches your policy.

Add a WooCommerce Snippet to Disable Add to Cart for User Roles

Once you’ve decided who should be able to buy, the next step is choosing where the rule should live and how WooCommerce should enforce it. A small PHP snippet can hide the purchase option and make the product unavailable to restricted accounts.

Start by placing the code somewhere that won’t be overwritten during a theme update. Then you can connect the rule to WooCommerce’s purchasing checks and adapt it to your catalog.

Add the code using a child theme or snippets plugin

A child theme keeps custom code separate from the parent theme, while a snippets plugin lets you manage it without editing theme files. Either option is preferable to changing your main theme directly, since an update could erase those changes.

Add the following snippet to your child theme’s functions.php file or a PHP snippets plugin. Replace the example role slugs with the ones used on your site.

function mystorerestrictrolepurchases( $purchasable, $product ) {
    if ( ! isuserloggedin() ) {
        return $purchasable;
    }
    $restrictedroles = array( 'subscriber', 'wholesalecustomer' );
    $user = wpgetcurrentuser();
    if ( arrayintersect( $restrictedroles, (array) $user->roles ) ) {
        return false;
    }
    return $purchasable;
}
addfilter( 'woocommerceispurchasable', 'mystorerestrictrolepurchases', 10, 2 );
addfilter( 'woocommercevariationispurchasable', 'mystorerestrictrolepurchases', 10, 2 );

Use a WooCommerce hook to restrict add-to-cart access

The snippet uses WooCommerce’s woocommerceispurchasable filter to mark products as unavailable to selected roles. This removes the normal Add to Cart option and also makes the product fail WooCommerce’s standard purchasability check when someone tries to add it to the cart.

The variation filter applies the same rule to product variations. After saving, test with both a restricted account and an allowed account, and check a variable product as well as a simple one. If you use a custom cart or checkout extension, test those flows too.

Customize the snippet for products, categories, or guest users

You can narrow the restriction further instead of applying it across the entire catalog. Add conditions inside the function before the role check, using product IDs or product categories to target only selected items.

For example, to restrict products in a category with the slug members-only, add this before checking the user’s roles:

if ( hasterm( 'members-only', 'productcat', $product->getid() ) ) {
    return false;
}

To restrict guests as well, change the first condition to return false; rather than returning $purchasable. That makes products unavailable to visitors who are not logged in. Before deploying, confirm the intended role slugs and category slug, then retest the purchase paths you identified earlier.

Test and Troubleshoot Role-Based Add-to-Cart Restrictions

With the rule in place, a quick round of testing can reveal whether it behaves as intended across your store—not just on one product page. Check the setup first, then try different account types and purchase routes.

Start by confirming the code is active and targets the right products and roles. These checks can catch small setup mistakes before customers run into them.

Check the WooCommerce disable add to cart for user roles setup

Review the snippet before testing: a misspelled role slug or a code error can stop the restriction from working. The role’s displayed name and its actual slug may differ, especially when a plugin created a custom role.

  • Confirm the snippet is enabled in your snippets plugin or saved in the active child theme.
  • Check the restricted role slugs against the roles assigned to test accounts.
  • Verify any product or category condition uses the correct ID or slug.
  • Look for PHP errors if the site behaves unexpectedly after saving.

If the restriction applies to every product, remove or review any extra targeting condition. Conversely, if only some products are affected, check whether the category or product condition is excluding the others.

Once the code checks out, compare what a restricted visitor sees with what an approved customer can do. Testing both sides helps distinguish a working rule from a button that has simply disappeared.

Test the restriction with different user accounts

Use separate accounts for each relevant role, and test in a private browser window or after signing out between sessions. This avoids mistaking a previous login or saved cart for the current account’s permissions.

Check a simple product and a variable product. For restricted accounts, confirm the button is unavailable and attempt to add an item; for an allowed account, make sure the normal purchase flow still works. If guests are part of the policy, test while fully logged out too.

Also try a product that was already in the cart before the restriction was applied. Removing the button does not necessarily clear existing cart items, so verify whether those items can still reach checkout.

If results differ between browsers or accounts, caching or another store feature may be affecting the test. The following checks can help isolate the cause.

Fix common issues with caching, plugins, and direct checkout access

A cached page can show an outdated button even when the PHP rule is active. Clear your site, hosting, and browser caches, and exclude cart and checkout pages from page caching where appropriate.

When another plugin provides a custom cart, quick-buy button, or checkout flow, test it separately. Such tools may use a different purchase route, so the standard WooCommerce check may not cover every custom workflow.

Finally, try a direct add-to-cart link and review checkout with an item already in the cart. If a restricted user can still submit an order, add server-side cart or checkout validation suited to your setup; hiding a control alone is not a complete access rule.

Set Clear Purchase Rules for Every Customer Role

Disabling Add to Cart for selected WooCommerce user roles gives you more control over who can buy while letting you shape the browsing experience to suit your store. Decide which customers to restrict and whether hiding the button is enough or purchasing must also be blocked, then use a carefully placed snippet to apply the rule where it’s needed.

Before relying on the change, test restricted and approved accounts across your product types and checkout paths. A little troubleshooting can catch role, cache, or plugin conflicts and help ensure the restriction works beyond the product page. With the right setup, you can manage access confidently while keeping the buying experience smooth for customers who are allowed to purchase.

Leave a Reply

Your email address will not be published. Required fields are marked *

      Written by Maeve Rodriguez

      Maeve is a Business Content Writer and Front-End Developer. She's a versatile professional with a talent for captivating writing and eye-catching design.