Ilias Kypraios
Ilias Kypraios

Ilias Kypraios

WooCommerce: Hide Other shipping methods if free shipping is available

WooCommerce: Hide Other shipping methods if free shipping is available

Ilias Kypraios's photo
Ilias Kypraios
·May 15, 2022·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

In case you need to hide other shipping methods if you offer free shipping through the default WooCommerce functionality you may need to hide the rest shipping methods if the order is eligible for free shipping.

Just add the code below to a site-specific plugin or to the functions.php file of your theme.

// hide other shipping methods if free shipping available
function ik_hide_free_shipping( $rates ) {
  $free = array();
  foreach ( $rates as $rate_id => $rate ) {
    if ( 'free_shipping' === $rate->method_id ) {
    $free[ $rate_id ] = $rate;
    break;
    }
  }
  return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'ik_hide_free_shipping', 100 );
 
Share this