我想在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();
}
}
}
答案 0 :(得分:0)
您应该将这些条件语句合并为一个全面的声明:
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();
}
}
}