K
K
Kirill Gorelov2020-10-11 20:13:51
1C-Bitrix
Kirill Gorelov, 2020-10-11 20:13:51

Bitrix, discounts for different users + sorting?

Hello.

You need to make several discount options for different user groups.
Purely theoretical, they are ready, but there is a problem.

Sort items by discounted price.
Now it works like this: I created a new property (numeric), and there I write down the final price with a discount.
And sorted by it. But now the question. How to do if I have several groups?
Do not create a new property for each group and sort by it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-10-15
Madzhugin @Suntechnic

Since we didn't wait for anything, here's my answer:
Since discounts are calculated at PHP runtime, there's no way you can order items by discounted price in the query, and sorting must be done AFTER the discount price is calculated.
Worse, to build each page, you have to extract ALL products, calculate the discounted price for ALL products, sort the array at runtime, and select the products for the page from it.
This is a very expensive operation that cannot be bypassed in any way. Everything will be fine until you have a lot of goods or very rare changes in them, but as soon as there are a lot of goods, all these calculations of yours will cause the server to consume all the memory and processor.
Therefore, we need a cache. And it was not bad to make this cache as narrow as possible in order to update it as rarely as possible. And it is better to calculate directly when adding a new product and preferably only for it.
What to store in this cache? It turns out just pre-calculated prices with a discount ...
And now we are back exactly here:

How to do if I have several groups?
Do not create a new property for each group and sort by it?

That's exactly what to do. If there are relatively few groups. Let's say up to 2-3 dozen such a solution will still be optimal and fastest.
Updating the values ​​hang on the product/element update on events.
If there are many groups, do the same but create a separate table and make joins when selecting.
And compare the speed of both solutions - from a certain point, with the growth of properties, the second one will become a little faster, it seems to me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question