Sort Shipping Options From Cheapest to Most Expensive
Lists the available shipping methods in price order on the cart and checkout, so the cheapest option is always shown first.
Opens WPIDE on
Code
add_filter( 'woocommerce_package_rates', function ( $rates ) {
if ( count( $rates ) < 2 ) {
return $rates;
}
uasort( $rates, function ( $a, $b ) {
return (float) $a->get_cost() <=> (float) $b->get_cost();
} );
return $rates;
}, 100 );
Notes
Free methods come first; pair it with "Select the Cheapest Shipping Method by Default" to preselect the cheapest one too.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.