Search code examples
phpwordpresswoocommercecartcoupon

Woocommerce check if coupon is valid or been used


how can i check is the coupon is valid or been used? I have a this code:

if (is_page('checkout')){ //check coupon insert first is valid or no }


Solution

  • You can use 2 methods on WC_cart object regarding applied coupons:

    $applied_coupons = WC()->cart->get_applied_coupons();
    

    Return an array of applied coupons codes

    And:

    if( WC()->cart->has_discount( 'your_coupon_code' ) && is_checkout() ) {
        // do something
    }
    

    Returns whether or not a discount has been applied (boolean).

    Note: The correct conditionals in woocommerce for targeting cart or checkout pages are is_checkout() for checkout page and is_cart() for cart page.

    References: