How to Hide Woocommerce shipping methods for specific shipping classes?

In this post I’m going to show you how to hide Woocommerce shipping methods for specific shipping classes. This allows you to hide all needed shipping methods by using a Woocommerce shipping class.

Before you jump in a quick comment though: all the snippets shown below will go to either to your child theme’s functions.php file or, better yet, use Code Snippets plugin for it (like I do).

If you’re a beginner, then I would suggest you to take a look at this video here below because in it I will show you exactly how to accomplish all that.

Video: How to Hide Woocommerce shipping methods for specific shipping classes?

How to Hide Woocommerce shipping for specific shipping classes?

Now, there are two things you would need to do beforehand:

  • Add a shipping class. Go to Woocommerce >> Settings >> Shipping >> Shipping classes and add one. For example: “Courier only”
  • Write down the shipping class slug because you will need it later. Where to get the slug? See the screenshot below.
  • Add shipping class to the needed product. Open your product and see under Shipping tab.
How to Hide Woocommerce shipping methods for specific shipping classes?

If this is out of the way let’s add first snippet. So, go to Snippets >> Add new and paste this snippet inside your code box. Pay attention though that you need to change the shipping class slug on line 4 accordingly.

So, if in this example I have a “Courier only” shipping class with a slug “courier-only”, then replace the slug in the code accordingly with the correct one.

One more thing, on line 12 there is a shipping method that you are hiding (Flat rate). Change the shipping method name and ID as needed. How to find your shipping ID number? Well, take a look at the video above (see the 3min 44sec mark).

