Running a WooCommerce store means you often want to incentivize customers to buy more. One of the most effective strategies is offering automatic discounts when the cart reaches a certain total.
In this guide, we’ll show you step-by-step how to apply a discount automatically based on the cart total using custom code no additional plugins required.
Why Offer Cart Total Discounts?
Offering discounts based on the cart total can:
- Increase Average Order Value (AOV): Encourage customers to spend more to unlock discounts.
- Boost Customer Loyalty: Reward loyal customers with automatic savings.
- Simplify Checkout: Automatic discounts remove the need for coupons, reducing friction.
Step 1: Determine Discount Rules
Decide the rules for your automatic discount. For example:
- 5% off for orders over $50
- 10% off for orders over $100
- 15% off for orders over $200
Step 2: Add PHP Snippet to Apply Discount
Copy the following code into your child theme’s functions.php
file or a custom plugin:
/**
* WooCommerce: Apply automatic discount based on cart total
* Author: Your Name
* Updated: 2025
*/
add_action( 'woocommerce_cart_calculate_fees', 'custom_cart_total_discount' );
function custom_cart_total_discount( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Get cart total
$cart_total = $cart->get_subtotal();
// Define discount rules
if ( $cart_total >= 200 ) {
$discount = $cart_total * 0.15; // 15% discount
$label = '15% Off Discount';
} elseif ( $cart_total >= 100 ) {
$discount = $cart_total * 0.10; // 10% discount
$label = '10% Off Discount';
} elseif ( $cart_total >= 50 ) {
$discount = $cart_total * 0.05; // 5% discount
$label = '5% Off Discount';
} else {
return;
}
// Apply discount
$cart->add_fee( $label, -$discount );
}
How It Works
- The snippet checks the cart subtotal.
- It applies the highest applicable discount based on the rules.
- The discount appears automatically in the cart and checkout page under fees.
Tip: You can customize this snippet to apply fixed discounts instead of percentages, or target specific products or categories.
Optional: Target Specific Categories
You can also apply discounts only if certain categories are in the cart:
$has_category = false;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( 'clothing', 'product_cat', $cart_item['product_id'] ) ) {
$has_category = true;
break;
}
}
if ( $has_category ) {
$cart->add_fee( 'Category Discount', -10 ); // $10 discount
}
Result
After adding this snippet:
- Discounts are applied automatically based on cart total.
- Customers see real-time savings during checkout.
- You encourage higher spend without relying on manual coupon codes.