WooCommerce 优惠券代码通配符

WooCommerce coupon code wildcard

我的购物车最低订单金额为 15 美元。但是,我想创建一些绕过最低订购要求的优惠券代码。我下面的代码允许我命名一个特定的优惠券。如何使用通配符,这样我就不必列出所有以字母 nm 开头的优惠券代码?

这是我使用的代码:

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' 
        );

    }
}

}

我想使用通配符的代码在这里(上面代码中的第 3 段):

 // No minimum purchase if a specific coupon code is used
if ( WC()->cart->has_discount ( 'nm*' ) ) {
return;
}

* 不起作用。我该如何编码?谢谢!!!!!

您可以检查购物车是否应用了折扣券,如果有以 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;
            }
        }
    }