1 回答

TA貢獻1799條經驗 獲得超9個贊
您只需要在循環中返回折扣,這樣當您找到適合產品計數的最小平板時,您就可以停止查看后續值。否則所有值將返回 50%。如果您跳出循環,則用戶擁有超過 50 個產品,他們將獲得最后一個折扣值,這是最大的一個。像這樣的東西:
public function product_discounts($products_count)
{
$discount_slabs = [
'10' => '15',
'20' => '25',
'30' => '35',
'50' => '50',
];
// set the base discount
$this->discount = 0;
foreach ($discount_slabs as $count => $discount) {
if ($products_count <= $count) {
// if less than this bracket, return the current discount
return $this->discount;
}
// otherwise, increase the discount level
$this->discount = $discount;
}
return $this->discount = $discount;
}
- 1 回答
- 0 關注
- 144 瀏覽
添加回答
舉報