Get highest and lowest price of a Product

Vote:
 

Hi,

Using Commerce 12.3.1.

I am trying to find an efficient way to find the highest and lowest price of a product. For example:

Product - Tables (T001) has 3 variants under it; small (T001-SM) priced at $5, medium (T001-M)priced at $7 and large (T001-L) priced at $10. I want to get a range of the prices from minimum to maximum ($5 -$10). I am able to get the price of all variants using IPriceDetailService, but this will kill the website when it goes live. The next option is IPriceService, I know that it is cached and I am unable to get all the prices of  product. 

Below is the snippet using IPriceDetailService, it works but this is not recommended:

var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();

priceDetailService.List(content); //In this case I was passing the content reference of the product

Below is the snippet using IPriceService, but I am unable to get any prices as it gives a count of 0:

var priceService = ServiceLocator.Current.GetInstance<IPriceService>();
IContentLoader loader = ServiceLocator.Current.GetInstance<IContentLoader>();
var x1 = loader.Get<EntryContentBase>(content);
var priceList = priceService.GetCatalogEntryPrices(new CatalogKey(x1.Code));

Any guidance on this will be helpful.

#200734
Edited, Jan 23, 2019 22:05
Vote:
 

You can do that with IPriceService by using  the method 

  IEnumerable<IPriceValue> GetPrices(MarketId market, DateTime validOn, IEnumerable<CatalogKey> catalogKeys, PriceFilter filter);

or

  IEnumerable<IPriceValue> GetPrices(MarketId market, DateTime validOn, IEnumerable<CatalogKeyAndQuantity> catalogKeysAndQuantities, PriceFilter filter);

The parameter catalogKeys is all variant's codes of that product

#200743
Jan 24, 2019 5:15
Vote:
 

I'm glad that you are aware that the default implementation of IPriceDetailService is not cached, a few more comments:

  • You don't have to load the content to get the code from the content link. that is heavy and expensive. Use ReferenceConverter instead.
  • As Binh's comment. Use IRelationRepository.GetChildren<ProductVariation>(product.ContentLink) to get the variants of a product, then use ReferenceConverter to get the codes of those content links, then use IPriceService to load prices of them.
#200748
Jan 24, 2019 8:21