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

How to Hide Shipping Method by Shipping Class in WooCommerce?

It is very common that you may have a mix of expensive and budget-friendly products in your WooCommerce store. Offering the same shipping options for all products might not make sense because different products often have different shipping requirements. Based on that you need to hide certain shipping methods that aligns well with the value of customers’ purchases. Using Shipping classes, you can easily tailor shipping options based on the products in the cart.

This post helps you to hide shipping methods based on specific shipping classes in WooCommerce.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Preliminary Steps

1. Create a shipping class from WooCommerce->Settings->Shipping->Shipping classes->Add shipping class. e.g. I have created a shipping class named ‘Electronic Products‘.

Preliminary steps to Hiding Shipping Method by Shipping Class in WooCommerce

2. Assign the products to the shipping class you have created from Products->Add/Edit product ->Product Data->Shipping ->Shipping class. The below image shows an example product assigned to the shipping class’ Electronic products’.

Preliminary steps to Hiding Shipping Method by Shipping Class in WooCommerce

3. Find the shipping class ID from WooCommerce->Settings->Shipping->Shipping classes tab. On this page, right-click on the shipping class name, select Inspect->Developer tools from the menu. e.g. The Shipping Class ID of the class ‘Electronic products’ is: 56.

Preliminary steps to Hiding Shipping Method by Shipping Class in WooCommerce

4. Find the shipping method ID that you want to disable when this product is in the cart. You can do this by going to the WooCommerce checkout page, then right-click on the Shipping method->Select Inspect->Developer tools. The Free shipping method ID, in this case, is free_shipping:35

Preliminary steps to Hiding Shipping Method by Shipping Class in WooCommerce

Solution: Hide Shipping Method for specific Shipping Class in WooCommerce

Imagine an online store selling electronic products containing high-value items and low-value items. Let’s say, I have assigned the high-value product “Moto G 5G Smartphone” to the shipping class “Electronic Products”. We want to hide the “Free shipping” shipping method when this product is in the cart.

add_filter('woocommerce_package_rates', 'ts_restrict_shipping_method_for_product', 10, 2);

function ts_restrict_shipping_method_for_product($rates, $package) {
    $shipping_class_target = 56; // Replace with your desired shipping class ID
    $in_cart = false;

    // Check if the specific shipping class product is in the cart
    foreach (WC()->cart->get_cart_contents() as $key => $values) {
        if ($values['data']->get_shipping_class_id() == $shipping_class_target) {
            $in_cart = true;
            break;
        }
    }

    // If the specific product is in the cart, hide 'Free Shipping:35'
    if ($in_cart && isset($rates['free_shipping:35'])) {
        unset($rates['free_shipping:35']);
    }

    return $rates;
}

Output 1

Use Case: Product with shipping class ‘Electronic Products’ added to cart

Let’s consider that a customer adds a “Moto G 5G Smartphone” (a high-value electronic product) to their shopping cart. The code helps us to hide the ‘Free shipping’ method as this high-value product is assigned to the class ‘Electronic Products’.

How to Hide Shipping Method by Shipping Class in WooCommerce

Output 2

Use Case: Any other product added to the cart

Let’s consider that a customer adds a “boAT Airpodes 131” (a low-value electronic product) to their shopping cart. As the low-value electronic product is not assigned to the class, the code does not hide the “Free Shipping” option and shows other available shipping methods.

How to Hide Shipping Method by Shipping Class in WooCommerce

Code Explanation

  • The code starts by registering a filter in WooCommerce. It hooks into the woocommerce_package_rates filter, which allows you to modify the available shipping rates during the checkout process. 
  • The ts_restrict_shipping_method_for_product function is defined. It takes two arguments: $rates and $package. This function will be used to filter and modify the shipping rates.
  • The variable named $shipping_class_target is assigned the value 56. This value represents the desired shipping class ID that you want to target. You should replace it with the actual shipping class ID you want to work with.
  • Another variable named $in_cart is initialized to false. It will be used to track whether the specified shipping class product is in the cart.
  • The foreach loop is setup to iterate through the contents of the customer’s cart. It retrieves the cart contents using WC()->cart->get_cart_contents().
  • Within the loop, it checks if the shipping class ID of each product in the cart matches the desired shipping class ID specified in $shipping_class_target.
  • If a product with the specified shipping class is found in the cart, $in_cart is set to true, and the loop is exited using break.
  • If the specified product is in the cart, this code checks if a specific shipping method with ID ‘free_shipping:35’ exists in the $rates array (which contains the available shipping methods). If it exists, it is removed from the array using unset. This effectively hides the ‘Free Shipping:35’ method.
  • Finally, the modified $rates array is returned. The shipping methods that were removed using unset are now hidden, and the modified $rates array is used for the checkout process.


Conclusion

The above code helps you to hide a specific shipping method when a product with a specific shipping class is in the cart. You can tweak the code a little to hide shipping methods for an array of shipping classes too. Some other possibilities to hide specific shipping methods for certain conditions are based on order weight, order total, and product quantity.

Let us know your feedback on how the code was useful or any other queries in the comments section.

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

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Anton
5 months ago

Thank you, it works great.

What if I need to hide 2 shipping methods?
I try to paste 2 times the code with different IDs, but it causes wordpress error.

Saranya
5 months ago
Reply to  Anton

Hi Anton,
Glad to know that the code is working well!
Regarding your requirement, could you please clarify if you intend to hide the shipping methods based on a single shipping class or if you are looking to hide them based on two different shipping classes?

2
0
Would love your thoughts, please comment.x
()
x