If you create rules for WooCommerce shipping costs, you’ll often end up with situations where customers can choose either paying $5.00 or $10.00 for shipping.
You can still hope some clients will choose to pay the higher rate, but in most situation the choice is useless.
There’s a code snippet available at WooCommerce documentation to hide other shipping options when free shipping is available.
Filter woocommerce_package_rates
I want to hide other options if a cheaper rate is available.
Add the following code to your themes functions.php:
add_filter( 'woocommerce_package_rates', 'show_only_lowest_shipping_rate' , 10, 2 ); /** * Show only lowest shipping rate */ function show_only_lowest_shipping_rate( $rates, $package ) { // Only modify rates if more than one rates are available if ( isset( $rates ) && count( $rates ) > 1 ) { $lowest_rate = null; $lowest_rate_key = null; $lowest_rate_cost = 1000000; foreach ( $rates as $rate_key => $rate ) { if( $rate->cost < $lowest_rate_cost ){ $lowest_rate_cost = $rate->cost; $lowest_rate_key = $rate_key; $lowest_rate = $rate; } } // return array only containing the lowest rate if( isset( $lowest_rate ) ) return array( $lowest_rate_key => $lowest_rate ); } return $rates; }
Fixed:
count( $rates > 1 ) should be: count( $rates) > 1 )
Thanks Chad!