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:
- Log into your WordPress site and access the Dashboard as the admin user.
- 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.
- 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.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- 100+ Tips, Tricks & Snippets Ultimate WooCommerce Hide Guide
- WooCommerce Redirect After Logout [Ultimate Guide]
- WooCommerce Redirect After Checkout: Redirect to Custom Thank You Page
- How to Edit Billing Details WooCommerce Checkout Page
- How to Clear Cart on Logout In WooCommerce
- How to Create Flat Rate Shipping By Weight In WooCommerce
- How to Customize WooCommerce Product Pages
- How to Add WooCommerce My Account Page Redirect
- How to use do_shortcode in WordPress (PHP)
- How to Set Up WooCommerce Storefront Shipping Quick Guide
- How to Disable Specific Plugin Updates In WordPress
- How to Hide Out-of-Stock Products in WooCommerce
- How to Get Checkout URL In WooCommerce
- How to Temporarily Disable Checkout In WooCommerce
- How to Get Payment Methods in WooCommerce » Code Example
- How to Set Up Free Shipping with Minimum Spend In WooCommerce
- How to Change Currency Symbol In WooCommerce
- How to Add Product to Cart Programmatically in WooCommerce
- How to Change Checkout Endpoints WooCommerce
- How to Skip Cart and Redirect to Checkout WooCommerce