How to Hide a Shipping Method for Product Category
In this guide, you will learn two ways of how you can enable or disable WooCommerce shipping methods depending on products from a specific category in the customer’s cart or an order.
First, I will show you how to easily add conditions with a plugin, and then we will also take a look at a programmatic approach.
Disable a Specific Shipping Method for a Product Category Using a Plugin (Recommended)
Here, we will use the Simple Conditional Shipping and Payments for WooCommerce plugin to create conditions in your store settings.
Creating a condition for a shipping method and a product category is quite simple: all you need to do is to visit WooCommerce > Settings > Conditions > Shipping conditions, then create a new conditional rule, and inside that rule, create a new “Product category” child condition.
An example of the conditional rule settings is in the screenshot below:

Having multiple products under the shared product category (or we can use a product tag, or a product brand, by the way) simplifies things a little bit when we need to have conditional rules for many different products at the same time. Without using a product category or a product tag, we would need to create multiple conditional rules, kind of like this:

Can we use conditional rules like this? Of course, we can, but having a product category or a product tag seems to be a much more convenient way to handle things here.
A Programmatic Way of Hiding a Shipping Method for Category
As usual, I won’t leave you without a working code snippet which you can use in your WooCommerce store as it is. By the way, if you don’t know how, there is a guide about it.
/**
* @snippet WooCommerce Hide a Shipping Method for a Product Category
* @author Misha Rudrastyh
* @url https://rudrastyh.com/woocommerce/hide-shipping-method-for-product-category.html
*/
add_filter( 'woocommerce_package_rates', 'rudr_no_local_pickup_if_product_cat' );
function rudr_no_local_pickup_if_product_cat( $rates ) {
$product_cat = 20;
$taxonomy = 'product_cat'; // you can use 'product_tag' for product tags or 'product_brand' for brands
foreach( WC()->cart->get_cart_contents() as $cart_item ) {
if( has_term( $product_cat, $taxonomy, $cart_item[ 'data' ]->get_id() ) ) {
foreach( $rates as $rate_id => $rate ) {
if( str_starts_with( $rate_id, 'local_pickup' ) ) {
unset( $rates[ $rate_id ] );
}
}
break;
}
}
return $rates;
}As you can see in the code above, some things are hard-coded there:
$product_cat– here I provided a product category ID. You need to find it in your WooCommerce admin dashboard. You can use slugs, if you want, but then you might add theget_term_by()function into the code snippet to extract a product category ID from its slug anyway.$taxonomy– you need to use a taxonomy slug here, for WooCommerce, the common ones areproduct_cat(Categories),product_tag(Tags),product_brand(Brands). Of course, if you decide to apply it to regular WordPress categories, you will need to usecategory.local_pickup– using it in combination with thestr_starts_with()PHP function allows us to apply the condition for local pickup shipping method in any shipping zone. But usually, the shipping method ID comes with a number, likelocal_pickup:4orflat_rate:5.
Also, keep in mind that the code snippet here is quite simplified, and it doesn’t apply to scenarios when a customer wants to pay for an order via the “My account” page. Don’t forget to check is_wc_endpoint_url( 'order-pay' ) in your code snippet to code different logic for situations like that, or just use the plugin approach where everything is already in place.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me