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

How to Hide WooCommerce Shipping Methods for Certain Conditions

How to Hide WooCommerce Shipping Methods For Certain Conditions - tychesoftwares.com

The shipping method is a service and charge that a customer sees on checkout while purchasing any item. It will be an essential option for an eCommerce store for a smooth delivery experience.

As an eCommerce solution, WooCommerce also provides shipping methods like Free shipping, Flat rate, and Local Pickup which can be set up for various shipping zones based on the locations programmatically. For example, the Free shipping option is available only for a certain range of postcodes that are near to the store location and the Flat Rate option is available for the other part of the delivery zones.

In this article, we will show you how to programmatically hide the shipping methods in WooCommerce for certain conditions.

Default Shipping Methods
Default Shipping Methods

Location-based shipping options can be easily achieved by the plugin but for certain conditions where location does not play a vital role, it becomes very difficult for the store owners to achieve that. The conditions could be:

  1. Certain Shipping methods like Free Shipping should not be available if the order weight is more than a certain number of lbs or kg.
  2. Some shipping method availability is also based on the number of quantities or the price of the whole order. For example, if the order total is more than $250 then only the Free Shipping method should be available and no paid shipping methods should be available. 
  3. Based on the product total quantity threshold you set, you can hide certain shipping methods.
  4. Various conditions, such as cart totals falling below, above, or falling between specific values, can be set to apply distinct fees for a particular shipping method.
  5. Restrict shipping method only to ‘Flat Rate’ if the shipping address field contains the values ‘PO BOX’ or ‘Parcel Locker’. When other values are entered, all the available shipping methods will be shown.

There can be more such conditions for which the shipping methods need to be shown or hidden during the checkout programmatically, but in this post, we shall explain the above 4 conditions. To achieve this for your WooCommerce store you can add a code snippet or use any of the shipping rate plugins available.

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.

Using a Code Snippet to Hide WooCommerce Shipping Methods Programmatically

Condition 1 – Hide Free Shipping in WooCommerce based on order weight

Let’s take an example of condition 1 which is mentioned above where Free Shipping should not be available if the order weight is more than 5 kgs

/** 
 * Hide free shipping when the order weight is more than 5 kgs.
 * 
 * @param array $rates Array of rates found for the package. 
 * @return array 
 */ 
function ts_hide_free_shipping_for_order_weight( $rates, $package ) { 
    $order_weight = WC()->cart->get_cart_contents_weight(); 
 
    if ( $order_weight > 5 ) { 
        foreach( $rates as $rate_id => $rate_val ) { 
            if ( 'free_shipping' === $rate_val->get_method_id() ) { 
                unset( $rates[ $rate_id ] );
            } 
        } 
    } 
    return $rates; 
} 

add_filter( 'woocommerce_package_rates', 'ts_hide_free_shipping_for_order_weight', 100, 2 );

Here the woocommerce_package_rates filter is being used to modify the calculated rates on the cart. It contains all the shipping rates which will be available once the product is added to the cart. So, we need to unset the required shipping method in WooCommerce, which will be Free shipping in our case from the array returned from this filter if the order weight is more than 5kgs.

Product Page:

Product with 6kg weight
Product with 6kg weight

Cart Page with Shipping Methods:

Free shipping is not available for the product with weight more than 5kg
Free shipping is not available for the product with weight more than 5kg

 

Read related Article: How to add WooCommerce checkout fees based on shipping method & payment method?

Condition 2 – Allow only Free Shipping in WooCommerce based on order total

Another instance of hiding shipping methods on WooCommerce Cart would be allowing only Free Shipping during checkout when the order total is more than $250. This can be achieved by adding the below code in the functions.php file.

