【问题标题】:WooCommerce coupon code wildcardWooCommerce 优惠券代码通配符
【发布时间】:2018-05-28 20:35:20
【问题描述】:

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

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

【问题讨论】:

    标签: woocommerce minimum coupon


    【解决方案1】:

    您可以检查购物车是否应用了折扣券,然后遍历每张优惠券是否有以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;
                }
            }
        }
    

    【讨论】:

    • 你是救生员,Reigel。您的代码完美运行。谢谢!
    • 很高兴我能帮上忙。如果您能接受答案并投赞成票,我将不胜感激。干杯! @辛迪
    猜你喜欢
    • 2014-11-26
    • 2016-02-25
    • 2017-07-13
    • 2018-05-11
    • 1970-01-01
    • 2018-04-12
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多