2016-07-29 47 views
0

我如何做到這一點卡住思路: 我有購物車,我想創建一個2×,3×2,5X3的折扣優惠券系統等公式優惠2×3×2等PHP

但我無法解決這個問題,得到一個公式,並顯示應用優惠券後的總價。

例如:商品價格:$ 5美金,我有一個優惠券2×1:

If I buy 2 items: TOTAL: $5,00 usd (2x1) 
If I buy 3 items: TOTAL: $10,00 usd (2x1 + 1) 
If I buy 4 items: TOTAL: $10,00 usd (2x1 + 2x1) 

以同樣的方式。項目價格:5美元。現在我有一張優惠券3x1。

If I buy 2 items: TOTAL: $10,00 usd (3x1 NOPE!) 
If I buy 3 items: TOTAL: $5,00 usd (3x1) 
If I buy 4 items: TOTAL: $10,00 usd (3x1 + 1) 
If I buy 5 items: TOTAL: $15,00 usd (3x1 + 2) 
If I buy 6 items: TOTAL: $10,00 usd (3x1 + 3x1) 
If I buy 7 items: TOTAL: $15,00 usd (3x1 + 3x1 + 1) 



如何獲取使用優惠券在PHP總價格是多少?

+0

是該項目的價格很重要?您的示例顯示具有相同價格的所有項目。如果我把兩件物品放在5美元,兩件放在6美元,會怎麼樣? – Gordon

+0

對於需要使用系統的客戶,我感到很遺憾,他們會以2的價格銷售6件產品,但3件的價格爲5件。 – hobbs

回答

0

另一種解決方案:

function calc($item_count, $unit_price, $coupon) 
{ 
    list($need, $paid) = explode('x', $coupon); 
    $left = $item_count % $need; 
    return $unit_price * (intval($item_count/$need) * $paid + $left); 
} 
+0

工作完美。非常感謝你!!! – Nicomuniz

0

沒有測試過,但是這個功能應該工作:

function discount($i_boughtitems,$i_necessaryitems,$i_discountitems,$i_priceofitem){ 
$i_priceofcart = 0; 

while($i_boughtitems => $i_necessaryitems){ 
    $i_priceofcart = $i_priceofcart+($i_priceofitem *$i_necessaryitems); 
    $i_boughtitems = $i_boughtitems - $i_necessaryitems; 
} 
$i_priceofcart = $i_priceofitem * $i_boughtitems; 
return $i_priceofcart; 

}