Skip to content

Posted in: Retired Snippets | View All

Important: Snippet Placement

RETIRED – 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:
  • 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.
      /**
* 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();
}
  

Audit History

Failed | January 31, 2023 | By jcullen

Scroll To Top