2014-10-26 149 views
4

我一直在寻找一段时间,但我无法找到如何从程序上删除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(); 
    } 

} 

回答

5

要使用它的优惠券代码使用WC_Cart->remove_coupon($code)车删除单个券。

从推车上删除所有的优惠券,你会用WC_Cart->remove_coupons($type) - $type默认为null所有,传递“购物车”的税收优惠券税后优惠券,“订单”前,必须删除。

要获得购物车中的所有优惠券作为array,您可以循环并可选择删除,使用WC_Cart->get_coupons()

for (WC()->cart->get_coupons as $code => $coupon){ 
    $valid = ? // decide to remove or not 
    if (! $valid){ 
     WC()->cart->remove_coupon($code); 
    } 
} 
+1

很好的答案,谢谢 – 2015-06-24 12:35:31

4

购物车方法remove_coupons()已经更新,因此不再需要类型。现在,删除所有的优惠券,这将这样的伎俩:

WC()->cart->remove_coupons();

欲了解更多信息,检查出的WC_Cart here类的文档。

+0

谢谢!不知道这个:) – 2016-05-11 16:47:01

+0

这已经是这种情况,'$ type'默认为'null'。 – doublesharp 2016-08-26 19:02:41

相关问题