PHP - 对可被 2 整除的商品应用折扣

PHP - apply discount to items divisible by 2

我正在使用一些代码,如果以可被 2 整除的倍数购买某件商品,该代码将对某件商品应用折扣,如果我输入任何可被 2 整除的数字,此方法有效。

问题是如果用户输入说 3,我仍然希望应用折扣,因为他们已经购买了 2 个加另一个。

解决这个问题的最佳方法是什么?这是我正在使用的代码:

if($price == 11.99 && $each_item['quantity'] % 2 == 0)
{   
    $toAdd = 3.98 * ($each_item['quantity'] / 2);

    $discount += $toAdd; 

    $priceTotal = $price * $each_item['quantity'];

    $priceTotal -= $discount;

    $finalDiscount += $discount;
}

将数量除以 2,然后四舍五入为整数以获得要应用的折扣数。

if ($price == 11.99) {
    $to_add = 3.98 * floor($each_item['quanity']/2);
    $discount += $toAdd;
    $priceTotal = $price * $each_item['quantity'];
    $priceTotal -= $discount;
    $finalDiscount += $discount;
}