Skip to content

Instantly share code, notes, and snippets.

@PluginHive
Last active October 4, 2019 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PluginHive/ccc1f3e0251f9056dc911e06edd30c92 to your computer and use it in GitHub Desktop.
Save PluginHive/ccc1f3e0251f9056dc911e06edd30c92 to your computer and use it in GitHub Desktop.
Code Snippet to rearrange shipping methods in cart page. Multi-Carrier Shipping Plugin for WooCommerce: https://www.pluginhive.com/product/multiple-carrier-shipping-plugin-woocommerce/
/**
* Snippet to rearrange shipping methods in cart page
* Created at : 18 July 2019
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/PluginHive/ccc1f3e0251f9056dc911e06edd30c92
**/
add_filter('woocommerce_package_rates', 'ph_sort_shipping_methods', 10, 2);
function ph_sort_shipping_methods($available_shipping_methods, $package)
{
// Arrange shipping methods as per your requirement
$sort_order = array(
'usps_G3' => array(), // Multicarrier Shipping Value Afetr ":"
'fedex_G2' => array(),
'flatrate_G5' => array(),
'stamps_usps_G4' => array(),
'ups_G1' => array(),
);
// unsetting all methods that needs to be sorted
foreach($available_shipping_methods as $carrier_id => $carrier){
$carrier_array = explode(":",$carrier_id);
$carrier_code = isset($carrier_array[1]) ? $carrier_array[1] : "";
if( !empty($carrier_code) && array_key_exists($carrier_code,$sort_order) )
{
$sort_order[$carrier_code][$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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment