Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting for price does not use discounted prices but the original prices #1137

Open
Algorithman opened this issue Jul 9, 2024 · 3 comments
Assignees
Milestone

Comments

@Algorithman
Copy link
Contributor

Describe the bug
We have a category for our special sale products with discounts ranging from 10% to 40%.
But if we sort by price (up or down), the sorting does not sort by effective price but original price or the product.

Reproduced it in demo shop.

image

@Algorithman Algorithman added the bug label Jul 9, 2024
@muratcakir muratcakir added this to the 5.2.0 milestone Jul 9, 2024
@Algorithman
Copy link
Contributor Author

Instead of

            else if (sorting.FieldName == names.Price)
            {
                query = OrderBy(query, x => x.Price, sorting.Descending);
            }

I came up with this snippet which works for me, but it certainly needs more tests. I only applied one discount at a time.
And it can (probably) be improved.

                if (sorting.Descending)
                {
                    query = query
                        .OrderByDescending(x =>
                            x.HasDiscountsApplied && x.AppliedDiscounts.Any(discount =>
                                (!discount.StartDateUtc.HasValue || discount.StartDateUtc.Value <= DateTime.UtcNow) &&
                                (!discount.EndDateUtc.HasValue || discount.EndDateUtc.Value >= DateTime.UtcNow))
                                ? x.Price - x.AppliedDiscounts
                                    .Where(discount =>
                                        (!discount.StartDateUtc.HasValue || discount.StartDateUtc.Value <= DateTime.UtcNow) &&
                                        (!discount.EndDateUtc.HasValue || discount.EndDateUtc.Value >= DateTime.UtcNow))
                                    .Max(discount =>
                                        discount.UsePercentage
                                            ? x.Price * discount.DiscountPercentage / 100m
                                            : discount.DiscountAmount)
                                : x.Price);
                }
                else
                {
                    query = query
                        .OrderBy(x =>
                            x.HasDiscountsApplied && x.AppliedDiscounts.Any(discount =>
                                (!discount.StartDateUtc.HasValue || discount.StartDateUtc.Value <= DateTime.UtcNow) &&
                                (!discount.EndDateUtc.HasValue || discount.EndDateUtc.Value >= DateTime.UtcNow))
                                ? x.Price - x.AppliedDiscounts
                                    .Where(discount =>
                                        (!discount.StartDateUtc.HasValue || discount.StartDateUtc.Value <= DateTime.UtcNow) &&
                                        (!discount.EndDateUtc.HasValue || discount.EndDateUtc.Value >= DateTime.UtcNow))
                                    .Max(discount =>
                                        discount.UsePercentage
                                            ? x.Price * discount.DiscountPercentage / 100m
                                            : discount.DiscountAmount)
                                : x.Price);
                }

@mgesing
Copy link
Contributor

mgesing commented Jul 22, 2024

This could be challenging to solve completely (including MegaSearch) because therefore all prices for all published currencies have to be calculated and saved in the search index. The above sorting only applies to the standard search via Linq. It's only a "half solution" because price filtering is also affected. Several of the fields do not have a database index, which can affect performance.

@Algorithman
Copy link
Contributor Author

My snippet also only sorts by product applied discounts, category discounts are not taken into account.
But since we only use product discounts (10% 20% 30% 40% 50%) and no megasearch, it's enough for us. (category discounts probably have to be done in-memory anyways, which opens another can of troubles)

But overall I think it is something which should be addressed, since sort by price is often used by customers looking through sale categories or weekly specials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants