How to programmatically remove applied discount coupons in Woocommerce? How to programmatically remove applied discount coupons in Woocommerce? wordpress wordpress

How to programmatically remove applied discount coupons in Woocommerce?


To remove a single coupon from the cart using its coupon code use WC_Cart->remove_coupon( $code ).

To remove all coupons from the cart you would use WC_Cart->remove_coupons( $type ) - $type defaults to null for all, pass in "cart" to remove before tax coupons, "order" for after tax coupons.

To get all of the coupons in the cart as an array you can loop over and optionally remove, use WC_Cart->get_coupons().

foreach ( WC()->cart->get_coupons() as $code => $coupon ){   $valid = ? // decide to remove or not   if ( ! $valid ){       WC()->cart->remove_coupon( $code );   }}


The cart method remove_coupons() has been since updated such that a type is no longer required. Now, to remove all coupons, this will do the trick:

WC()->cart->remove_coupons();

For more information, check out the documentation for the WC_Cart class here.