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!
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 of9999
(higher priority ensures it runs later in the filter chain). - Inside the function, it checks if the current user has the role
customer
usingwc_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 usingunset
. - 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;
}