I have my cart set with a minimum order of $15. However, I want to create some coupon codes that will bypass the minimum order requirement. My code below allows me to name a specific coupon. How do I use a wildcard so that I don't have to list every coupon code I have that starts with the letters nm?
Here is the code I'm using:
add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
/* add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' ); */
add_action( 'woocommerce_check_cart_items' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 15;
// No minimum purchase if a specific coupon code is used
if ( WC()->cart->has_discount ( '*nm*' ) ) {
return;
}
if ( WC()->cart->subtotal < $minimum ) {
if( is_cart() ) {
wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order subtotal is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);
} else {
wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);
}
}
}
The code where I want to use a wildcard is here (3rd paragraph in the code above):
// No minimum purchase if a specific coupon code is used
if ( WC()->cart->has_discount ( 'nm*' ) ) {
return;
}
The * doesn't work. How do I code this? Thank you!!!!!
You can check if cart has discount coupon applied, then loop through each coupon if there's one that starts with nm
.
// No minimum purchase if a specific coupon code is used
if ( WC()->cart->has_discount ( ) ) { // check if has discount coupons
// loop through each coupon
foreach ( WC()->cart->applied_coupons as $coupon ) {
if (strpos($coupon, 'nm') === 0) {
// coupon starts with 'nm'
return;
}
}
}