Apply a coupon for minimum cart total

The code snippet below allows you to:

  • Show a notice on the cart and checkout page, reminding customers that they get a discount if spending more than a minimum amount.
  • Automatically apply a discount and show a notice that the discount was appliedย when the cart totalย is more than a minimum amount.
  • Requirements

Requirements:

  • A coupon called COUPONย created in WooCommerce > Coupons without a minimum amount.
  • The $minimum_amount variable adjusted to the minimum amount according to your needs.
  • Notices edited to reflect the discount wanted.

Add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

<div>
<pre>/**
* Apply a coupon for minimum cart total
*/

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

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();
}</pre>
</div>

Keep yourself in the loop!

Sign up for the WooCommerce developer newsletter:
Hidden
Hidden
Hidden