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 关注
- 125 浏览
添加回答
举报