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:

WooCommerce hide shipping method for a product category
All local pickup shipping methods will be hidden if there are products from the “Magic items” product category in the cart.

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:

Hide a shipping method for multiple products in WooCommerce
We use multiple conditional rules to specify whether either of the products is in the customer’s cart.

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:

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

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

Follow me on X