Check if coupon is applied in woo commerce

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 )

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

check any coupon code applied on cart or not, i want to check either the coupon code is applied on cart or not i have already try. WC()->cart->applied_coupons. but none of them is work for me i just want to� As with every other coupon, you want visitors to know about the discount as soon as possible. That way, the potential savings can influence their decision to make a purchase. With that in mind, let’s talk about how to set up that product-specific coupon. How To Check for a Product When Applying a WooCommerce Coupon (2 Steps)

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

WooCommerce: Check if Coupon Already Applied � GitHub, <? /** * WooCommerce: Hide 'Coupon form' on checkout page if a coupon was already applied in the cart. * Add to theme functions.php file or Code Snippets plugin. */ add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' ); function woocommerce_coupons_enabled_checkout( $coupons_enabled ) { Another great time to use coupon URLs is for a flash sale on social media. Again, the coupon is easy for your customers to use, plus only giving the URL (not the coupon code) means you can keep your message short. Creating a Coupon Link for WooCommerce. The best way to create coupon links in WooCommerce is by using the Advanced Coupons.

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

Coupon Management, Note: The sum of the cart subtotal + tax is used to determine the minimum amount . Maximum spend – Allows you to set� Coupons are a great way to offer discounts and rewards to your customers, and can help promote sales across your shop. To use coupons with WooCommerce. Go to: WooCommerce > Settings > General > Enable coupons; Tick the checkbox to Enable the use of coupon codes. Save Changes. More info at: Configuring WooCommerce – General options.

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

Apply a coupon for minimum cart total, Show a notice on the cart and checkout page, reminding customers that they get a discount if spending more than a minimum amount. Automatically apply a� Check for user coupons (now that we have billing email). public get_applied_coupons This method should be called on a callback attached to the woocommerce

Coupon Shortcodes, It provides the following conditional shortcodes that allow to enclose content which is shown if coupons are applied, valid or not valid. [coupon_is_applied]; [� Coupons are a great way to attract new customers and get loyal ones to come back for more. WooCommerce includes simple yet powerful coupon functionality that fits most stores’ needs right out-of-the-box — which means you can start offering coupons with WooCommerce in just a few clicks.

WooCommerce URL Coupons, If disabled, the coupon will initially check for any requirements, and fail if they're not present (won't apply again once the requirements are met). If a coupon has a � PHP Snippet 1: Apply a Coupon Programmatically if a Product is in the Cart. Notes: Create a coupon code that you want to apply once a certain product is added to cart (go to WooCommerce / Coupons / Add New and decide your coupon code.

WooCommerce: Apply Coupon Programmatically if Product @ Cart, Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins via your child theme. Watch me code and learn� I'd like to be able to add a fixed $20 fee if a given coupon code (100% off) is applied to the cart. The fee should also not have taxes applied.

Comments
  • What is the result of your test?
  • 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.
  • Does your test actually echo 'YAY it works'?
  • 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
  • Here is the documentation on the Cart Class