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.
First make sure you have enabled coupons in the settings. Then go to Commerce -> Coupons and create a coupon called COUPON. The coupon type is “percentage discount” and the “coupon amount” is 25. The minimum spending amount is set in the code below as 100.
/**
* Apply a coupon discount for minimum cart total
*/
add_action('woocommerce_before_cart' , 'cc_add_coupon_notice');
add_action('woocommerce_before_checkout_form' , 'cc_add_coupon_notice');
function cc_add_coupon_notice() {
$cart_total = WC()->cart->get_subtotal();
$minimum_amount = 100; // minumum cart amount to apply the discount
$currency_code = get_woocommerce_currency();
wc_clear_notices();
if ($cart_total < $minimum_amount) {
WC()->cart->remove_coupon('COUPON');
wc_print_notice("Get 25% off if you spend more than $minimum_amount $currency_code!", 'notice');
} else {
WC()->cart->apply_coupon('COUPON');
wc_print_notice('You just got 25% off your order!', 'notice');
}
wc_clear_notices();
}
View Raw
Code ID: 9933