So you have a shop based on WordPress and WooCommerce and you need to change the way that shipping rates are presented.
For example, you offer free shipping when the customer reaches a specific cart amount, and you would like to hide all other shipping rates. By default, they will still be present at checkout.
There is a quick code snippet for this. You can add it to a functions.php in your theme.
function woo_hide_shipping_when_free_is_available( $rates ) {
$free = array();
/* Run all rates and if the they are free add them to an array
*/
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
}
}
//return the new rates array
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'woo_hide_speedy_to_office_when_free', 100 );
This code above will remove all paid shipping rates when there is even one free shipping available.