我正在尝试使用预定义的板作为数组来获取折扣数(值)。
例如,如果总产品数量在 11 到 20 之间,那么我想返回 25 作为折扣值。
这可能很简单,但我不知道该怎么做。因为简单的 foreach 循环可能不起作用。
/**
* The function returns the discount amount from the slabs
*
* @param int $products_count total number of product in cart
*
* @return mixed null|int returns discount value if matches else null
*/
public function product_discounts($products_count)
{
$discount_slabs = [
'10' => '15',
'20' => '25',
'30' => '35',
'50' => '50',
];
foreach ($discount_slabs as $count => $discount) {
if ($products_count <= $count) {
$this->discount = $discount;
}
}
return $this->discount;
}
扬帆大鱼