Ecommerce Shopify WordPress Discussion

PHP Warning when Rearranging WooCommerce Shipping Methods in the Cart

Can anyone tell me why I am getting a PHP warning for the below code? The warning is triggered by this line: foreach($available_shipping_methods as $carrier_id => $carrier){ The error message is: PHP Warning: Invalid argument supplied for foreach() in /home/customer/www/website.com/public_html/wp-content/themes/theme-child/functions.php on line 000 The code: 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(), 'flat_rate' => array(), 'local_pickup' => array(), 'request_shipping_quote' => 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; }
Normally you don't need any code to rearrange shipping methods as this can be done directly in the settings for each shipping Zone, see: Sort shipping methods displayed in WooCommerce Cart and Checkout If you still need to sort shipping methods programmatically from an array of method IDs (slugs), use the following instead: add_filter('woocommerce_package_rates', 'reorganize_shipping_methods_from_method_id', 10, 2 ); function reorganize_shipping_methods_from_method_id( $rates, $package ) { // Here set the desired sorting by shipping method ID in the array $desired_sorting = array( 'wf_shipping_ups', 'flat_rate', 'local_pickup', 'request_shipping_quote' ); $sorted_rates = array(); // Initializing // Loop through desired shipping method sorting array foreach( $desired_sorting as $method_id ) { // Loop through shipping rates available for current shipping package foreach( $rates as $rate_key => $rate ) { if( $rate->method_id === $method_id ) { $sorted_rates[$rate_key] = $rate; unset($rates[$rate_key]); } } } // Merge both arrays (in case you didn't list all shipping methods IDs) return array_merge( $sorted_rates, $rates ); } Code goes in functions.php file of your child theme (or in a plugin). Tested and work. Important: You will have to empty your cart to refresh shipping method cache. Other related threads: Sort WooCommerce shipping options by ascending cost with free shipping at the end Sort WooCommerce shipping options by type and by ascending cost

January 23, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.