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_rates
filter allows us to modify the available shipping methods in WooCommerce. - The function
hide_shipping_when_free_is_available
runs through the list of shipping methods and checks iffree_shipping
is 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.php
file 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.
&