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 ); | |
} | |
} |
Hello Jordan!
This code works perfect and is exactly what i was searching!
But I have one question to it:
Is ist possible that the code takes only effect when I enter a specific coupon code?
For example I enter “12345” the code takes effect.
Many thanks in advance!
Hi Andreas,
This will allow you to set any coupon code. I was using 3 different codes by default but you can stick with one if that’s all you need and update the conditional values accordingly.
Thank you for your fast response!
I think it was a missunderstanding.
I want that the customer has to type in a specific code (12345) and when he apply the coupon your code takes effect.
Only then when the customer types in the specific coupon code.
Many thanks!