WooCommerce优惠券代码通配符

时间:2018-05-28 20:35:21

标签: woocommerce minimum coupon

我的购物车订单最低为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;
}

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

1 个答案:

答案 0 :(得分:0)

您可以检查购物车是否已应用折扣优惠券,然后循环浏览每张优惠券(如果有优惠券以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;
            }
        }
    }