Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Hide Free Shipping for Applied Coupon and Subtotal Below a Certain Amount?

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.

How to Hide Free Shipping for Applied Coupon and Subtotal Below a Certain Amount?

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x