【问题标题】:formula discount 2x1 3x2 etc PHP公式折扣 2x1 3x2 等 PHP
【发布时间】:2016-12-03 17:06:49
【问题描述】:

我一直在思考如何做到这一点: 我有一个购物车,我想创建一个具有 2x1、3x2、5x3 折扣等的优惠券系统。

但我无法解决此问题以获取公式并在应用优惠券后显示总价。

例如:商品价格:5 美元,我有一张 2x1 的优惠券:

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中如何使用优惠券获取总价?

【问题讨论】:

  • 商品价格重要吗?您的示例显示所有具有相同价格的项目。如果我以 5 美元的价格放置两件商品,以 6 美元的价格放置两件商品会怎样。
  • 我为那些不得不使用系统以 2 的价格向他们出售 6 件商品但以 3 的价格出售 5 件商品的客户感到抱歉。

标签: php formula shopping-cart discount coupon


【解决方案1】:

尚未测试,但此功能应该可以工作:

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;

}

【讨论】:

    【解决方案2】:

    另一种解决方案:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2018-02-26
      • 2013-09-08
      • 2011-11-08
      • 1970-01-01
      相关资源
      最近更新 更多