1

i have two coupons and multiple shipping zones

  • (shipping zone A, shipping zone B, shipping zone C)
  • (coupon 1) and (coupon 2). what i want is that if a user choose a specific shipping zone lets say (shipping zone C) i want to apply (coupon 2) for (shipping zone C) specifically. and (coupon 1) will be applies for all other shipping zones.

this is what i tried so far

<?php
add_action('woocommerce_before_cart', 'add_coupon_notice');
add_action('woocommerce_before_checkout_form', 'add_coupon_notice');
function add_coupon_notice()
{
    if (is_cart() || is_checkout())
    {
        global $woocommerce;
        $cart_total = WC()
            ->cart->total;
        $minimum_amount = 100;
        $currency_code = get_woocommerce_currency();
        $states = array(
            'shipping zone A',
            'shipping zone B',
            'shipping zone C'
        );
        wc_clear_notices();
        if ($cart_total < $minimum_amount && in_array(WC()
            ->customer
            ->get_shipping_state() , $states))
        {
            WC()
                ->cart
                ->apply_coupon('COUPON 1');
            wc_print_notice('You just got 15% off your order!', 'notice');
        }
        else
        {
            WC()
                ->cart
                ->apply_coupon('COUPON 2');
            wc_print_notice('You just got 15% off your order!', 'notice');
        }
        wc_clear_notices();
    }
}
aynber
  • 22,380
  • 8
  • 50
  • 63
Ali
  • 33
  • 1
  • 5
  • So what happens with your current code? Does it work under some conditions? Under which conditions does it not work, and how exactly does it fail? Please edit this info into your question. :) – ZeroOne Oct 26 '20 at 12:47
  • its working only for one coupon it will not activate any other coupon – Ali Oct 27 '20 at 09:24

1 Answers1

1

i managed to solve it with below code.

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    function woocommerce_custom_surcharge() {
        global $woocommerce;
        $state = $woocommerce->customer->get_shipping_state();       
        if ( $state == 'shipping zone 1' ) {
            WC()->cart->apply_coupon( 'COUPON 2' );
            WC()->cart->remove_coupon( 'COUPON 1' );   
        } else {
             WC()->cart->apply_coupon( 'COUPON 1' );
             WC()->cart->remove_coupon( 'COUPON 2' );
        } 
    }
Ali
  • 33
  • 1
  • 5