我想在12(数量)上应用折扣,并删除低于12的折扣(数量)。我已经创建了一个优惠券代码的20%折扣('新')。当有人点击购物车页面上的更新购物车按钮时,我申请并删除了优惠券代码(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();
}
}
}
您应该将这些条件语句组合成一个全面的语句:
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();
}
}
}