Do you want to hide other shipping methods when ‘Free Shipping’ is available in WooCommerce?
By default, WooCommerce shows all shipping methods that match the customer’s criteria and cart contents. Free shipping is displayed alongside flat rates and other available shipping methods even if the customer is qualified for free shipping.
Therefore, to enhance the shopping cart experience for customers and prevent confusion, it’s beneficial to automatically hide other shipping methods when free shipping is available in WooCommerce.
Below code snippet instructs WooCommerce to hide other shipping methods when free shipping is available.
/**
* 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 maverick_hide_other_shipping_when_free_shipping_is_available($rates, $package)
{
$free_shipping = array();
// Loop through available rates
foreach ($rates as $rate_id => $rate) {
// Check if free shipping is available
if ('free_shipping' === $rate->method_id) {
$free_shipping[$rate_id] = $rate;
break; // Exit loop when free shipping is found
}
}
// Return only free shipping if available
return !empty($free_shipping) ? $free_shipping : $rates;
}
add_filter('woocommerce_package_rates', 'maverick_hide_other_shipping_when_free_shipping_is_available', 100, 2);
Benefits of Automatically Hiding Other Shipping Methods when ‘Free Shipping’ is Available in WooCommerce
- Improved User Experience: Automatically displaying only the free shipping option simplifies the checkout process for customers.
- Reduced Confusion: Customers won’t be confused by multiple shipping options when free shipping is available.
- Streamlined Checkout: A single shipping option speeds up the checkout process, potentially reducing cart abandonment rates.