-
-
Save boywondercreative/e0bf3bb829c7726cfb11286ecf7c7ea8 to your computer and use it in GitHub Desktop.
How to change default shipping method programmatically.
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