• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Razworks Food Blog

Razworks Food Blog

  • Home
  • Contact Us

Woocommerce Free Shipping default selection

January 28, 2020 by razworks

When entering a coupon code for Free Shipping, the Free shipping option is not selected by default. A quick solution is to add a function that disables all other shipping options, leaving Free Shipping as the only available option.

Add this snippet to your functions file. It hides everything but free_shipping if it’s available and is compatible with Shipping Zones.

/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

Filed Under: Uncategorized

Primary Sidebar

Recent Posts

  • Woocommerce Free Shipping default selection
  • iOS specific CSS
  • Gravity Forms Google Analytics tracking in Tag Manager
  • SSL htaccess redirect
  • Outstanding Volunteer Award

Footer

Breakfast

Breakfast

Dinner

Dinner

Grilling

Grilling

Copyright © 2021 · Genesis Sample on Genesis Framework · WordPress · Log in