MichelH

Apply a coupon for minimum cart total

Apr 5th, 2023 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.78 KB | Source Code | 0 0
  1. /**
  2. * Apply a coupon for minimum cart total
  3. */
  4.  
  5. add_action( 'woocommerce_before_cart' , 'add_coupon_notice' );
  6. add_action( 'woocommerce_before_checkout_form' , 'add_coupon_notice' );
  7.  
  8. function add_coupon_notice() {
  9.  
  10.         $cart_total = WC()->cart->get_subtotal();
  11.         $minimum_amount = 15;
  12.         $currency_code = get_woocommerce_currency();
  13.         wc_clear_notices();
  14.  
  15.        if ( $cart_total < $minimum_amount ) {
  16.               WC()->cart->remove_coupon( '5% OFF' );
  17.               wc_print_notice( "Get 5% off if you spend more than $minimum_amount $currency_code!", 'notice' );
  18.         } else {
  19.               WC()->cart->apply_coupon( '5% OFF' );
  20.               wc_print_notice( 'You just got 5% off your order!', 'notice' );
  21.         }
  22.           wc_clear_notices();
  23. }
Advertisement
Add Comment
Please, Sign In to add comment