2017-04-23 124 views
1

在我的WooCommerce網站中,我有幾個產品以相同的價格80 $
我想添加一個折扣的產品數量。WooCommerce:根據單個項目數量添加折扣

的邏輯是這樣的:

if (Products Quantity is 2){ 
    // the original product price change from 80$ to 75$ each. 
} 

if(Products Quantity is 3 or more){ 
    //the original product price change from 80$ to 70$ each.  
} 

例如,

如果顧客挑選2個產品,原價將(80$ x 2) =>160$
但折扣後,它將是:(75$ x 2) =>150$

而且......

如果遊客挑選3個款產品,原價將(80$ x 3) =>240$
但收費後,它將是:(70$ x 3) =>210$

請幫忙嗎?

謝謝

回答

2

這個自定義掛鉤函數應該做你期望的。您可以根據個別料品數量在其中設定您的累進折扣限額

這裏是你的活躍兒童主題(或主題)的function.php文件中的代碼

## Tested and works on WooCommerce 2.6.x and 3.0+ 
add_action('woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1); 
function progressive_discount_by_item_quantity($cart_obj) { 

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

    # Progressive quantity until quantity 3 is reached (here) 
    # After this quantity limit, the discount by item is fixed 
    # No discount is applied when item quantity is equal to 1 

    // Set HERE the progressive limit quantity discount 
    $progressive_limit_qty = 3; // <== <== <== <== <== <== <== <== <== <== <== 

    $discount = 0; 

    foreach($cart_obj->get_cart() as $cart_item_key => $item_values){ 

     $qty = $item_values['quantity']; 

     if($qty =< $progressive_limit_qty) 
      $param = $qty; // Progressive 
     else 
      $param = $progressive_limit_qty; // Fixed 

     ## Calculation ## 
     $discount -= 5 * $qty * ($param - 1); 
    } 

    if($discount < 0) 
     $cart_obj->add_fee(__('Quantity discount'), $discount); // Discount 

} 

代碼去或者還有任何插件文件。

經過測試,適用於WooCommerce 2.6.x和3.0+