A client with a WooCommerce shop recently asked if there was a way to add a coupon code to the cart automatically based on the cart total. The client had three specific coupons they wanted to work with.
10% – if total was $400 or more
15% – if total was $600 or more
20% – if total was $800 or more
There are plugin solutions for this of course but I figured I could come up with something to accomplish this without needing to rely on a plugin so I got started. Turns out it was pretty easy to accomplish because WooCommerce is awesome. 😉
Because the product totals continue to change on the cart page if a customer adds or removes products from the cart, I decided to go with the cart subtotal rather than the cart total because the subtotal won’t change when the coupon codes change. This provides a more accurate way to apply the coupon codes based on the amount being ordered.
Hope this helps you out!
| <?php | |
| /** | |
| * @snippet Add A Coupon Dynamically based on Cart Subtotal with WooCommerce | |
| * @sourcecode http://hirejordansmith.com | |
| * @author Jordan Smith | |
| * @compatible WooCommerce 2.4.7 | |
| */ | |
| add_action( 'woocommerce_before_cart', 'apply_matched_coupons' ); | |
| function apply_matched_coupons() { | |
| global $woocommerce; | |
| // Set your coupon codes | |
| $tri10 = 'tri10'; | |
| $tri15 = 'tri15'; | |
| $tri20 = 'tri20'; | |
| // Get the cart subtotal in non-decimal number format | |
| $cart_subtotal = WC()->cart->subtotal; | |
| // If cart subtotal is less than 400 remove coupons | |
| if ($cart_subtotal < 400) { | |
| if ( $woocommerce->cart->has_discount( $tri10 ) || $woocommerce->cart->has_discount( $tri15 ) || $woocommerce->cart->has_discount( $tri20 ) ) { | |
| WC()->cart->remove_coupon( $tri10 ); | |
| WC()->cart->remove_coupon( $tri15 ); | |
| WC()->cart->remove_coupon( $tri20 ); | |
| } | |
| } | |
| // If cart subtotal is greater 399 AND is less than 600 add or remove coupons | |
| if ($cart_subtotal > 399 && $cart_subtotal < 600 ) { | |
| if ( $woocommerce->cart->has_discount( $tri10 ) ) return; | |
| if ( $woocommerce->cart->has_discount( $tri15 ) || $woocommerce->cart->has_discount( $tri20 ) ) { | |
| WC()->cart->remove_coupon( $tri15 ); | |
| WC()->cart->remove_coupon( $tri20 ); | |
| } | |
| $woocommerce->cart->add_discount( $tri10 ); | |
| } | |
| // If cart subtotal is greater than 599 AND is less than 800 add or remove coupons | |
| if ($cart_subtotal > 599 && $cart_subtotal < 800) { | |
| if ( $woocommerce->cart->has_discount( $tri15 ) ) return; | |
| if ( $woocommerce->cart->has_discount( $tri10 ) || $woocommerce->cart->has_discount( $tri20 ) ) { | |
| WC()->cart->remove_coupon( $tri10 ); | |
| WC()->cart->remove_coupon( $tri20 ); | |
| } | |
| $woocommerce->cart->add_discount( $tri15 ); | |
| } | |
| // If cart subtotal is greater than 799 add or remove coupons | |
| if ( $cart_subtotal > 799) { | |
| if ( $woocommerce->cart->has_discount( $tri20 ) ) return; | |
| if ( $woocommerce->cart->has_discount( $tri10 ) || $woocommerce->cart->has_discount( $tri15 ) ) { | |
| WC()->cart->remove_coupon( $tri10 ); | |
| WC()->cart->remove_coupon( $tri15 ); | |
| } | |
| $woocommerce->cart->add_discount( $tri20 ); | |
| } | |
| } |


