
Offering discounts automatically in WooCommerce is a powerful way to increase sales without forcing customers to manually enter coupon codes.
In this guide, you’ll learn how to auto-apply a coupon based on cart contents using WooCommerce hooks.
This method improves user experience and ensures customers never miss a discount they qualify for.
Why Auto-Apply Coupons Work So Well
Most customers forget or ignore coupon fields during checkout. Automatic discounts solve this problem instantly.
- No need for customers to enter coupon codes
- Faster checkout process
- Higher conversion rates
- Better perceived value
The WooCommerce Hook We Will Use
We will use the following hook:
woocommerce_before_calculate_totals
This hook runs every time WooCommerce recalculates the cart, making it perfect for applying dynamic discounts.
Example Code
Add this code to your theme’s functions.php file or a custom plugin:
add_action('woocommerce_before_calculate_totals', 'auto_apply_custom_coupon');
function auto_apply_custom_coupon($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$coupon_code = 'SAVE10';
$minimum_amount = 80;
$cart_total = $cart->subtotal;
if ($cart_total >= $minimum_amount) {
if (!$cart->has_discount($coupon_code)) {
$cart->apply_coupon($coupon_code);
}
} else {
if ($cart->has_discount($coupon_code)) {
$cart->remove_coupon($coupon_code);
}
}
}
How This Code Works
This function checks the cart total every time it updates:
- If the cart total is above the minimum amount, the coupon is automatically applied
- If the total drops below the threshold, the coupon is removed
- The system prevents duplicate coupon applications
Important Notes
- Replace SAVE10 with your actual WooCommerce coupon code
- Make sure the coupon exists in WooCommerce admin panel
- Test with taxes and shipping enabled
Advanced Ideas
You can expand this functionality even further:
- Apply different coupons for different product categories
- Stack multiple automatic discounts
- Trigger coupons for logged-in users only
- Combine with free shipping rules
