Last active
October 4, 2019 09:34
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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