[英]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.