Skip to content

Instantly share code, notes, and snippets.

@LucGosso
Created November 29, 2020 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucGosso/2f45cf87da13144c999ef1dc3dd7dca1 to your computer and use it in GitHub Desktop.
Save LucGosso/2f45cf87da13144c999ef1dc3dd7dca1 to your computer and use it in GitHub Desktop.
Properly update price in episerver commerce https://devblog.gosso.se/?p=1486
/// <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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment