Woocommerce - Hide Other Shipping Methods When Free Shipping Is Available

WooCommerce: Hide other shipping methods except local pickup when free shipping is available

To hide all shipping methods except local pickup and free shipping methods when free shipping is available, use the following:

add_filter( 'woocommerce_package_rates', 'hide_shipping_except_local_when_free_is_available', 100 );
function hide_shipping_except_local_when_free_is_available($rates) {
$free = $local = array();

foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
} elseif ( 'local_pickup' === $rate->method_id ) {
$local[ $rate_id ] = $rate;
}
}
return ! empty( $free ) ? array_merge( $free, $local ) : $rates;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related:

  • How to hide free shipping when there is shipping costs in WooCommerce
  • Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3
  • WooCommerce - Hide other shipping methods when FREE SHIPPING is available

Hide shipping methods based on availability of other shipping methods in WooCommerce

  • $rate->method_id will be equal to local_pickup, free_shipping, flat_rate, etc..

  • while $rate_id will be equal to local_pickup:1, free_shipping:2, etc..

So either you use it like this:

function filter_woocommerce_package_rates( $rates, $package ) { 
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
// Checks if a value exists in an array, multiple can be added, separated by a comma
if ( in_array( $rate->method_id, array( 'local_pickup', 'free_shipping' ) ) ) {
unset( $rates['flat_rate:28'] );
}
}

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

OR like this:

function filter_woocommerce_package_rates( $rates, $package ) { 
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
// Checks if a value exists in an array, multiple can be added, separated by a comma
if ( in_array( $rate_id, array( 'local_pickup:1', 'free_shipping:2' ) ) ) {
unset( $rates['flat_rate:28'] );
}
}

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

Code goes in functions.php file of the active child theme (or active theme). Tested and works in Wordpress 5.8.1 & WooCommerce 5.8.0

Don't forget to empty your cart to refresh shipping cached data.

How to hide free shipping when there is shipping costs in WooCommerce

Try the following, that will hide Free shipping if any shipping method with a cost is available:

add_filter( 'woocommerce_package_rates', 'hide_free_shipping_for_available_rate_costs', 100, 2 );
function hide_free_shipping_for_available_rate_costs( $rates, $package ) {
$has_cost = false;

foreach ( $rates as $rate_key => $rate ) {
if( $rate-cost > 0 ) {
$has_cost = true;
}
if ( 'free_shipping' === $rate->method_id ) {
$free_rate_key = $rate_key;
}
}

if ( $has_cost && isset($free_rate_key) ){
unset($rates[$free_rate_key]);
}
return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:

  1. This code is already saved on your function.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.

    You are done and you can test it.

Related: WooCommerce - Hide other shipping methods when FREE SHIPPING is available

Disable only flat rate shipping method when free shipping is available in Woocommerce

As you have 3 shipping methods, 1 free shipping, 1 flat rate and 1 custom '2588', it's possible to hide the flat rate shipping method when free shipping is available instead:

add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 1000, 2 );
function free_shipping_disable_flat_rate( $rates, $package ) {
// Here your free shipping rate Id
$free_shipping_rate_id = 'free_shipping:12';

// When your Free shipping method is available
if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
// Loop through shipping methods rates
foreach ( $rates as $rate_key => $rate ) {
// Removing "Flat rate" shipping method
if ( 'flat_rate' === $rate->method_id ){
unset($rates[$rate_key]);
}
}
}
return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:

  1. This code is already saved on your function.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.

    You are done and you can test it.

Hide specific shipping method when another specific shipping method is active in WooCommerce

Updated - added a missing ;

First you need to get the correct shipping rate Ids for "Delivery by express mail ($35)" and also for "Free shipping by express mail".

This can be do when inspecting the generated html code (with your browser inspector) on your shipping methods radio buttons. so you will see something like:

Sample Image

where the shipping method rate Id is free_shipping:10 (the radio button "value").

The code below will hide "Paid delivery by express mail (cost $35)" when "Free delivery by express mail" is available $(you will have to set both shipping rate Ids)*:

add_filter( 'woocommerce_package_rates', 'customizing_shipping_methods', 10, 2 );
function customizing_shipping_methods( $rates, $package ) {
$flat_express_delivery_rate_id = 'flat_rate:12'; // Paid delivery by express mail (cost $35)
$free_express_delivery_rate_id = 'free_shipping:10'; // Free delivery by express mail

// When Free delivery by express mail is available
if( isset($rates[$free_express_delivery_rate_id]) ) {
// Remove Paid delivery by express mail (cost $35)
unset($rates[$flat_express_delivery_rate_id]);
}
return $rates;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Refresh shipping methods

Once you have saved this code, empty cart to refresh shipping methods, or if needed, go to shipping areas settings, then disable / save and re-enable / save any shipping methods from a shipping zone.

Hide other shipping options when Free shipping is available for one zone only in Woocommerce

The following code will hide all other shipping methods when free shipping is available for a specific Zone (you will define in the function the targeted zone ID or Zone name):

add_filter( 'woocommerce_package_rates', 'free_shipping_hide_others_by_zone', 100, 2 );
function free_shipping_hide_others_by_zone( $rates, $package ) {

// HERE define your shipping zone ID OR the shipping zone name
$defined_zone_id = '';
$defined_zone_name = 'Europe';

// Get The current WC_Shipping_Zone Object
$zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$zone_id = $zone->get_id(); // The zone ID
$zone_name = $zone->get_zone_name(); // The zone name

$free = array(); // Initializing

// Loop through shipping rates
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id && ( $zone_id == $defined_zone_id || $zone_name == $defined_zone_name ) ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}

Code goes in function.php file of the active child theme (or active theme). Tested and work.

Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3

Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
// HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
$flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );

$free = $flat2 = array();
foreach ( $rates as $rate_key => $rate ) {
// Updated Here To
if ( in_array( $rate->id, $flat_rates_express ) )
$flat2[ $rate_key ] = $rate;
if ( 'free_shipping' === $rate->method_id )
$free[ $rate_key ] = $rate;
}
return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested on WooCommerce 3 and works.

Refresh the shipping caches:

1) First empty your cart.

2) This code is already saved on your function.php file.

3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.



Related Topics



Leave a reply



Submit