Product Price Update

Vote:
 

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).

#191699
Apr 26, 2018 17:16
Vote:
 

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.

#191716
Edited, Apr 27, 2018 1:52
Vote:
 

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);
#191733
Apr 27, 2018 7:58
Vote:
 

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.

#191742
Edited, Apr 27, 2018 10:34
Vote:
 

Thank you, Jafet.

I will try and let you know.

#191749
Apr 27, 2018 11:11
Vote:
 

Hi Jafet,

I just update the old price by referring the below link.

https://world.episerver.com/forum/developer-forum/Episerver-Commerce/Thread-Container/2017/8/quotbest-praticequot-advice-for-updating-prices/

#191789
Apr 27, 2018 18:37
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.