/* Hide Woocommerce shipping method for a specific shipping class */
function hide_flat_rate_shipping( $rates, $package ) {
    // shipping class IDs that need the method removed
    $shipping_classes = array('courier-only'); // Here goes your shipping class slug
    $if_exists = false;
 
    foreach( $package['contents'] as $key => $values ) {
        if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
            $if_exists = true;
    }
 
    if( $if_exists ) unset( $rates['flat_rate:2'] ); // Here goes your shipping method that needs to be hidden. Don’t forget to add correct one here
 
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_flat_rate_shipping', 10, 2 );

There’s also an alternative way to hide multiple shipping methods for various shipping classes using a single function. For example, in this case:

  • If a product belongs to the shipping class ‘package1,’ we will deactivate the flat rate for zones 1, 2, and 3.
  • If the product belongs to the shipping class ‘package2,’ we will deactivate local pickup for zones 1, 2, and 3.
function wpsh_hide_shipping( $rates, $package ) {
    // Define an array that maps shipping classes to the shipping methods that need to be hidden
    $class_to_methods = array(
        'package1' => array(
            'flat_rate:4', // Flat rate for Zone 1
            'flat_rate:5', // Flat rate for Zone 2
            'flat_rate:6'  // Flat rate for Zone 3
        ),
        'package2' => array(
            'local_pickup:3', // Local pickup for Zone 1
            'local_pickup:4', // Local pickup for Zone 2
            'local_pickup:5'  // Local pickup for Zone 3
        )
    );
    
    $shipping_class = false;

    foreach( $package['contents'] as $key => $values ) {
        $shipping_class = $values['data']->get_shipping_class();
        if( array_key_exists($shipping_class, $class_to_methods) ) {
            foreach ($class_to_methods[$shipping_class] as $method) {
                unset($rates[$method]);
            }
        }
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'wpsh_hide_shipping', 10, 2 )

How to Hide Multiple Woocommerce shipping methods for specific shipping classes?

In the previous example, we hid a Flat rate method. But what if you need to hide multiple Woocommerce shipping methods for specific shipping classes? Well, just take the code below and see the comments inside the code.

See the example that I am using to hide three different shipping methods (SmartPost, DPD and Flat rate) for the Courier only shipping class.

/* Hide Woocommerce shipping method for a specific shipping class (for free shipping) */
function wpsh_hide_shipping( $rates, $package ) {
    // Add your shipping class IDs that need the method removed
   $shipping_classes = array('cargobus');
    $if_exists = false;
 
    foreach( $package['contents'] as $key => $values ) {
        if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
            $if_exists = true;
    }
   // Add your shipping methods that need the method removed
    if( $if_exists ) {
	  unset( $rates['local_pickup:2'] ); // Local pickup
	  unset( $rates['flat_rate:1'] ); // Flat rate
	}
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'wpsh_hide_shipping', 10, 2 );

How to display Woocommerce shipping class name on cart and checkout pages?

Next, I’m going to display Woocommerce shipping class name on the cart and checkout pages. It will appear below the product title. Hence, let’s use this piece of code.

// Display Woocommerce shipping class on cart page

add_filter( 'woocommerce_cart_item_name', 'display_shipping_class_in_cart_and_checkout', 20, 3);
function display_shipping_class_in_cart_and_checkout( $item_name, $cart_item, $cart_item_key ) {
    // Only in cart page (remove the line below to allow the display in checkout too)
    if( ! ( is_cart() || is_checkout() ) ) return $item_name;

    $product = $cart_item['data']; // Get the WC_Product object instance
    $shipping_class_id = $product->get_shipping_class_id(); // Shipping class ID
    $shipping_class_term = get_term( $shipping_class_id, 'product_shipping_class' );

    if( empty( $shipping_class_id ) )
        return $item_name; // Return default product title (in case of)

    $label = __( 'Shipping', 'woocommerce' ); // Change your label if needed

    return $item_name . '<br>
        <p class="item-shipping_class" style="margin:12px 0 0;">
            <strong>' .$label . ': </strong>' . $shipping_class_term->name . '</p>';
}

How to display a shipping class based custom message on Woocommerce cart and checkout pages?

First, why would you need to display a shipping class based custom message on Woocommerce cart and checkout pages? I use it because if I hide shipping methods and my customer may be confused about why there is only a Courier and no other methods available.

Therefore, with this snippet here below, I will display this custom message here below for the products that have “Courier only” shipping class. “Some of your products have a shipping class “Courier only”. For that reason we can offer only this shipping method.”

/* Display a hipping class based custom message on Woocommerce cart and checkout pages */
add_action( 'woocommerce_review_order_before_order_total', 'display_shipping_class_message' );
add_action( 'woocommerce_cart_totals_after_order_total', 'display_shipping_class_message' );
function display_shipping_class_message(){
  
    $shipping_classes = array('courier-only');   // Here goes your shipping class slugs in the array
    // Here goues your message shown in the cart and checkout page
    $message = __('<strong>Some of your products have a shipping class "Local pickup only". For that reason we can offer only local pickup as a shipping method.</strong>', "woocommerce");

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        $shipping_class = $cart_item['data']->get_shipping_class();
        
        // echo '<pre>' . print_r($shipping_class, true) . '</pre>'; // Uncomment for testing

        // Check cart items for specific shipping class, displaying a message
        if( in_array($shipping_class, $shipping_classes ) ){
		  // Change NB! text if needed
            echo '<tr class="shipping-note">
                <td colspan="2"><strong>'.__("NB!", "woocommerce").'</strong> '.$message.'</td> 
            </tr>';
            break;
        }
    }
}

Now, if you add all these snippets as I showed you, then this should be the end result. As you see from the screenshot below:

  • One product has “Shipping: Courirer only” class
  • Only available shipping method is Courier and all other ones (Free shipping included) is hidden
  • There is a custom message shown below shipping totals
How to Hide Woocommerce shipping methods for specific shipping classes?

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Here are some of my favorite WordPress tools

Thanks for reading this article! I hope it's been useful as you work on your own websites and e-commerce sites. I wanted to share some tools I use as a WordPress developer, and I think you'll find them helpful too.

Just so you know, these are affiliate links. If you decide to use any of them, I'll earn a commission. This helps me create tutorials and YouTube videos. But honestly, I genuinely use and recommend these tools to my friends and family as well. Your support keeps me creating content that benefits everyone.

Themes: Over the past few years, I've consistently relied on two primary themes for all sorts of projects: the Blocksy theme and the Kadence Theme. If you explore this website and my YouTube channel, you'll come across numerous tutorials that delve into these themes. If you're interested in obtaining a 10% discount for both of these themes, then:

Code Snippets Manager: WPCodeBox allows you to add code snippets to your site. Not only that, but it also provides you with the capability to construct and oversee your WordPress Code Snippets library right in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get a 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration, and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get a 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I like but the one that stands out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy-to-use managed cloud hosting, then I recommend Verpex Hosting (see my review here). By the way, this site is hosted in Verpex.)

To see all my most up-to-date recommendations, check out this resource that I made for you!

Janek T.
Janek T.

Improve this text: {CLIPBOARD}

- I have been passionate about Wordpress since 2011, creating websites and sharing valuable tips on using Wordpress and Woocommerce on my site.
- Be the first to receive notifications about new tutorials by subscribing to my Youtube channel .
- Follow me on Twitter here

Articles: 134