公式优惠2×3×2等PHP
问题描述:
我如何做到这一点卡住思路: 我有购物车,我想创建一个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总价格是多少?
答
另一种解决方案:
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
答
没有测试过,但是这个功能应该工作:
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;
}
是该项目的价格很重要?您的示例显示具有相同价格的所有项目。如果我把两件物品放在5美元,两件放在6美元,会怎么样? – Gordon
对于需要使用系统的客户,我感到很遗憾,他们会以2的价格销售6件产品,但3件的价格为5件。 – hobbs