Skip to main content
Login Join
Snippet · PHP

Automatically Apply a Coupon Code in WooCommerce

Shared by Amit Dholiya · July 20, 2026 · @woocommerce_before_cart, woocommerce_before_checkout_form

3 copies
8 views
Back to Snippets

Steps

  1. Open functions.php.
  2. Add the code below.
  3. Save the file.

Done — the specified coupon code will be automatically applied when a customer visits the cart or checkout page.

Notes

  • Replace WELCOME10 with your actual coupon code.
  • The coupon must already exist in WooCommerce.
  • Works on both Cart and Checkout pages.
  • Can be added to your theme’s functions.php file or a custom plugin.
function auto_apply_coupon() {
    $coupon_code = 'WELCOME10';

    if ( WC()->cart && ! WC()->cart->has_discount( $coupon_code ) ) {
        WC()->cart->apply_coupon( $coupon_code );
    }
}

add_action(
    'woocommerce_before_cart',
    'auto_apply_coupon'
);

add_action(
    'woocommerce_before_checkout_form',
    'auto_apply_coupon'
);

0 comments