Last active
March 10, 2021 04:48
-
-
Save xadapter/db74411a4ae29aff72c909423357a3da to your computer and use it in GitHub Desktop.
Snippet to change the default WooCommerce shipping method. Supports PluginHive Shipping Plugins: https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/
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
function wf_default_shipping_method( $method ) { | |
$the_cheapest_cost = 1000000; | |
$packages = WC()->shipping()->get_packages()[0]['rates']; | |
foreach ( array_keys( $packages ) as $key ) { | |
if ( ( $packages[$key]->cost > 0 ) && ( $packages[$key]->cost < $the_cheapest_cost ) ) { | |
$the_cheapest_cost = $packages[$key]->cost; | |
$method_id = $packages[$key]->id; | |
} | |
} | |
return $method_id; | |
} | |
add_filter( 'woocommerce_shipping_chosen_method', 'wf_default_shipping_method', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment