Skip to content

How to Check If a WooCommerce Coupon Is Applied

Sometimes we need to check if a specific WooCommerce coupon is applied on the cart page or in the order details. We can check this programmatically or through the order details page in the WooCommerce menu of the WordPress dashboard.

First, let’s learn how to check if a certain coupon is applied for the order or in the cart page through the WordPress dashboard and then we’ll learn how to perform the same process using code snippets.

How to Check Coupon Status via Order Details

If you navigate to the WordPress dashboard and then WooCommerce  > Orders menu, you will see a list of the orders that have been placed in your shop.

WooCommerce coupon - WP dashboard - WooCommerce - Orders

Once on the order page, click on one of the orders. For this example, I chose an order that has a WooCommerce coupon applied to it so we can see where coupons are displayed on the order details page.

As you can see in the picture above, a coupon with the code “8PUH555T” has been applied for this order and the discounted amount is $100.

How to Check Coupon Status Programmatically

Using a bit of code, you can check coupons in various hooks and pages depending upon your needs. For example, if you need to remove a coupon, you can check the cart page or checkout page before going to the payment page.

You can use these methods on the JupiterX theme or any other theme by addings some code snippets to your theme’s function.php file.

By using the following code snippet, I can check if a certain coupon is applied or not. If the coupon is applied, I will remove it.

add_action( 'woocommerce_before_cart', 'remove_certain_coupon', 999 );
 
function remove_certain_coupon() {
    if ( ! is_cart() ) { // Just apply this in the cart page.
        return;
    }
   
    $applied_coupons = WC()->cart->get_applied_coupons(); // All of the applied coupons.
    $certain_coupon  = '8puh555t'; // Recommended to use lowercase.
   
    if ( in_array( $certain_coupon, $applied_coupons, true ) ) {
        WC()->cart->remove_coupon( $certain_coupon ); // Remove certain coupon from cart.
    }
}

In this case we limited this snippet to the cart page using is_cart() condition, but you can use any kind of condition you need to check.

There is another easier method we can use to check if a certain WooCommerce coupon is applied for a WooCommerce session with this code snippet:

if( WC()->cart->has_discount( '8puh555t' ) ) {
    // Coupon is applied, let's do something.
}

You may sometimes need to check if a coupon is applied. This time we do not have any coupon code to check, so we can check the number of coupons applied to see if it’s more than zero. See the following snippet:

// following method returns an array of applied coupons.
$applied_coupons = WC()->cart->get_applied_coupons();
 
if( count( $applied_coupons ) > 0 ) { // Is array length greater than zero ?
    // Yes there is , let's do something
}

So far, using these methods we are only able to check client cart objects. When the order is finalized, however, the cart object cannot determine which coupons are applied.

For finalized orders we can use the order object. On the order page you have access to the order ID, which is the only place we can check order details. Of course, you have access to the order details in the database if you have access to the order ID.

Check this snippet, which gives us access to WooCommerce coupons through the order ID if any coupon is applied to the order.

We can run this code in the thank you page hooks where we have access to the order ID:

add_action( 'woocommerce_before_thankyou', 'get_order_coupons', 10, 1 );
 
function get_order_coupons( $order_id ) {
    $order           = wc_get_order( $order_id );
    $applied_coupons = $order->get_coupon_codes();
 
    // Now we have access to order applied coupons and we can do whatever we want.
}

Conclusion

Based on what we’ve shown you, we can check the status of WooCommerce coupons and the status of user orders details to make sure everything is fine at any step of the shopping process.

The cart object and the order object provide us with what we need to handle the shopping process however we want.

If you have any questions related to WooCommerce coupons, don’t hesitate to ask us in the comments.

Create Your
Dream Website with

Stay in the Loop

Sign up for our newsletter and stay up-to-date on the
latest WordPress trends, insights, and resources.

By entering your email, you agree to our Privacy policy and Terms of Services.

Share

Hadi Mohammade

Hadi Mohammade

I am a web artisan with more than 6 years of experience and am really interested to share my knowledge with everyone out there. Alongside programming I am a professional gamer, playing some online video games with friends after work is the best of fun for me.

2 Comments

  1. The information is very special, I will have to follow you.

  2. Very good article, I have learned and gained a lot of experience for myself. Please continue to contribute such useful articles to us!


Add a Comment

Your email address will not be published. Required fields are marked *