Now, It is easy to customize the behavior of shipping methods using these simple code snippets. Either hide or show free shipping based on certain conditions. This code will hide the free shipping method when a specific coupon is applied and the minimum subtotal requirement is not met.
add_filter( 'woocommerce_package_rates', 'ts_coupons_removes_free_shipping', 33, 38 ); function ts_coupons_removes_free_shipping( $rates, $package ){ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return $rates; $min_subtotal = 75; // Minimal subtotal allowing free shipping $coupon_code = 'christmas'; // The required coupon code // Get cart subtotals and applied coupons $cart = WC()->cart; $subtotal_excl_tax = $cart->get_subtotal(); $subtotal_incl_tax = $subtotal_excl_tax + $cart->get_subtotal_tax(); $discount_excl_tax = $cart->get_discount_total(); $discount_incl_tax = $discount_excl_tax + $cart->get_discount_tax(); $applied_coupons = $cart->get_applied_coupons(); // Get applied coupons array // Calculating the discounted subtotal including taxes $disc_subtotal_incl_tax = $subtotal_incl_tax - $discount_incl_tax; if( in_array( strtolower($coupon_code), $applied_coupons ) && $disc_subtotal_incl_tax < $min_subtotal ){ foreach ( $rates as $rate_key => $rate ){ // Targeting "Free shipping" if( 'free_shipping' === $rate->method_id ){ unset($rates[$rate_key]); } } } return $rates; }
Output
The above code removes the free shipping option based on these conditions if a specific coupon (‘christmas’) is applied and the discounted subtotal is below $75.
Alternative to the above scenario, you can also set all shipping cost to zero for a free shipping coupon in WooCommerce that will help in clearance sales or during seasonal offers.