Add the below code at the end of your theme’s functions.php file or you can also add the below code inside your site-specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Apply a coupon for minimum cart total */ function add_coupon_notice() { $cart_total = WC()->cart->get_subtotal(); $minimum_amount = 50; $currency_code = get_woocommerce_currency(); wc_clear_notices(); if ( $cart_total < $minimum_amount ) { WC()->cart->remove_coupon( 'COUPON' ); wc_print_notice( "Get 50% off if you spend more than $minimum_amount $currency_code!", 'notice' ); } else { WC()->cart->apply_coupon( 'COUPON' ); wc_print_notice( 'You just got 50% off your order!', 'notice' ); } wc_clear_notices(); } add_action( 'woocommerce_before_cart' , 'add_coupon_notice' ); add_action( 'woocommerce_before_checkout_form' , 'add_coupon_notice' ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.