Banner

Auto Apply Coupon

Automatically applies a predefined coupon code to the cart.

PHP
/**
 * Snippet Name:     Auto Apply Coupon
 * Snippet Author:   coding-bunny.com
 * Description:      Automatically applies a predefined coupon code to the cart.
 */

define('COUPON_AUTO_APPLY_CODE', 'YOUR_COUPON_NAME_HERE');

function cbaac_apply_coupon_automatically() {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( ! WC()->cart ) return;

    if ( ! WC()->cart->has_discount( COUPON_AUTO_APPLY_CODE ) ) {
        WC()->cart->apply_coupon( COUPON_AUTO_APPLY_CODE );
    }
}
add_action( 'woocommerce_before_calculate_totals', 'cbaac_apply_coupon_automatically', 10 );

How To Implement This Solution?

Leave a Reply

My Agile Privacy
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.

Need help?

Choose one of the following options:

Powered by CodingBunny