简体   繁体   English

第一次单击购物车页面woocommerce上的更新购物车按钮后,优惠券代码未删除

[英]Coupon code is not remove after first click of update cart button on cart page woocommerce

I want to apply discount on 12(quantity) and remove discount below 12(quantity). 我想对12(数量)应用折扣,并删除12(数量)以下的折扣。 I have created one coupon code for 20% discount('genew'). 我创建了一个优惠券代码以获取20%的折扣(“ genew”)。 I applied and removed coupon code on when some one is clicked on update cart button on cart page(woo-commerce). 我在购物车页面上的“更新购物车”按钮上单击某人时应用并删除了优惠券代码(woo-commerce)。 Remove coupon code function is only work when someone click on update cart button two times. 删除优惠券代码功能仅在有人两次单击更新购物车按钮时才起作用。 one first click it is not remove the coupon code. 第一次单击它不会删除优惠券代码。

Here are function that I am using in function.php 这是我在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();
}   

}

} }

You should combine these conditional statements into one, comprehensive statement: 您应该将这些条件语句合并为一个全面的语句:

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Woocommerce中的购物车项目自动应用/删除优惠券代码 - Auto apply / remove a coupon code based on cart items in Woocommerce 应用优惠券代码后更改WooCommerce购物车价格 - Changing WooCommerce cart price after applied coupon code 在Woocommerce中单击后,将“添加到购物车”按钮更改为“查看购物车”按钮 - Change Add to Cart Button to View Cart Button after click in Woocommerce WooCommerce购物车页面需要实时更新,而无需“更新购物车”按钮 - WooCommerce Cart page needs to update live without the Update Cart button 删除在 WooCommerce 中单击“添加到购物车”按钮后出现的“查看购物车”链接 - Remove “View Cart” link which appears after click on the “Add To Cart” button in WooCommerce WooCommerce:更改删除购物车中的优惠券链接 - WooCommerce: Change remove coupon link in cart 将优惠券百分比添加到 WooCommerce 中的购物车页面 - Add Coupon Percentage to Cart Page in WooCommerce WooCommerce:在购物车页面上移动优惠券字段 - WooCommerce: Move Coupon Field on Cart Page 有条件地隐藏 Woocommerce 中的购物车页面优惠券字段 - Hide conditionally Cart page coupon field in Woocommerce 具有自定义价格的购物车商品的Woocommerce优惠券代码 - Woocommerce coupon code for cart items with custom price
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM