S
S
ssrdop2017-10-24 16:14:53
PHP
ssrdop, 2017-10-24 16:14:53

How to correctly generate the final price and build dynamic discounts?

For example, we have 2000 products on the site.
On certain days, there are discounts on certain products.
There are discounts at certain times.
Discounts on kits and more, that is, there can be many conditions.
How to program all this in terms of architecture so that you can easily remove it, add new discounts?
How can this be implemented for the end client in the admin panel?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
4
4X_Pro, 2017-10-24
@ssrdop

Firstly, I would make the iPriceCalc interface, and a set of classes that implement this interface, and all the calculations would be put into them. (Accordingly, the class should have one method that passes information about the product and, just in case, the logged in user.) Plus, just in case, I would provide for each product the ability to specify in the database which class should be used to calculate the price.
The editor of dynamic discounts would do this: a table with several selects in each row. In the first of them, we select what exactly we are checking (current date, current day of the week, current time, product price, category, etc.). In the second - the check operation (less than, greater than, equal to, not equal to). In the third - the value with which we compare.
At the end of the table - the size of the discount and its type (absolute in rubles or relative in percent).
We save all this into an array (using serialization in PHP or JSON) and pass it to the class that calculates the price. It goes through this array sequentially, takes each line, substitutes the real value instead of the first select, looks at which operation to apply and compares. If all conditions in the set are met, the discount is applied and the class returns the corrected price.
That is, we get something like this:

class ConditionPriceCalculator implements iPriceCalc {
function calc($product,$user) {
$data = get_file_contents('файл с условиями');
$conditions = unserialize($data);
$result = true;
foreach ($conditions as $condition) {
   // в поле comp_value лежит условное значение из первого selectа, для которого нужно посчитать реальное значение
   if ($condition['comp_value']=='day') $value1==date('d');
   elseif ($condition['comp_value']=='weekday') $value1=date('N');
  // ... и так далее
  $value=$condition['value']; // тут хранится, с чем сравнивать
  if ($condition['op']=='eq') $cur_res = $value1==$value2;
  if ($condition['op']=='ne') $cur_res = $value1!=$value2;
  //  и т.д.
  if (!$cur_res) $result = false; // если условие не выполнилось, общий результат сбрасываем в false, еще можно добавить break;
}
if ($result) {
  // если все условия из набора выполнились, рассчитываем новую цену:
}
}
}

In principle, it is possible to envisage that there are several such sets of conditions and that the first triggered one is applied.
The only limitation is that you cannot combine them in one set of conditions by "OR" and not by "AND".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question