How to Hide Shipping Methods for Certain Conditions

WooCommerce Hide Shipping Methods for Certain Conditions WooCommerce allows store owners to create shipping methods for their store. The shipping method is a service and charge, which a customer must pay when purchasing an item. A shipping fee is an essential option for any WooCommerce store if you want to have a smooth delivery experience.

Hide Shipping Methods for Certain Conditions

WooCommerce is one of the best eCommerce solutions that provides shipping methods like Free shipping, Flat rate, and Local Pickup. You can set them up for various shipping zones based on the locations. For example, the Free shipping option can be used only for postcodes near the store location and the Flat rate option for other delivery zones.

It is worth mentioning that location-based shipping can be achieved by using a plugin. However, in conditions where the location is not the most important role, it becomes very hard for store owners to achieve that. Some of the conditions could be:

  • Free shipping should not be available if the order weight is more than a certain number of lbs or kg.
  • When shipping method is availability is also based on the number of quantities or the price of that order. For example, if the total is $500, only the Free Shipping method should be available.Shipping methods

Additionally, other conditions for which the shipping methods need to be shown or hidden during the checkout. However, this post explains the conditions I have stated above.

Steps to Hide WooCommerce Shipping Methods for Certain Conditions

Let us use the first condition, which I mentioned above, where free shipping is available if the order weight is more than 7kgs. You can achieve that by following these steps:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to hide WooCommerce shipping methods for specific conditions.
  3. Add the following code to the functions.php file:
/**

 * Hide free shipping when the order weight is more than 10kgs.

 *

 * @param array $rates Array of rates found for the package.

 * @return array

 */

function njengah_hide_free_shipping_for_order_weight( $rates, $package ) {

    $order_weight = WC()->cart->get_cart_contents_weight();

    if ( $order_weight > 7 ) {

        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', 'njengah_hide_free_shipping_for_order_weight', 100, 2 );
  1. This is the outcome:shipping method with condition

How the Code Works

I have used the woocommerce_package_rates filter to modify the calculated rates on the cart. It has all the shipping rates, which will be available once the product is added to the cart. Therefore, this condition will unset the required shipping method, free shipping if the order weight is more than 5kgs.

The other instance was to allow only Free Shipping during checkout when the order total is more than $500. You can achieve this by adding the following code in the functions.php file:

/**

* Hide shipping rates when the order total is more than $250.

* @param array $rates Array of rates found for the package.

* @return array

*/

function njengah_hide_shipping_for_order_total( $rates ) {

$free = array();

$order_total = WC()->cart->get_subtotal();

if( $order_total > 500 ) {

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', 'njengah_hide_shipping_for_order_total', 100 );

When the order total is more than $500, then the array with only free shipping methods are returned from the filter else all the shipping rates are returned.

This is the outcome:free shipping

In the code above, the get_method_id() function fetches the method id only for the three methods for all the shipping zones and not for a particular method of a single shipping zone to hide all the flat rates, local pickup, or free shipping methods available during checkout. However, if you want to hide a specific shipping method, then the combination of shipping method id and instance id needs to be used.

Conclusion

In summary, this post illustrates how you can hide shipping methods for certain conditions that do not involve conditions. However, if you need added functionality, you can use a plugin like the Flexible Shipping Pro for WooCommerce by WP Desk to enable shipping methods based on certain conditions. Add the code snippets I have shared to achieve advanced shipping options.

Similar Articles