Search the site:

How to Hide WooCommerce Shipping Methods For Certain Conditions

The shipping method is a service and charge which 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. For example, Free shipping option is available only for a certain range of postcodes which are near to the store location and Flat Rate option is available for the other part of the delivery zones.

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 Free Shipping method should be available and no paid shipping methods should be available. 

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

Using a code snippet

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. You can achieve this by adding the below code in the functions.php file of the currently active theme.

Note: These changes can be overwritten with an update of the current theme, so it is recommended to create a child theme and add the required code snippets in the functions.php file of the child theme. Or you can use Code Snippets plugin to add custom snippets.

/** 
 * Hide free shipping when the order weight is more than 10kgs. 
 * 
 * @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 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

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 shipping rates 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 an array with only free shipping methods are returned from the filter else all the shipping rates are returned.

Hide Shipping Rate when Order total is more than $250
Hide Shipping Rate when Order total is more than $250

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 inspect the element in the developer tool in your browser.

Instance ID

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 by WooCommerce: This plugin extends WooCommerce’s default shipping options giving you highly customizable shipping options.
  2. Advanced Flat Rate Shipping Plugin For WooCommerce: This plugin will help you configure shipping rates on your store based on advanced shipping rules.
  3. 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.
  4. Flexible Shipping Pro for WooCommerce by WP Desk: This plugin allows you to enable shipping method 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 the plugins is that this plugin allows only free shipping method for set rules and paid plugin allows all shipping methods.
  2. Flexible Shipping for WooCommerce by WP Desk: This is a free version of the Flexible Shipping Pro for WooCommerce plugin. It allows setting shipping methods only for the price and weight.

For detail explanation of the above mentioned plugins you can check this post.

Conclusion

As we have seen in this post that the advance shipping options can be achieved by simply adding a code snippet on your website or by using various plugins available. So you can try out the given code snippet or experiment with any of the plugins and let us know if this is beneficial for your WooCommerce Store.

Browse more in: WooCommerce Tutorials, Code Snippets, WooCommerce How Tos, WooCommerce Shipping
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Brenda
1 year ago

I am hoping to do something like this on my website with a minimum flat rate of $5, maximum of $11, and 10% of cart subtotal between $50.01 and $109.99. Currently, with the default woocommerce shipping, it is pulling from the cart total and I want it to be based on cart subtotal. What code would I use to achieve this?

Rafal
Rafal
11 months ago

Hi, this code works perfectly… but only for non-taxes products. I don’t why, but “get_subtotal()” doesn’t include taxes. Do you have any idea how to modify this snippet to work with taxable products?

louis
louis
3 months ago
Reply to  Rafal

a subtotal is the amount of money before taxes, so it’s normal it doesn’t include taxes, if you want to have the amount for tax from the subtotal, you can use

get_subtotal_tax()

for more cart methods, you can visit the cart class documentation here

Joan Houston
10 months ago

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

louis
louis
3 months 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

louis
louis
3 months 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.

from purchased

Subscribe for more offers

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