Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Hide Specific Shipping Methods Based on Product Categories in WooCommerce?

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.

How to Hide Specific Shipping Methods Based on Product Categories in WooCommerce?

Similarly, you can also hide specific shipping methods for specific products in WooCommerce cart page.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x