Steps
- Open
functions.php. - Add the code below.
- Save the file.
Done — the specified coupon code will be automatically applied when a customer visits the cart or checkout page.
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'
);