Search code examples
phpwordpresswoocommercecouponshipping-method

Hide Free shipping for specific applied coupon when discounted subtotal is below 75


I am trying to create a very specific type of coupon code.

  • If a customer enters "allthings30" they get 30% off.

  • However if the order is over £75, They get free shipping as well.

Now the website, already has free shipping in place, but I want those disabled if the order is below £75, only when this code is applied.

Using other questions on stackoverflow, I have managed to create the code, but it is being applied to every singe couple.

How do I apply this code to only the "allthings30" coupon. Any help is greatly appropriated.

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 33, 38 );
function coupons_removes_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $min_subtotal      = 75; // Minimal subtotal allowing free shipping
    $coupon_code    = 'allthings30'; // The required coupon code

    // Get needed cart subtotals
    $subtotal_excl_tax = WC()->cart->get_subtotal();
    $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
    $discount_excl_tax = WC()->cart->get_discount_total();
    $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();

    // Calculating the discounted subtotal including taxes
    $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;

    $applied_coupons = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() );

    if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){
            // Targeting "Free shipping"
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

Solution

  • You are very near… The following will do the job:

    add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 33, 38 );
    function coupons_removes_free_shipping( $rates, $package ){
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return $rates;
    
        $min_subtotal   = 75; // Minimal subtotal allowing free shipping
        $coupon_code    = 'summer'; // 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;
    }
    

    Don't forget after saving that code to your theme's functions.php file to refresh your shipping rates in Admin shipping rates settings, disabling and save any shipping method and re-enable and save it back…