Skip to content

Instantly share code, notes, and snippets.

@bublick
Last active February 10, 2021 06:07
Show Gist options
  • Save bublick/3a04e81792190c5f8abe277734ce670d to your computer and use it in GitHub Desktop.
Save bublick/3a04e81792190c5f8abe277734ce670d to your computer and use it in GitHub Desktop.
Hide shipping rates when free shipping is available
/**
* WordPress + WooCommerce
* Hide shipping rates when free shipping is available
*/
function lis_woo_show_free_shipping_only( $rates, $package ) {
$new_shipping_rates = array();
foreach ( $rates as $rate_id => $rate ) {
// Fill new shipping rates array only if free shipping is available.
if ( 'free_shipping' === $rate->method_id ) {
$new_shipping_rates[ $rate_id ] = $rate;
break;
}
}
if ( ! empty( $new_shipping_rates ) ) {
return $new_shipping_rates;
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'lis_woo_show_free_shipping_only', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment