What can we help you with?

Code Snippet to Rearrange Shipping Methods in cart page

 

The following code snippet helps you to rearrange the shipping methods in the cart page.

Add the following code to functions.php file or in any file relevant.

Add_filter('woocommerce_package_rates', 'wf_sort_shipping_methods', 10, 2);

function wf_sort_shipping_methods($available_shipping_methods, $package)
{
// Arrange shipping methods as per your requirement
$sort_order = array(
'wf_shipping_ups' => array(),
'wf_shipping_usps' => array(),
'free_shipping' => array(),
'local_pickup' => array(),
'legacy_flat_rate' => array(), 
);

// unsetting all methods that needs to be sorted
foreach($available_shipping_methods as $carrier_id => $carrier){
$carrier_name = current(explode(":",$carrier_id));
if(array_key_exists($carrier_name,$sort_order)){
$sort_order[$carrier_name][$carrier_id] = $available_shipping_methods[$carrier_id];
unset($available_shipping_methods[$carrier_id]);
}
}

// adding methods again according to sort order array
foreach($sort_order as $carriers){
$available_shipping_methods = array_merge($available_shipping_methods,$carriers);
}
return $available_shipping_methods;
}

In the above code, the arrangement of the shipping methods are in the order :

1. UPS
2. USPS
3. Free Shipping
4. Local Pickup
5. Flat Rate

So when the code is applied, the cart page will look like

Cart

Previous WooCommerce – Sort shipping options / methods / services by shipment cost
Next Skip products from shipping package (Code Snippet)
You must be logged in to post a comment.