/**
 * Hide other shipping methods in WooCommerce except Free Shipping when order total is more than $250.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
function ts_hide_shipping_for_order_total( $rates ) {
  $free = array();

  $order_total = WC()->cart->get_subtotal();
  
  if( $order_total > 250 ) {
    foreach ( $rates as $rate_id => $rate ) {
      if ( 'free_shipping' === $rate->get_method_id() ) {
        $free[ $rate_id ] = $rate;
      }
    }
  }
  return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_for_order_total', 100 );

Here, when the order total is more than $250, then the code will return only the free shipping method. If the order total is less than $250, then all shipping methods are returned.

 

Hide Shipping Rate when Order total is more than $250
Hide other WooCommerce shipping methods except Free Shipping when order total is more than $250

How to Find method ids for specific shipping zone?

In the above examples, we have taken the reference of the Free Shipping method, so the method id used is free_shipping. For altering other shipping methods the method ids are:

Local Pickup: local_pickup
Flat Rate: flat_rate

The get_method_id() function fetches the method id only for the 3 methods for all the shipping zones and not for an individual method of a single shipping zone, so it will hide all the flat rates, local pickup, or free shipping methods available during checkout. But if you want to hide a specific shipping method then the combination of shipping method id and instance id needs to be used.

For example, ‘flat_rate:3’ where 3 is the unique instance id stored for each shipping method for a zone in the database. You can find the instance id of the shipping method by right-clicking on the shipping method and inspecting the element in the developer tool in your browser.

Instance ID

 

Condition 3 – Hide Free Shipping in WooCommerce based on product quantity

The provided code below assists in hiding shipping options according to the total product quantity of the order. When the total product quantity reaches the threshold value of 10, the local pickup option is hidden. Once the product quantity exceeds the threshold value, all shipping options will be displayed.

function ts_show_hide_local_pickup_based_on_quantity( $rates ) {
  $total_product_quantity = WC()->cart->get_cart_contents_count(); // Get the total quantity of items in the cart
  
  $minimum_quantity_for_pickup = 10; // Define the minimum quantity to show Local Pickup, modify this as needed
  
  // If order quantity is less than 10, unset Local Pickup shipping method
  if ( $total_product_quantity < $minimum_quantity_for_pickup ) {
    foreach ( $rates as $rate_id => $rate ) {
      if ( 'local_pickup' === $rate->get_method_id() ) {
        unset( $rates[ $rate_id ] );
      }
    }
  }

  return $rates;
}
add_filter( 'woocommerce_package_rates', 'ts_show_hide_local_pickup_based_on_quantity', 100 );

As per the code, if the product quantity is below 10, we applied the condition to hide Local pickup from the shipping options.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

And if the product quantity is above 10, all the shipping options including the ‘Local pickup’ shipping option will be shown.

 

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

Condition 4 – Calculate shipping rates based on cart subtotal

There may be a case where you would like to set up custom shipping cost calculation on your WooCommerce store that meets the following conditions.

  • For cart subtotals between $50.01 and $109.99, the shipping cost should be 10% of the cart subtotal.
  • For cart subtotals equal to or greater than $110, a maximum flat rate of $11 should be applied.
  • For cart subtotals below $50.01, a minimum flat rate of $5 should be applied.
// Modify shipping rates based on cart subtotal
function ts_shipping_rates_based_on_subtotal($rates) {
    $cart_subtotal = WC()->cart->get_subtotal(); // Get the cart subtotal

    // Custom shipping cost rules
    $minimum_rate = 5;   // Minimum flat rate
    $maximum_rate = 11;  // Maximum flat rate
    $percentage_rate = 0.10; // 10% rate

    // Calculate the shipping cost based on cart subtotal
    if ($cart_subtotal >= 50.01 && $cart_subtotal <= 109.99) {
        foreach ($rates as $rate_key => $rate) {
            if ('flat_rate' == $rate->method_id) {
                $rates[$rate_key]->cost = $cart_subtotal * $percentage_rate;
            }
        }
    } elseif ($cart_subtotal >= 110) {
        foreach ($rates as $rate_key => $rate) {
            if ('flat_rate' == $rate->method_id) {
                $rates[$rate_key]->cost = $maximum_rate;
            }
        }
    } else {
        foreach ($rates as $rate_key => $rate) {
            if ('flat_rate' == $rate->method_id) {
                $rates[$rate_key]->cost = $minimum_rate;
            }
        }
    }

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



Output Image 1: For cart subtotals between $50.01 and $109.99, the shipping cost of 10% is applied to the ‘Flat rate’ shipping option.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

Output Image 2: For cart subtotals equal to or greater than $110, a maximum flat rate of $11 is applied.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares
Output Image 3: For cart subtotals below $50.01, a minimum flat rate of $5 is applied.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

Condition 5 – Hide Shipping Methods except ‘Flat rate’ in WooCommerce based on Address Field Values

The following code snippets allow you to customize the available shipping methods based on the value entered in the shipping address. When a customer enters a shipping address containing ‘PO BOX’ or ‘Parcel Locker,’ the code selectively displays only the ‘Flat Rate’ shipping method. This customization is achieved using the woocommerce_package_rates filter hook.

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

function ts_show_flat_rate( $rates, $package ) {
    // Check if the 'PO BOX' or 'Parcel Locker' is present in the shipping address
    $shipping_address = $package['destination']['address'];

    if ( strpos( $shipping_address, 'PO BOX' ) !== false || strpos( $shipping_address, 'Parcel Locker' ) !== false ) {
        $all_flat_rates = array();
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'flat_rate' === $rate->method_id ) {
                $all_flat_rates[ $rate_id ] = $rate;
                break;
            }
        }
        if ( empty( $all_flat_rates ) ) {
            return $rates;
        } else {
            return $all_flat_rates;
        }
    } else {
        // If 'PO BOX' or 'Parcel Locker' is not present in the shipping address, return all shipping methods
        return $rates;
    }
}

Output 1: If either the ‘PO BOX’ or ‘Parcel Locker’ values are entered in the address fields, the code will show only the ‘Flat rate’ shipping method.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

Output 2: For addresses without ‘PO BOX’ or ‘Parcel Locker’, the code will show all available shipping methods.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

Condition 6: Hide Shipping Methods with No Prices

This customization is helpful when store owners with multiple shipping options want to show only the shipping methods with prices. Hiding shipping methods with zero costs when they are not applicable ensures customers are presented with clear and accurate pricing information. You can implement this logic with any of the above conditions such as order weight, order total, product quantity, etc.

The code snippet below will help to hide shipping methods with zero costs.

function ts_hide_shipping_without_prices( $rates ) {
    foreach ( $rates as $rate_id => $rate ) {
        if ( 0 == $rate->cost ) {
            unset( $rates[ $rate_id ] );
        }
    }
    return $rates;
}

add_filter( 'woocommerce_package_rates', 'ts_hide_shipping_without_prices', 100 );

Let’s take a look at the available shipping options that are been created on the backend.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

When the code is implemented, it hides the shipping methods that don’t have prices, displaying only the shipping options with prices.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

When the code is not implemented, all the available shipping methods with/without prices will be retrieved and shown on the front end.

How to Hide WooCommerce Shipping Methods for Certain Conditions - Tyche Softwares

Using Different WooCommerce Extensions for Shipping Methods

There are multiple paid or free extensions available to set up shipping methods for different conditions. Some of the plugins which are most used are:

Paid Plugins:

  1. Table Rate Shipping for WooCommerce: This plugin has the ability to add multiple rates for a given customer based on a variety of conditions set by admin. These can include shipping destination, cart subtotal, item shipping class, price, weight, and so much more.

  2. Table Rate Shipping by WooCommerce: This plugin extends WooCommerce’s default shipping options giving you highly customizable shipping options.
  3. Advanced Flat Rate Shipping Plugin For WooCommerce: This plugin will help you configure shipping rates on your store based on advanced shipping rules.
  4. WooCommerce Advance Shipping plugin by Jeroen sormani: This plugin allows you to enable Shipping methods during checkout based on set rules. Rules can be set for different cart details, product details or different user details.
  5.  WooCommerce Table Rate Shipping – Flexible Shipping PRO: This plugin allows you to enable shipping methods based on the particular item, cart line item, price, or weight rules.

Free Plugins:

Some of the above plugins also have a free version for them with limited features. They are:

  1. WooCommerce Advanced Free Shipping by Jeroen Sormani: This is a free version of the WooCommerce Advanced Shipping plugin. The only difference with both plugins is that this plugin allows only free shipping methods for set rules and paid plugin allows all shipping methods.
  2. Table Rate Shipping Method for WooCommerce by Flexible Shipping:
    This is a free version of the WooCommerce Table Rate Shipping – Flexible Shipping PRO. It allows setting shipping methods only for the price and weight.

For a detailed explanation of the above-mentioned plugins, you can check this post.

Conclusion

As we have seen in this post that the advanced shipping options can be achieved by simply adding a code snippet on your website programmatically (i.e. hide the shipping method in WooCommerce programmatically) or by using various plugins available. Have you tried any of the conditions mentioned above? If you want to hide a shipping method based on a condition not discussed here, just leave a comment, and we’ll help you with the solution.

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

Share It:

Subscribe
Notify of
20 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Matthew
8 months ago

Is it possible to remove/hide delivery methods that haven’t a price? I’m sending a screenshot for example: https://postimg.cc/WFmKdcBC/583b2b03

Matthew
7 months ago
Reply to  Saranya

I noticed that the code also hides Free Shipping (because it lacks a price), which is confusing for the buyer. Is it possible to hide “empty” delivery methods, excluding Free Shipping, because it must be displayed? Thanks in advance for your help.

Matthew
7 months ago
Reply to  Saranya

Unfortunately, the provided code also hides free delivery and displays the message “No delivery methods found, contact the Administrator”.
I use the WCFM Marketplace plugin, which also provides free shipping for each registered Manufacturer, maybe that’s the problem?

George
1 year ago

Hi, thank you very much for the detailed list.
Do you have code that will hide all shipping methods except australia post when a PO BOX or Parcel Locker is found in the address fields.

I think it may be easy but i am not sure how to do it.

Jim
1 year ago

Hi, I have been looking for a code to exclude bulky shipping classed goods from a Rural delivery only shipping method.
Is there a code to achieve this?

Admin
1 year ago
Reply to  Jim

Hi Jim, You can exclude them either based on their weight or based on the shipping class itself. The very first code snippet in this post under “Using a code snippet” allows to exclude shipping methods based on the weight of the product. You can check that one out.

:Vishal

louis
3 years ago

thanks this is really helpful. the fact that woocommerce calls this “package_rates” is confusing because it suggests you can only modify package rates, but actually you can use it to select whatever shipping method you want according to whatever criteria you need.

Admin
1 year ago
Reply to  louis

I am glad you found this helpful, Louis.

3 years ago

I want to activate local pickup and hide shipping options for specific quantities. Is there a code for that?

louis
3 years ago
Reply to  Joan Houston

this code also works for local pickup. You can use

$rate_val->get_label()

instead of get id to select one specific shipping method by name, and apply whatever criteria you want

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