Check if Coupon is Applied

In this tutorial I am going to cover different ways how you can check if a specific (or any) coupon has previously been added to WooCommerce cart or to a specific order.

In Cart

We can check quite easily if a coupon has been applied to cart using WC()->cart->get_applied_coupons() method that returns an array of all coupon codes like Array( 0 => blackfrd, 1 => nysale). So it is super simple to check if there is a specific coupon in that array, just use in_array() PHP function.

if( in_array( 'blackfrd', WC()->cart->get_applied_coupons() ) ) {
	// Coupon "blackfrd" is added to cart
}

The same using WC()->cart->has_discount() method. I think it is more correct and clear to be honest.

if( WC()->cart->has_discount( 'blackfrd' ) ) {
	// Coupon "blackfrd" is added to cart
}

By using WC()->cart->get_applied_coupons() we can also check if ANY coupon has been added to cart.

if( count( WC()->cart->get_applied_coupons() ) > 0 ) {
	// There are coupons
}

In Specific Order

Sometimes you may want to check if a coupon has been applied to a specific order in WooCommerce.

Check if coupon has been applied to an order WooCommerce
This metabox you can find on any order edit page in WordPress admin.

We have plenty of things to check here!

First of all let’s try $order->get_coupon_codes() method which is similar to WC()->cart->get_applied_coupons() we used above.

$order = wc_get_order( $order_id );

if( in_array( 'blackfrd', $order->get_coupon_codes() ) ) {
	// Coupon "blackfrd" is added to an order
}

Let’s also check if ANY coupon has been added to the order.

if( count( $order->get_coupon_codes() ) > 0 ) {
	// There are coupons
}

We can also find out what was the discount amount using either $order->get_discount_total() or $order->get_total_discount(), the second one allows to calculate tax discount as well plus it has woocommerce_order_get_total_discount filter hook inside.

echo 'Your discount: ' . $order->get_discount_total();
// Your discount: 200

It is also possible to check if order items specifically have discounts. In order to do that we are going to use get_items() method of WC_Order object, and get_subtotal() and get_total() methods of WC_Order_Item object.

foreach( $order->get_items() as $item ) {
	echo 'Discount: ' . ( $item->get_subtotal() - $item->get_total() );		
}
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter