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 );

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 above and paste it below it and change these parts:

  • Function name on line 2. So, instead “hide_flat_rate_shipping” add “hide_local_pickup” or whatever other function name.
  • Shipping method ID on line 12. So, instead of “flat_rate:2” add “free_shipping:6” or whatever ise the shipping ID for the shipping method you are hiding.
  • Function name on line 16. So, instead “hide_flat_rate_shipping” add “hide_local_pickup” or whatever other function name.

See the example that I am using to hide Free shipping for the Courier only shipping class.

/* Hide Woocommerce shipping method for a specific shipping class (for free shipping) */
function hide_free_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['free_shipping:6'] ); // 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_free_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 hipping 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?

Related Woocommerce tips

Here are some of my favorite Wordpress tools

Thank you for reading this article. I hope you found it helpful as you build your own websites and e-commerce sites. Here are some tools I use as a Wordpress developer and enthusiast that I hope you’ll also find helpful.

These are affiliate links, so if you do decide to use any of them, I’ll earn a commission and this helps me create these tutorials and make Youtube videos. But in all honesty, these are the exact tools that I use and recommend to everyone, even my friends and family.

Themes: For the last couple of years I have two go-to themes which I use for every kind of projects. Those two themes are Blocksy theme and Kadence Theme. On this site and my Youtube channel you’ll see a lot of tutorials I have made about them. If you would like to get a 10% discount for both of them then:

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 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 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I really like but the one that stands really 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: For getting started, I really like A2 Hosting. They have the dedicated and fast Wordpress hostinh and support has also been fast and responsive. If you’re more tech savvy and want to have a really fast managed cloud hosting then I recommend Cloudways. (Btw, this site is hosted in Cloudways.)

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

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.

Choose amount

Best selling plugins

Default image
Janek T.
I am a Wordpress enthusiast who has been making websites since 2011. In this site I am offering simple to follow tips on how to use Wordpress and Woocommerce. If you want to be the first to be notified about the new tutorials then please subscribe to my Youtube channel here Follow me in Twitter here
Articles: 72