Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active March 10, 2021 04:48
Show Gist options
  • Save xadapter/db74411a4ae29aff72c909423357a3da to your computer and use it in GitHub Desktop.
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/
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