每位客户购买5个产品,他们将获得2个产品的奖励。
但是,每个客户购买10种产品,他们将获得6种产品的奖金。
现在的问题是,如果有客户购买了 26 种产品怎么办?
所以条件将是:
客户购买
(10 x 2) //get 6 products x 2 = 12 products bonus/free
+
(5 x 1) // get 2 products x 1 = 2 products bonus/free
到目前为止,这是我的情况:
if ($quatity > 4 && $quatity < 10){
echo "Congrats, you get 2 products free"
}
if ($quatity > 9){
echo "Congrats, you get 6 products free"
}
如果只有客户购买超过 5 或 10 种产品,上面的代码对我工作正常。
但正如我上面提到的,如果客户购买超过 10 种产品,例如 26 种产品或 200 种产品怎么办?我是否应该手动编写 if 函数,例如 if ($quatity > 9){}
、if ($quatity > 19){}
、if ($quatity > 29){}
、if ($quatity > 39){}
等,
或
有人能想出简单的方法吗?
提前感谢!
这段代码会给你你想要的。
它将分解 10 种产品奖金,然后查看是否还有 5 种奖金可供赠送。
$tenBonus = (int)($quatity / 10);
$fiveBonus = $quatity % 10 > 4 ? 1 : 0;
$bonus = ($tenBonus * 5) + ($fiveBonus * 2);
if($bonus > 0) {
echo "Congrats, you get ". $bonus ." products free";
}