
Update prices properly in Episerver Commerce
Examples and valuable information about the price apis.
Published 30 nov 2020
Episerver Commerce 13
First of all, if you are working with Episerver Commerce, you should buy and read the ultimate guide Pro Episerver Commerce by Quan Mai (awesome guide with example of all parts of Episerver Commerce)
Thank you Quan Mai for these take aways!
Updating Prices
Pretty normal scenario is to have a scheduled job updating prices with for example updated prices or campaigns.
IPriceService
Did you know this?
- IPriceService is optimized for batch processing
- You save all prices of a SKU
- It optimizes prices before saving
- It reorders and sorts the prices in database table
- The prises are ordered by period, price and min quantity
- If there are overlapped periods, the lowest price will win
- Higher prices can be removed
- Prices time periods can be changed
Note: You may use IPriceDetailService to save prices as is, with no optimization, no cache, and it syncs well with IPriceService.
Use of CustomerPricing
You might have different prices for markets or currencies for an SKU, but you might also want to have different prices for different customers (or types of customers).
Quan Mai 2018 (Pro Episerver Commerce by Quan Mai – page 83 )

Sale Type and Sale Code could be used to list “Manufacturer suggested retail price” (MSRP) for display to user in front-end. In “Pro Episerver Commerce” page 90 you find a detailed example how to implement such.

Code sample batch saving price from ERP
In this sample i use salescode on CustomerPricing to name the different campaigns.
At the end i batch save all prices at once.
/// <summary> | |
/// Iterates the prices from ERP into Episerver Commerce | |
/// </summary> | |
/// <param name="variant"></param> | |
/// <param name="item">Custom Entity from ERP system</param> | |
private static void AddUpdatePrice(VariationContent variant, PriceMessage item) | |
{ | |
//no need to delete any prices, IPriceService optimizes the prices from the new prices | |
var priceService = ServiceLocator.Current.GetInstance<IPriceService>(); | |
List<IPriceValue> priceValues = new List<IPriceValue>(); | |
var catkey = new CatalogKey(variant.Code); | |
// Add changed prices | |
foreach (PriceRule rule in item.PriceRuleSet) // list of prices for this SKU | |
{ | |
//Find Campaign or regular price, this is a custom implementation, need to be changed to your custom entities | |
var price = rule.Prices.First(p => p.ValueTypeCode == "LRSEx:CampaignSalesUnitPrice" || p.ValueTypeCode == "RegularSalesUnitPrice"); | |
var priceValue = new PriceValue | |
{ | |
CatalogKey = catkey, | |
MarketId = MarketId.Default, | |
CustomerPricing = new CustomerPricing(CustomerPricing.PriceType.AllCustomers, rule.Description), // rule.Description is the campaign name | |
MinQuantity = 0m, | |
UnitPrice = new Money(price.Value, new Currency(price.Currency)), | |
ValidFrom = DateTime.UtcNow | |
}; | |
//add time stamps | |
if (rule.EffectiveDateTimestampSpecified) | |
priceValue.ValidFrom = rule.EffectiveDateTimestamp; | |
if (rule.ExpirationDateTimestampSpecified) | |
priceValue.ValidUntil = rule.ExpirationDateTimestamp; | |
priceValues.Add(priceValue); | |
} | |
if (priceValues.Any()) | |
{ | |
//batch save prices | |
priceService.SetCatalogEntryPrices(catkey, priceValues); | |
} | |
} |
About the author
Luc Gosso
– Independent Senior Web Developer
working with Azure and Episerver
Twitter: @LucGosso
LinkedIn: linkedin.com/in/luc-gosso/
Github: github.com/lucgosso