Check if coupon is applied in woo commerce Check if coupon is applied in woo commerce wordpress wordpress

Check if coupon is applied in woo commerce


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);    }}


global $woocommerce;if (!empty($woocommerce->cart->applied_coupons)){        //print_r($woocommerce->cart->applied_coupons); - keys of coupons here}


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