Sort By Price on Product Variation using Find Query

P
P
Vote:
 

Hi all!

We are using Catelog Product Model to search products and already had Sort by options with DisplayName and Relavance.

Now we would like to sort the results with Price(Asc & Dsc) wich is with variation. In our case We have developed query with  Catelog Product Model, 

So until we get the results of query we are not going to get the price of variation.

If anyone achieved this scenario please help us to break it.

Here is some code snippet:

query = query.OrderByDescending(p => p.DisplayName); //Used to sort with Display Name. P->Product  Model

We tried with 

query = query.OrderByDescending(p => p.GetProductPrice()); which is not effecting the results, because of not having price here.

var results = query
.ApplyBestBets()
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Track()
.GetContentResult();

Thank you!

#223113
Edited, May 19, 2020 7:18
Vote:
 

In that case, you can create your own custom OrderBy method.

In that method you can get all the variants and thne, you can do a FirstOrDefault for price (of all variants under that product) or you can take either max priced variant or the min priced variant. Then sort your product based on that variant price.

#223478
May 28, 2020 11:58
Vote:
 

If I understand the problem correctly I would solve it by: 

  1. Add new method to model Product: GetPrice() 
  2. In method add logic to fetch price
  3. Use conventions so that method is indexed
    SearchClient.Instance.Conventions.ForInstancesOf<ProductModel>().IncludeField(x => x.GetPrice());​
  4. Run indexing job
  5. Use new field for OrderBy
    query.OrderByDescending(p => p.GetPrice());​
#225149
Edited, Jul 07, 2020 6:27
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.