我一直在尋找一段時間,但我無法找到如何從程序上刪除woocommerce優惠券。如何編程刪除Woocommerce中的應用折扣優惠券?
我試圖根據購物車總額進行折扣。我需要申請移除優惠券,因爲如果您有價值1000歐元的產品(應用15%折扣優惠券)並移除產品並僅留下價值50歐元的產品,您仍然可以享受此15%的折扣,因爲我的代碼不會移除已應用的優惠券。
因此,這裏是我的代碼現在:
add_action('woocommerce_before_cart', 'apply_matched_coupons');
function apply_matched_coupons() {
global $woocommerce;
$coupon_code5 = '5p'; // your coupon code here
$coupon_code10 = '10p'; // your coupon code here
$coupon_code15 = '15p'; // your coupon code here
$coupon_code20 = '20p'; // your coupon code here
$coupon_code25 = '25p'; // your coupon code here
if ($woocommerce->cart->has_discount($coupon_code)){
return;
}
if ($woocommerce->cart->cart_contents_total >= 4000) {
$woocommerce->cart->add_discount($coupon_code25);
$woocommerce->show_messages();
}
else if ($woocommerce->cart->cart_contents_total >= 2000) {
$woocommerce->cart->add_discount($coupon_code20);
$woocommerce->show_messages();
}
else if ($woocommerce->cart->cart_contents_total >= 1000) {
$woocommerce->cart->add_discount($coupon_code15);
$woocommerce->show_messages();
}
else if ($woocommerce->cart->cart_contents_total >= 500) {
$woocommerce->cart->add_discount($coupon_code10);
$woocommerce->show_messages();
}
else if ($woocommerce->cart->cart_contents_total >= 200) {
$woocommerce->cart->add_discount($coupon_code5);
$woocommerce->show_messages();
}
}
很好的答案,謝謝 – 2015-06-24 12:35:31