9

I need to find a way to check if a coupon is applied to WooCommerce checkout, if so I would like to do something. I have tried searching around for this and cannot find a solution.

Here is a slimmed down version of what I am trying:

add_action('woocommerce_before_cart_table', 'apply_product_on_coupon');
function apply_product_on_coupon( ) {
    global $woocommerce;
    $coupon_id = '12345';
        
        if( $woocommerce->cart->applied_coupons === $coupon_id ) {
        echo 'YAY it works';
    }
}

So is this not the right way to check if the coupon exists in cart? if( $woocommerce->cart->applied_coupons === $coupon_id )

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Derek
  • 4,747
  • 7
  • 44
  • 79
  • What is the result of your test? – qwertynl Nov 20 '13 at 15:53
  • Well in this example if the coupon "12345" is applied to cart then it will echo the "Yay it works". What I really plan on doing is adding a free product to cart if the coupon is applied, I have everything down except checking to see if my coupon is applied. – Derek Nov 20 '13 at 16:13
  • Does your test actually echo 'YAY it works'? – maiorano84 Nov 20 '13 at 16:16
  • It will once I can test it against the coupon_id, I have no way of checking the applied coupons to see if coupon '12345' is applied to cart or not.. The above code will work if I remove the === $coupon_id, but it works for all coupons applied and not my specific coupon – Derek Nov 20 '13 at 16:23

4 Answers4

22

From your example, something like this might work. This is untested, but should give you a step in the right direction:

add_action('woocommerce_applied_coupon', 'apply_product_on_coupon');
function apply_product_on_coupon( ) {
    global $woocommerce;
    $coupon_id = '12345';
    $free_product_id = 54321;

    if(in_array($coupon_id, $woocommerce->cart->get_applied_coupons())){
        $woocommerce->cart->add_to_cart($free_product_id, 1);
    }
}
Isaac Lubow
  • 3,557
  • 5
  • 37
  • 53
maiorano84
  • 11,574
  • 3
  • 35
  • 48
  • 1
    Here is the documentation on the [Cart Class](http://docs.woothemes.com/wc-apidocs/class-WC_Cart.html#_add_to_cart) – maiorano84 Nov 20 '13 at 16:25
  • 1
    If anyone is interested, WC_Cart::get_applied_coupons() returns an array of coupon code strings, this is not clear in the docs. – rafark Nov 09 '21 at 02:39
6
global $woocommerce;
if (!empty($woocommerce->cart->applied_coupons))
{
        //print_r($woocommerce->cart->applied_coupons); - keys of coupons here
}
realmag777
  • 2,050
  • 1
  • 24
  • 22
2

This might be an ages issue but an easy solution is to use

WC()->cart->applied_coupons

This return array lists of applied coupons, you can then use foreach, for or in_array to check applied coupons.

Hope that helps

Ryan S
  • 6,076
  • 1
  • 20
  • 14
2

If you know the coupon code but not the coupon post ID, you can use this mash up of realmag777's answer and maiorano84's answer.

function CheckCouponIsApplied($cpn_code)
{
    global $woocommerce;
    $lowercasecouponcode = strtolower($cpn_code); //ENSURE LOWERCASE TO MATCH WOOCOMMERCE NORMALIZATION
    return in_array($lowercasecouponcode, $woocommerce->cart->applied_coupons);
}
Marcello B.
  • 4,177
  • 11
  • 45
  • 65