Created
November 29, 2020 17:36
-
-
Save LucGosso/2f45cf87da13144c999ef1dc3dd7dca1 to your computer and use it in GitHub Desktop.
Properly update price in episerver commerce https://devblog.gosso.se/?p=1486
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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