If you are running a WooCommerce store and seeking to hide specific shipping methods based on certain product categories, the following code provides a solution.
add_filter( 'woocommerce_package_rates', 'ts_product_category_hide_shipping_methods', 90, 2 ); function ts_product_category_hide_shipping_methods( $rates, $package ) { // Set your product categories in the array (IDs, slugs, or names) $categories = array( 'accessories' ); $found = false; // Loop through each cart item checking for the defined product categories foreach ( $package['contents'] as $cart_item ) { if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) { $found = true; break; } } // If the specified product category is found, unset the undesired shipping methods if ( $found ) { foreach ( $rates as $rate_id => $rate ) { if ( 'local_pickup:3' === $rate->method_id ) { unset( $rates[ $rate_id ] ); } } } return $rates; }
Output
The below output shows that the “local pickup” Shipping options are hidden based on the specific product categories on the WooCommerce cart page.
Similarly, you can also hide specific shipping methods for specific products in WooCommerce cart page.