【问题标题】:Coupon code is not remove after first click of update cart button on cart page woocommerce首次点击购物车页面 woocommerce 上的更新购物车按钮后,优惠券代码不会被删除
【发布时间】:2014-09-22 06:34:29
【问题描述】:

我想对 12(数量)应用折扣并删除低于 12(数量)的折扣。 我为 20% 折扣('genew')创建了一个优惠券代码。当有人点击购物车页面(woo-commerce)上的更新购物车按钮时,我应用并删除了优惠券代码。删除优惠券代码功能仅在有人单击更新购物车按钮两次时才起作用。第一次点击它不会删除优惠券代码。

这是我在 function.php 中使用的函数

add_action('woocommerce_before_cart_table', 'discount_coupon');
function discount_coupon() {
global $woocommerce;
global $count_cart_quantity;
if ( $count_cart_quantity >= 12 ) {
$coupon_code = 'genew';
if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
    $woocommerce->show_messages();
}

}

if ( $count_cart_quantity < 12 && $count_cart_quantity > 1 ) {
$coupon_code = 'genew';
if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) {
    $woocommerce->show_messages();
}   

}

}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您应该将这些条件语句组合成一个综合语句:

    add_action('woocommerce_before_cart_table', 'discount_coupon');
    function discount_coupon() {
        global $woocommerce;
        global $count_cart_quantity;
    
        $coupon_code = 'genew';
    
        if ( $count_cart_quantity >= 12 ) {
            if (!$woocommerce->cart->add_discount(sanitize_text_field($coupon_code))) {
                $woocommerce->show_messages();
            }
        }
        elseif ( $count_cart_quantity < 12 && $count_cart_quantity > 1 ) {
            if (!$woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) {
                $woocommerce->show_messages();
            }
        }
    }
    

    【讨论】:

    • 问题是在数量减少到 12 以下之后,它不会更新总数并给予 20% 的折扣。如果我们在将数量更改为 12 以下后第一次点击更新按钮,那么它不会更新总价,但如果我们第二次点击更新购物车按钮,那么它会显示正确的总数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 2013-07-16
    相关资源
    最近更新 更多