function my_only_show_cheapest_shipping_rate( $rates, $package ) {
$cheapest_method = '';
// Loop through shipping rates
if ( is_array( $rates ) ) :
foreach ( $rates as $key => $rate ) :
// Set variables when the rate is cheaper than the one saved
if ( empty( $cheapest_method ) || $rate->cost < $cheapest_method->cost ) :
$cheapest_method = $rate;
endif;
endforeach;
endif;
// Return the cheapest rate when possible
if ( ! empty( $cheapest_method ) ) :
return array( $cheapest_method->id => $cheapest_method );
endif;
return $rates;
}
add_action( 'woocommerce_package_rates', 'my_only_show_cheapest_shipping_rate', 10, 2 );