Flat Rate Actions and Filters
- Code Snippet for sorting the shipping options by cost.
Code:
add_filter( 'woocommerce_package_rates' , 'xa_sort_shipping_services_by_cost', 10, 2 ); function xa_sort_shipping_services_by_cost( $rates, $package ) { if ( ! $rates ) return; $rate_cost = array(); foreach( $rates as $rate ) { $rate_cost[] = $rate->cost; } // using rate_cost, sort rates. array_multisort( $rate_cost, $rates ); return $rates; }
Description: Default, it will display shipping methods based on the order or created date based on the setting saved. After adding the above filter it will display the shipping based on the shipping price.
- Change the tax postfix text
Code:
/** Change the include text */ add_filter( 'gettext', function( $translation, $text, $domain ) { if ( $domain == 'woocommerce' ) { if ( $text == 'includes' ) { $translation = 'INC.'; } } return $translation; }, 10, 3 );
- Allow specific shipping methods by Applying the highest master setting
Code:
add_filter('show_shipping_in_highest', 'show_shipping_in_highest_callback'); function show_shipping_in_highest_callback($ids_array){ return array('local_pickup:3', 'free_shipping:2'); //Add your shipping id here }
Add the above screenshot value in the array to display the force with the highest shipping method.
- Change the order of shipping methods by it's type
Code:
/* Filter to change the order of defaul shipping and our advanced shipping */ add_filter('woocommerce_package_rates', 'dotsotre_sort_shipping_methods', 10, 2); function dotsotre_sort_shipping_methods($available_shipping_methods, $package) { // Arrange shipping methods as per your requirement $sort_order = array( 'advanced_flat_rate_shipping' => array(), 'free_shipping' => array(), 'local_pickup' => array(), '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; }