Skip to content

Instantly share code, notes, and snippets.

@boywondercreative
Forked from xadapter/functions.php
Created March 13, 2019 22:44
Show Gist options
  • Save boywondercreative/e0bf3bb829c7726cfb11286ecf7c7ea8 to your computer and use it in GitHub Desktop.
Save boywondercreative/e0bf3bb829c7726cfb11286ecf7c7ea8 to your computer and use it in GitHub Desktop.
How to change default shipping method programmatically.
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