WooCommerce: Exclude Shipping Method From Specific User Role

WooCommerce offers a robust shipping system, but what if you want to tailor it based on the logged in user role?

In this blog post, we’ll show you how to exclude specific shipping methods from users with a particular role. This allows you to, for example, offer free shipping only to VIP members or restrict express delivery options to regular customers.

All you need is the user role slug (WordPress Dashboard > Users -> All Users > above the user list table you’ll see a horizontal list displaying the current user roles available on your site (e.g., Administrator, Editor, Author). Hover on one of them and see the URL, which will contain the exact slug e.g. “customer”) and the shipping rate ID you wish to disable (e.g. “flat_rate:9”. For more info you can find out how to find IDs here: https://businessbloomer.com/woocommerce-disable-free-shipping-if-cart-has-shipping-class).

Enjoy!

Let’s say we want to disable Local Pickup for logged in customers. The snippet below will give you a hint!

PHP Snippet: Disable Shipping Rate For Specific User Role (customer, subscriber, etc.) @ WooCommerce Cart & Checkout

  • The code uses a filter hook woocommerce_package_rates to intercept the available shipping rates before they are displayed to the customer during cart/checkout.
  • The bbloomer_disable_shipping_rate_user_role function is attached to this filter with a priority of 9999 (higher priority ensures it runs later in the filter chain).
  • Inside the function, it checks if the current user has the role customer using wc_current_user_has_role.
  • If the user is a customer and the flat_rate:5 rate exists in the $rates array, it removes that specific rate using unset.
  • Finally, the modified $rates array is returned.

Note: This code assumes the shipping rate ID you want to disable for customers is flat_rate:5. Replace it with the actual ID if it’s different on your WooCommerce setup.

/**
 * @snippet       Disable Shipping Method By User Role
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_package_rates', 'bbloomer_disable_shipping_rate_user_role', 9999, 2 );
    
function bbloomer_disable_shipping_rate_user_role( $rates, $package ) {
	if ( wc_current_user_has_role( 'customer' ) ) {
		if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:5'] );
	}
	return $rates;    
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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