Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
00 Days
00 Hours
00 Minutes
00 Seconds

How to Sort WooCommerce Shipping Options by Ascending Cost with Free Shipping at the End?

The code given below reorders WooCommerce shipping methods, arranges the rates from low to high order cost, and also ensures that free shipping methods are displayed at the end in the sorted shipping options.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_filter( 'woocommerce_package_rates' , 'ts_sort_shipping_method_by_cost_zero_empty_cost_last', 10, 2 );
function ts_sort_shipping_method_by_cost_zero_empty_cost_last( $rates, $package ) {
if ( empty( $rates ) || ! is_array( $rates ) ) return;
// Sort shipping methods based on cost
uasort( $rates, function ( $a, $b ) {
if ( $a == $b ) return 0;
return ( $a->cost < $b->cost ) ? -1 : 1;
} );
$free = $zero = []; // Initializing
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ) {
// For "free shipping" methods
if ( 'free_shipping' === $rate->method_id ) {
// set them on a separated array
$free[$rate_key] = $rate;
// Remove "Free shipping" method from $rates array
unset($rates[$rate_key]);
}
// For other shipping rates with zero cost
elseif ( $rate->cost == 0 ) {
// set them on a separated array
$zero[$rate_key] = $rate;
// Remove the current method from $rates array
unset($rates[$rate_key]);
}
}
// Merge zero cost and "free shipping" methods at the end if they exist
return ! empty( $free ) || ! empty( $zero ) ? array_merge($rates, $zero, $free) : $rates;
}
add_filter( 'woocommerce_package_rates' , 'ts_sort_shipping_method_by_cost_zero_empty_cost_last', 10, 2 ); function ts_sort_shipping_method_by_cost_zero_empty_cost_last( $rates, $package ) { if ( empty( $rates ) || ! is_array( $rates ) ) return; // Sort shipping methods based on cost uasort( $rates, function ( $a, $b ) { if ( $a == $b ) return 0; return ( $a->cost < $b->cost ) ? -1 : 1; } ); $free = $zero = []; // Initializing // Loop through shipping rates foreach ( $rates as $rate_key => $rate ) { // For "free shipping" methods if ( 'free_shipping' === $rate->method_id ) { // set them on a separated array $free[$rate_key] = $rate; // Remove "Free shipping" method from $rates array unset($rates[$rate_key]); } // For other shipping rates with zero cost elseif ( $rate->cost == 0 ) { // set them on a separated array $zero[$rate_key] = $rate; // Remove the current method from $rates array unset($rates[$rate_key]); } } // Merge zero cost and "free shipping" methods at the end if they exist return ! empty( $free ) || ! empty( $zero ) ? array_merge($rates, $zero, $free) : $rates; }
add_filter( 'woocommerce_package_rates' , 'ts_sort_shipping_method_by_cost_zero_empty_cost_last', 10, 2 );
function ts_sort_shipping_method_by_cost_zero_empty_cost_last( $rates, $package ) {
    if ( empty( $rates ) || ! is_array( $rates ) ) return;

    // Sort shipping methods based on cost
    uasort( $rates, function ( $a, $b ) {
        if ( $a == $b ) return 0;
        return ( $a->cost < $b->cost ) ? -1 : 1;
    } );

    $free = $zero = []; // Initializing

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // For "free shipping" methods
        if ( 'free_shipping' === $rate->method_id ) {
            // set them on a separated array
            $free[$rate_key] = $rate;

            // Remove "Free shipping" method from $rates array
            unset($rates[$rate_key]);
        } 
        // For other shipping rates with zero cost
        elseif ( $rate->cost == 0 ) {
            // set them on a separated array
            $zero[$rate_key] = $rate;

            // Remove the current method from $rates array
            unset($rates[$rate_key]);
        }
    }

    // Merge zero cost and "free shipping" methods at the end if they exist
    return ! empty( $free ) || ! empty( $zero ) ? array_merge($rates, $zero, $free) : $rates;
}

Output

Here is the output of the sorted shipping options arranged from low to high cost, followed by free or zero-cost options always at the end of the list.

How to Sort WooCommerce Shipping Options by Ascending Cost with Free Shipping at the End?

The below output shows that the default options of how it was arranged before applying the code.

How to Sort WooCommerce Shipping Options by Ascending Cost with Free Shipping at the End?


You can also try to clearly show the shipping costs as ‘0’ for free shipping methods. Look into our post that will help you to display $0.00 amount for free shipping rates in WooCommerce.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of


0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.