Hide all shipping method but free shipping

In the user experience, you should automatically apply the free shipping method whenever possible, which helps customers feel more comfortable with your purchase.

The code snippet below will help you do this:

function only_show_free_shipping_when_available( $rates, $package ) {
 $new_rates = array();
 foreach ( $rates as $rate_id => $rate ) {
  // Only modify rates if free_shipping is present.
  if ( 'free_shipping' === $rate->method_id ) {
   $new_rates[ $rate_id ] = $rate;
   break;
  }
 }
if ( ! empty( $new_rates ) ) {
  //Save local pickup if it's present.
  foreach ( $rates as $rate_id => $rate ) {
   if ('local_pickup' === $rate->method_id ) {
    $new_rates[ $rate_id ] = $rate;
    break;
   }
  }
  return $new_rates;
 }
return $rates;
}
add_filter( 'woocommerce_package_rates', 'only_show_free_shipping_when_available', 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *