# Hide a Shipping Method for a Shipping Class

> Removes a shipping method, such as local pickup or express delivery, when the cart contains a product from a specific shipping class (for example bulky items).

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, shipping, shipping-classes
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-hide-shipping-for-class/

## Code

```php
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
    $shipping_class = 'bulky';       // Shipping class slug.
    $method         = 'free_shipping'; // Method ID to hide when the class is in the cart.

    $found = false;

    foreach ( (array) $package['contents'] as $item ) {
        if ( isset( $item['data'] ) && $item['data']->get_shipping_class() === $shipping_class ) {
            $found = true;
            break;
        }
    }

    if ( ! $found ) {
        return $rates;
    }

    foreach ( $rates as $id => $rate ) {
        if ( $rate->get_method_id() === $method ) {
            unset( $rates[ $id ] );
        }
    }

    return $rates;
}, 10, 2 );
```

## Notes

Set $shipping_class to the class slug from WooCommerce > Settings > Shipping > Classes and $method to the method ID (flat_rate, local_pickup, free_shipping).

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hide a Shipping Method for a Shipping Class" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
