Want to automatically apply a coupon in WooCommerce without using an extra plugin?
By default, WooCommerce requires customers to manually enter coupon codes at checkout. But in many cases, store owners want discounts to apply automatically based on cart conditions, product totals, or promotional campaigns.
In this tutorial, we’ll use a simple WooCommerce PHP snippet to auto apply a coupon code without installing any plugin.
What This Snippet Does
- Automatically applies a coupon code
- No plugin required
- Works directly in WooCommerce cart
- You can control minimum cart amount
- Lightweight and beginner friendly
PHP Snippet: Auto Apply Coupon in WooCommerce
Add this code inside your child theme’s functions.php file or use the Code Snippets plugin.
/** * Auto Apply Coupon in WooCommerce */
add_action( 'woocommerce_before_cart', 'wp_codex_auto_apply_coupon' );
function wp_codex_auto_apply_coupon() {
$coupon_code = 'SAVE10'; // Add Your Coupon code
$minimum_amount = 100; // Minimum cart total
if ( WC()->cart->subtotal >= $minimum_amount ) {
if ( ! WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->apply_coupon( $coupon_code );
wc_print_notice( 'Coupon automatically applied!', 'success' );
}
} else {
if ( WC()->cart->has_discount( $coupon_code ) ) {
WC()->cart->remove_coupon( $coupon_code );
wc_print_notice( 'Coupon removed because minimum amount is not reached.', 'notice' );
}
}
}How This Code Works
This snippet checks the WooCommerce cart subtotal. If the cart total is greater than or equal to the minimum amount, the coupon is applied automatically.
If the customer removes products and the subtotal becomes lower than the required amount, the coupon will also be removed automatically.
Change the Coupon Code
Replace this line:
$coupon_code = 'SAVE10';with your own WooCommerce coupon code.
Change Minimum Cart Amount
Replace this line:
$minimum_amount = 100;with your preferred cart total amount.
Where to Add This Snippet?
- Child theme functions.php file
- Code Snippets plugin
- Custom WooCommerce functionality plugin
Bonus Idea
You can also modify this snippet to:
- Apply coupons for specific products
- Apply discounts for specific user roles
- Auto apply free shipping coupons
- Apply coupons only for first orders
- Apply seasonal promotional discounts
This WooCommerce auto apply coupon snippet is a simple way to improve the customer experience and increase conversions without adding another plugin to your store.
It’s lightweight, easy to customize, and works well for most WooCommerce promotional campaigns.

