Answer the question
In order to leave comments, you need to log in
How to formulate a discount system correctly?
Good afternoon, can you advise? i have an array so i output from the model
$model = Yii::app()->db->createCommand()->
select( '*' )->
from('DiscountRegular')->
where('active = "1"')->
queryAll();
array(2) {
[0]=>
array(7) {
["id"]=>
string(1) "1"
["name"]=>
string(6) "7roses"
["active"]=>
string(1) "1"
["sum"]=>
string(2) "10"
["start_date"]=>
string(19) "2018-10-25 13:40:07"
["end_date"]=>
string(19) "2018-11-30 09:45:28"
["price"]=>
string(3) "351"
}
[1]=>
array(7) {
["id"]=>
string(1) "2"
["name"]=>
string(1) "2"
["active"]=>
string(1) "1"
["sum"]=>
string(2) "20"
["start_date"]=>
string(19) "2018-10-30 10:25:40"
["end_date"]=>
string(19) "2018-11-30 10:25:40"
["price"]=>
string(3) "250"
}
}
if($result == $model[0]['price'] or $result > $model[0]['price']){
public function checkDiscount($price)
{
$model = Yii::app()->db->createCommand()->
select( '*' )->
from('DiscountRegular')->
where('active = "1"')->
queryAll();
$regular_discount = DiscountRegular::calculateDiscount();
$result = preg_replace("/\..+/", "", $regular_discount);
if($result == $model[0]['price'] or $result > $model[0]['price']){
$timestamp_start = strtotime($model[0]['start_date']);
$timestamp_stop = strtotime($model[0]['end_date']);
if($timestamp_start <= time() && $timestamp_stop >= time() && $model[0]['active'] == '1')
{
$percent = $model[0]['sum'];
$number_percent = $price / 100 * $percent;
$result = $price - $number_percent;
return $result;
}else{
return false;
}
}else{
return false;
}
}
Answer the question
In order to leave comments, you need to log in
First, you can tell what's going on here (and what happens at this stage):
$regular_discount = DiscountRegular::calculateDiscount();
$result = preg_replace("/\..+/", "", $regular_discount);
although the indicated amount of purchases is 351, the discount for 250 does not workshould this apply? Your condition says unequivocally - discount = 20%, at 250<=amount<351; discount = 10%, if the amount>=351. Ie, kmk, you formulated the problem incorrectly.
$applicableDiscounts = $queryResults; //результаты запроса выше
$currentPercentage = 0;
$prevMinOrderSum = 0; //нужно для запоминания текущих границ скидки, чтобы не применялась тупо последняя выбранная из бд скидка
foreach ($applicableDiscounts as $discountData) {
if ($price >= $discountData['minOrderSum'] && $discountData['minOrderSum'] > $prevMinOrderSum) { //где $price - сумма вашего заказа
$currentPercentage = $discountData['percentage'];
$prevMinOrderSum = $discountData['minOrderSum'];
}
}
$finalPrice = $price * (1 - $currentPercentage / 100);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question