What can we help you with?

WooCommerce – Sort shipping options / methods / services by shipment cost

In general, WooCommerce does not sort the shipping methods/ services by default while it is displayed on the cart/checkout page. The below code is used to sort the shipping options by cost.

Code Snippet for sorting the shipping options by cost

The below code snippet will help to sort the shipping options based on cost.

add_filter( 'woocommerce_package_rates' , 'xa_sort_shipping_services_by_cost', 10, 2 );
function xa_sort_shipping_services_by_cost( $rates, $package ) 
{
if ( ! $rates ) return $rates;

$rate_cost = array();
foreach( $rates as $rate ) {
$rate_cost[] = $rate->cost;
}

// using rate_cost, sort rates.
array_multisort( $rate_cost, $rates );

return $rates;
}

Suppose the unsorted format appears like this :

WooCommerce Shipping Services

Upon using the code, the  sorted list will look like :

WooCommerce Shipping Services Sorted.

 

 

Previous Code Snippet for Splitting the Cart Based on the Dry Ice Products
Next Code Snippet to Rearrange Shipping Methods in cart page
You must be logged in to post a comment.