dot_0620 2016-07-29 05:25 采纳率: 100%
浏览 17
已采纳

公式折扣2x1 3x2等PHP

I'm stuck thinking about how to do this: I have a shopping cart and I want to create a coupon system with 2x1, 3x2, 5x3 discount, etc.

But I can't solve this to get a formula and show the total price after apply the coupon.

For example: item price: $5 usd and I have a coupon 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)

In the same way. Item price: $5 usd. and now I have a coupon 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)



How to obtain the total price using the coupon in PHP?

  • 写回答

2条回答 默认 最新

  • drqrdkfue521903877 2016-08-02 08:15
    关注

    Another solution:

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?