Skip to content

Instantly share code, notes, and snippets.

@wbcomdev
Created May 14, 2021 05:45
Show Gist options
  • Select an option

  • Save wbcomdev/47fb3e2e2c8a64bcdad63afb4471ed72 to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/47fb3e2e2c8a64bcdad63afb4471ed72 to your computer and use it in GitHub Desktop.
/**
* Apply a coupon for minimum cart total.
*/
function wb_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', 'wb_add_coupon_notice' );
add_action( 'woocommerce_before_checkout_form', 'wb_add_coupon_notice' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment