To hide all other shipping methods when free shipping is available 2.0
October 21, 2024 | Mashiur Rahman
How to Hide All Shipping Methods When Free Shipping is Available in WooCommerce
Shipping plays a crucial role in the customer’s purchase decision. Offering free shipping can significantly reduce cart abandonment and enhance user experience. However, when both free and paid shipping options are displayed at checkout, it can confuse customers and may lead them to question which option is better. To avoid this confusion, many online store owners prefer to hide all shipping methods when free shipping is available.
If you’re running a WooCommerce store and want to automatically hide all other shipping methods when free shipping is available, you’re in the right place. In this tutorial, I’ll show you how to achieve this using a simple snippet of code that you can add to your theme’s functions.php file.
Why Hide Other Shipping Methods?
Before diving into the solution, let’s first understand why you might want to hide other shipping methods when free shipping is available:
- Reduce Confusion for Customers: Displaying multiple shipping options, especially when free shipping is available, can confuse customers. They might wonder why there are paid options when free shipping is also listed.
- Improve Checkout Experience: A streamlined checkout process encourages faster decision-making. Fewer distractions lead to fewer abandoned carts.
- Boost Sales Conversions: By highlighting free shipping as the primary or only available option, customers may feel they’re getting a better deal, which can lead to higher conversion rates.
Now, let’s get started with the solution.
Step-by-Step Guide to Hiding Other Shipping Methods
This method involves adding a custom function to your functions.php file, which is part of your theme. When free shipping is available for a customer’s cart, the function will automatically hide all other shipping options.
1. Access Your WordPress Theme’s Functions File
To access the functions.php file, follow these steps:
- Log in to your WordPress dashboard.
- Navigate to Appearance > Theme File Editor.
- On the right-hand side, you’ll see a list of theme files. Scroll down and select
Theme Functions (functions.php).
2. Add the Custom Code to Hide Shipping Methods
Next, add the following code to your functions.php file:
add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 100, 2 ); function hide_shipping_when_free_is_available( $rates, $package ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( ‘free_shipping’ === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return !empty( $free ) ? $free : $rates;
}
This code works by filtering the available shipping methods. It loops through all available shipping options and checks if free shipping is available. If it detects free shipping, it hides all other shipping methods and shows only the free shipping option.
3. Save the Changes
After adding the code, scroll down and click the Update File button to save your changes.
4. Test the Changes
It’s essential to test your checkout process after implementing the code. Add items to your cart and proceed to checkout to see if only free shipping is displayed when it’s available.
How the Code Works
Let’s break down the code to understand what it does:
- The
woocommerce_package_ratesfilter allows us to modify the available shipping methods in WooCommerce. - The function
hide_shipping_when_free_is_availableruns through the list of shipping methods and checks iffree_shippingis available. - If it finds free shipping, it creates an array containing only the free shipping method.
- Finally, it returns either the free shipping method (if available) or all available shipping methods (if free shipping is not applicable).
When to Use This Solution
This solution is perfect for stores that regularly offer free shipping based on certain conditions, such as:
- Cart total threshold (e.g., free shipping for orders over $50).
- Coupon code-based free shipping.
- Location-based free shipping.
By implementing this, you ensure that your customers are presented with a simple and clean shipping option that enhances their buying experience.
Things to Keep in Mind
- Backup First: Always create a backup of your
functions.phpfile before making any changes. This ensures that you can restore your site if anything goes wrong. - Child Theme: If you’re working with a theme that gets regular updates, consider creating a child theme and adding the code there. This will prevent your changes from being overwritten during theme updates.
Final Thoughts
Offering free shipping is a proven strategy to boost sales, but it’s essential to ensure the checkout process remains straightforward. Hiding other shipping methods when free shipping is available helps avoid confusion and creates a more seamless experience for your customers.
By following this simple method, you can improve your WooCommerce store’s user experience and increase conversions with just a few lines of code.
&