Woocommerce default shipping selection
This function makes Woocommerce automatically select the lowest cost shipping by default
function ea_default_shipping_method( $method ) {
$_cheapest_cost = 100000;
$packages = WC()->shipping()->get_packages()[0]['rates'];
foreach ( array_keys( $packages ) as $key ) {
if ( ( $packages[$key]->cost > 0 ) && ( $packages[$key]->cost < $_cheapest_cost ) ) {
$_cheapest_cost = $packages[$key]->cost;
$method_id = $packages[$key]->id;
}
}
return $method_id;
}
add_filter( 'woocommerce_shipping_chosen_method', 'ea_default_shipping_method', 10 );