购物车中每个NTH商品的自定义折扣

问题描述:

我从this question here来到这里寻找我的WooCommerce网站的解决方案。我正在寻找一种方法,只在购物车中有数量的物品时才能在购物车中获得折扣。购物车中每个NTH商品的自定义折扣

例如:

如果只有1购物车中物品:全价
2个项目在购物车: - (减) $ 2两的它在车
5项: - (减) $ 2的它4(如果有奇数个在车中的项目1项将始终拥有全价)。

顺便说一句,我所有的产品都具有相同价钱。

有人能帮助我,因为有人帮助我提到的问题链接上的人?

这里是你的自定义功能woocommerce_cart_calculate_fees行动挂钩钩住,会做你期待什么:

add_action('woocommerce_cart_calculate_fees','cart_conditional_discount', 10, 1); 
function cart_conditional_discount($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    $cart_count = 0; 

    foreach($cart_object->get_cart() as $cart_item){ 
     // Adds the quantity of each item to the count 
     $cart_count += $cart_item["quantity"]; 
    } 

    // For 0 or 1 item 
    if($cart_count < 2) { 
     return; 
    } 
    // More than 1 
    else { 
     // Discount calculations 
     $modulo = $cart_count % 2; 
     $discount = (-$cart_count + $modulo); 

     // Adding the fee 
     $discount_text = __('Discount', 'woocommerce'); 
     $cart_object->add_fee($discount_text, $discount, false); 
    } 
} 

代码放在您的活动子主题(或主题)的function.php文件或也在任何插件文件中。

此代码已经过测试并可正常工作。

+0

我认为'$ discount'会是'$ discount =( - $ cart_count + $ modulo)* 2;'... – Reigel

+0

@Reigel问题不是很清楚......显然OP想要让一个累进折扣2 $ each 2 items ...我的代码适用于这种情况。 – LoicTheAztec

+0

是一致认为它不清楚。 – Reigel