Table of Contents
Coupons or discounts are often used to drive traffic to your online store. WooCommerce built-in features offer an option only to enable free shipping when a specific coupon is applied. But to maintain your profit margin you may have the need to disable free shipping when a coupon is applied automatically on the WooCommerce cart page. So that, you can offer coupons for various products without worrying about the impact on shipping costs.
This post helps you to disable the free shipping method after applying a coupon code in WooCommerce.
Where to Add Custom Code in WooCommerce?
It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.
Preliminary Steps
- You should have a valid working coupon code or else create a new coupon.
- Creating a new coupon is done from WooCommerce >Coupons>Add Coupon. e.g. I have created the coupon ‘flashsale‘.
- Configuration of the current coupon is done from WooCommerce >Coupons>Edit coupon>Coupon data.
2. Do not enable the ‘Allow free shipping’ checkbox as enabling it will enable free shipping for this specific coupon. As we want to do the opposite, which is to hide free shipping when a specific coupon code is applied, we are not enabling the checkbox.
Solution: Disable Free Shipping Method After Applying Coupons in WooCommerce
If you run an online store selling a variety of tech gadgets and smartwatches, then you are bound to offer discount coupons to boost sales. The code below helps you disable ‘Free shipping’ whenever a customer utilizes any coupon for products.
add_filter( 'woocommerce_package_rates', 'ts_hide_free_shipping_method_for_coupons', 10, 2 ); function ts_hide_free_shipping_method_for_coupons( $rates, $package ) { $coupon_codes = array('flashsale'); // <== HERE set your coupon codes $applied_coupons = WC()->cart->get_applied_coupons(); // Applied coupons if( empty($applied_coupons) ) return $rates; // For specific applied coupon codes if( array_intersect($coupon_codes, $applied_coupons) ) { foreach ( $rates as $rate_key => $rate ) { // Targetting "Free shipping" if ( 'free_shipping' === $rate->method_id ) { unset($rates[$rate_key]); // hide free shipping method } } } return $rates; }
Easily Set Fees & Discounts For Payment Gateways
Finding it difficult to manage all payment gateway fees and adjusting your product pricing accordingly?
The Payment Gateway Based Fees & Discounts plugin is designed to set up fees & discounts for each of your payment modes and simplify your payment process.
Output 1
Use Case: When coupon is applied
When the customer selects a smartwatch product and if any coupon is applied, the code will disable the ‘Free shipping‘ method and list other available shipping options.
Output 2
Use Case: When coupon is not applied
If the customer selects the smartwatch product and if no coupon is applied, the code will not disable ‘Free shipping‘ and instead show all available shipping options.
Code Explanation
WooCommerce Filter Function to Hide Free Shipping Method for Coupons
- The code registers a filter hook named ‘woocommerce_package_rates‘. This hook is triggered when WooCommerce calculates the shipping rates for an order package.
- This code defines the ts_hide_free_shipping_method_for_coupons function, which is called when the hook is triggered.
Coupon Code Definition:
- The $coupon_codes array is defined with the coupon code ‘flashsale’. This is the coupon code that will trigger the behavior of hiding the free shipping method. You can modify this code to use your desired coupon code.
Get Applied Coupons:
- The function WC()->cart->get_applied_coupons() retrieves the applied coupon codes from the WooCommerce shopping cart and stores them in the $applied_coupons array.
Check for No Applied Coupons:
- This conditional statement if( empty($applied_coupons) ) checks if there are no applied coupons. If the $applied_coupons array is empty, it means no coupons have been applied, and the code proceeds.
Check for Specific Coupon Codes:
- ‘if( array_intersect($coupon_codes, $applied_coupons) ) {‘
This line checks if there are applied coupons that match the defined coupon codes (in this case, ‘flashsale’). If there is a match, the code proceeds to hide the free shipping method.
Iterate Through Shipping Rates:
- The code uses a foreach loop to iterate through each shipping rate in the $rates array.
Targeting “Free Shipping” Method:
if ( ‘free_shipping’ === $rate->method_id ) {
- Within the loop, this line checks if the shipping method’s ID for the current rate is ‘free_shipping’. This is used to identify the free shipping method.
Hide the Free Shipping Method:
- If the current rate corresponds to the free shipping method, the code removes it from the $rates array using the unset() function. This action effectively hides the free shipping method from the available shipping options.
Return Modified Shipping Rates:
- Finally, the function returns the modified $rates array, which may or may not include the free shipping method, depending on whether the specified coupon code was applied.
Conclusion
The code snippet helps you to disable the ‘Free shipping method’ when a specific coupon code is applied to your WooCommerce store. You can also modify the code to change the cost of a specific shipping method when a specific coupon is applied.
Let us know your feedback on how the code was useful or any other queries in the comments section.