1

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;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Take a look at this, this can certainly help you to further adjust your code https://stackoverflow.com/questions/57963173/disable-cart-needs-payment-for-a-specific-coupon-code-in-woocommerce & https://stackoverflow.com/questions/56094263/allow-specific-products-to-be-purchase-only-if-a-coupon-is-applied-in-woocommerc – 7uc1f3r May 05 '20 at 10:17
  • Hi there, thanks for the reply. I am very new to php. The code above is from this post https://stackoverflow.com/questions/52912181/applied-coupons-disable-free-shipping-conditionally-in-woocommerce. I have tried to implement some changes using the links you proved. But it is not working – Nikhil Puthu May 05 '20 at 11:08
  • Adjust your question where necessary, clearly showing what you've tried so far and where you're getting stuck on. [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – 7uc1f3r May 05 '20 at 11:13

1 Answers1

0

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…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399