How to Disable Shipping By Category In WooCommerce

Disable Shipping Method in WooCommerce Based on Category
Do you want to Hide WooCommerce shipping methods based on category?

By default, WooCommerce allows you to charge shipping for products based on country.

You may want to disable shipping methods for certain categories.

The shipping method is a service and charge, that a customer must pay when purchasing an item.

A shipping fee is an essential option for any WooCommerce store if you want to have a smooth delivery experience.

You are provided with shipping methods like Free shipping, Flat rate, and Local Pickup. It is very easy to set them up for various shipping zones based on the locations.

For example, the Free shipping option can be used only for postcodes near the store location, and the Flat rate option for other delivery zones.

How to Disable Shipping Method in WooCommerce Based on Category

In this post, we will share how you can disable a shipping method based on a product category.
For example, you may have a category with large car parts, which may be shipped only with the Flat rate shipping method.

WooCommerce does not have a built-in solution to achieve this. This means that we have to use a custom code snippet.

Let us see how you can achieve this.

Steps to Disable Shipping Method in WooCommerce Based on Category

Here are the steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will disable shipping methods based on category.
  3. Add the following code to the PHP file:

 

/**

 * Filter shipping methods in the checkout

 */

add_filter( 'woocommerce_package_rates', 'njengah_filter_shipping_methods', 10, 2 );

function njengah_filter_shipping_methods( $rates, $package ) {

            // Find Economy shipping method

            $economy_shipping_method_key = FALSE;

            foreach ( $rates as $rate_key => $rate ) {

                        if ( is_object( $rate ) && method_exists( $rate, 'get_label' ) && $rate->get_label() === "Economy" ) {

                                    $economy_shipping_method_key = $rate_key;

                        }

            }

            // Go through all products and check their category

            if ( $economy_shipping_method_key !== FALSE ) {

                        $car_parts_found = FALSE;

                        foreach ( $package['contents'] as $key => $item ) {

                                    $categories = get_the_terms( $item['product_id'], 'product_cat' );

                                    if ( $categories && ! is_wp_error( $categories ) && is_array( $categories ) ) {

                                                foreach ( $categories as $category ) {

                                                            if ( "Car Parts" === $category->name ) {

                                                                        $car_parts_found = TRUE;

                                                            }

                                                }

                                    }

                        }

                        // Car parts has been found, disable the Economy shipping

                        if ( $car_parts_found === TRUE ) {

                                    unset( $rates[$economy_shipping_method_key] );

                        }

            }

            return $rates;

}

Conclusion

In this post, we have shared a custom PHP code snippet to hide shipping methods based on category.

Remember to replace the “Economy” and “Car Parts” (lines 9 and 22) in the code to suit your needs.

If you want to add many shipping restrictions, we recommend using a plugin like WooCommerce Conditional Shipping Pro.

This plugin makes it very easy to restrict shipping methods directly from the WooCommerce settings.

If you encounter any problems implementing the solution above, please contact a qualified WordPress developer.

Similar Articles

  1. How to Edit Billing Details WooCommerce Checkout Page
  2. How to Clear Cart on Logout In WooCommerce