Check if a coupon is applied to cart in WooCommerce
cart, coupon, php, woocommerce, wordpress
Solution
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);
}
}
Problem
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 )`