Mika MAtikainen

Apr 15, 2020 · 4 min read

How to Apply a coupon for minimum cart total in WooCommerce?


/**
* Apply a coupon for minimum cart total
*/

add_action( 'woocommerce_before_cart' , 'wpsc_add_coupon_notice' );
add_action( 'woocommerce_before_checkout_form' , 'wpsc_add_coupon_notice' );

function wpsc_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();
}

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Share This