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?
  • How to Add Free Shipping Notifications (with amount left) to Woocommerce Product and Cart Pages
  • How to merge Woocommerce cart and checkout page?
  • How to create, remove, rename and re-order Woocommerce product tabs?
  • How to Hide Woocommerce Checkout Fields When Local Pickup is Selected?
  • Woocommerce: How to Give a Discount to Local Pickup?
  • How To Customize Woocommerce My Account Page with Gutenberg?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top