Take the community feedback survey now.
Take the community feedback survey now.
Not sure if I understood your question correctly, but prices are placed on variant level, not product level.
If you're going to have the same price for all variants on a product you'll have to loop through all the variants and add the price to each of them and save them using the PriceDetailService.Save()-method.
To get the content references of all the variants of a product you can call the ProductContent.GetVariants()-method.
Updating customer group data is just a matter of running PriceDetailService.List and selecting an appropriate IPriceDetailValue.
I wrote together some sample code from what I gathered from your question it will obviously need editing but just to give you an idea:
// Inject these:
IPriceDetailService _priceDetailService;
IContentRepository _contentRepository;
public void CreateOrUpdateCustomerGroupPrice(ProductContent product, string customerGroupName, Money price, MarketId market)
{
var variantReferences = product.GetVariants();
foreach (var variantReference in variantReferences)
{
// Gets current read only prices.
var variantPrices = _priceDetailService.List(variantReference);
var currentPrice = variantPrices.FirstOrDefault(
x =>
x.CustomerPricing.PriceTypeId == CustomerPricing.PriceType.PriceGroup &&
x.CustomerPricing.PriceCode == customerGroupName);
CatalogKey catalogKey; // we need to either generate this or copy it from the current price
if (currentPrice == null)
{
// lot's of loading for a catalogkey, maybe needs another approach.
var variant = _contentRepository.Get<VariationContent>(variantReference);
catalogKey = new CatalogKey(variant.LoadEntry());
}
else
{
catalogKey = currentPrice.CatalogKey;
// Delete the old price
_priceDetailService.Delete(currentPrice.PriceValueId);
}
// Mostly copied from world sample code. Set values to whatever you need here, parameterize the ones that aren't static.
var newPriceValue = new PriceDetailValue
{
CatalogKey = catalogKey,
MarketId = market,
CustomerPricing = new CustomerPricing(CustomerPricing.PriceType.PriceGroup, customerGroupName),
ValidFrom = DateTime.UtcNow.AddDays(-7),
ValidUntil = DateTime.UtcNow.AddDays(7),
MinQuantity = 0m,
UnitPrice = price
};
// Save the new price
_priceDetailService.Save(newPriceValue);
}
}
Disclaimer: This code is untested and is only intended as a sample solution.
Hi Jafet,
Thank You for your response.
I too used the same procedure what you did.
While Insert the price we first delete old price right. That's an actual problem I need to update 10k products means I have to delete the old price and add the new one.
Is there any way to update the price?
_priceDetailService.Delete(currentPrice.PriceValueId);
Ok, try the constructor overload of PriceDetailValue that accepts an IPriceDetailValue instead:
// Inject these:
IPriceDetailService _priceDetailService;
IContentRepository _contentRepository;
public void CreateOrUpdateCustomerGroupPrice(ProductContent product, string customerGroupName, Money price, MarketId market)
{
var variantReferences = product.GetVariants();
foreach (var variantReference in variantReferences)
{
// Gets current read only prices.
var variantPrices = _priceDetailService.List(variantReference);
var currentPrice = variantPrices.FirstOrDefault(
x =>
x.CustomerPricing.PriceTypeId == CustomerPricing.PriceType.PriceGroup &&
x.CustomerPricing.PriceCode == customerGroupName);
PriceDetailValue newPriceValue;
if (currentPrice == null)
{
// lot's of loading for a catalogkey, maybe needs another approach.
var variant = _contentRepository.Get<VariationContent>(variantReference);
// Set values to whatever you need here, parameterize the ones that aren't static.
newPriceValue = new PriceDetailValue
{
CatalogKey = new CatalogKey(variant.LoadEntry()),
MarketId = market,
CustomerPricing = new CustomerPricing(CustomerPricing.PriceType.PriceGroup, customerGroupName),
ValidFrom = DateTime.UtcNow.AddDays(-7),
ValidUntil = DateTime.UtcNow.AddDays(7),
MinQuantity = 0m,
UnitPrice = price
};
}
else
{
newPriceValue = new PriceDetailValue(currentPrice); // Copies the previous one including it's priceValueId
newPriceValue.UnitPrice = price;
}
// Save the new price
_priceDetailService.Save(newPriceValue); // Updates or creates the price based on if newPriceValue already has a priceValueId set or not
}
}
Disclaimer: This code is untested and is only intended as a sample solution.
Hi everyone,
I am using E-commerce site. So, I need to update the product price values frequently. I try to update the product price. But it's not worked.
I just referred the following link,
https://world.episerver.com/documentation/Items/Developers-Guide/Episerver-Commerce/9/Pricing/Pricing-examples/
Still, now, I used to delete the product price before update the new price value.
Is there any way to update the price based on product Customer Group( Sale Code).