Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

OrderBy price commerce results

Vote:
 

Have Googled and browsed around some topics but not sure I have found a conclusive answer to what I thought should be a simple question:

Can I sort Find results of commerce variations by price?

As prices are nested objects I can't see a way to OrderBy them natively with the Find API.. I have also looked at https://github.com/x2find/Nested2Find which might work (but I haven't tried this yet).

Hopefully this is a simple question for someone :)

Matt

#149392
May 31, 2016 13:46
Vote:
 

In theory, it won't work, as decimal was not supported by current EPiFind (or ElasticSearch)

#149553
Jun 03, 2016 5:24
Vote:
 

Thanks for a reply - at least I won't burn any more time on a dead end :)

#149607
Jun 06, 2016 4:27
Vote:
 

If I was desperate it would be interesting to know if simply modifying the indexing to round prices to ints would allow sorting.

#149608
Jun 06, 2016 4:29
Vote:
 

My current workaround is to use "Double" in my index, sorting worked as expected. 

#149609
Jun 06, 2016 5:40
Vote:
 

As of Find 12 you can sort on a decimal property. As for nested you currently have to specify:

.OrderBy(x => x.Players, p => p.Salary, null /* optional filter */, SortMissing.First, SortOrder.Ascending, SortMode.Min)

where SortMode.Mix/Max/Avg/Sum specifies if you want sorting to be done for the minimum, maximum, average or sum of the nested object values.

/Henrik

#149857
Jun 09, 2016 8:37
Vote:
 

Just to report back, since Episerver.Find.Commerce just got updated to support Find v12, I just gave this price sorting a go and it works perfectly!

In our case we actually have a complex nested filter for determining a customer's price - and I've used this code here which behaves exactly how we intended:

query.OrderByDescending(v => v.Prices(), p => p.UnitPrice.Amount, price =>
                (
                    price.CustomerPricing.PriceTypeId.Match(CustomerPricing.PriceType.UserName) & price.CustomerPricing.PriceCode.Match(accountNumber)
                    | price.CustomerPricing.PriceTypeId.Match(CustomerPricing.PriceType.PriceGroup) & price.CustomerPricing.PriceCode.Match(priceGroup)
                )
                & price.ValidFrom.LessThan(currentDate)
                & (price.ValidUntil.Exists() | price.ValidUntil.GreaterThan(currentDate))
                & price.UnitPrice.Currency.CurrencyCode.Match(customerContact.PreferredCurrency)
                & (price.MarketId.Value.Match(currentMarket.MarketId.Value) | price.MarketId.Value.Match(string.Empty)))
#150208
Jun 14, 2016 3:30
Vote:
 

Great!

I really appreciate the positive feedback. #happydev :-)

/Henrik

#150213
Jun 14, 2016 8:52
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.