Quantcast
Channel: Using Optimizely Platforms
Viewing all articles
Browse latest Browse all 8020

How to create a Variant programmatically with price together

$
0
0
Hi,

In our project we have a validation rule to ensure that every variant has at least one price assigned to it.

I am trying to create a variant in code, but I cannot create a price for it before it is being saved in the database. How can I achieve this? My code is something like:

            var newVariant = _contentRepository.GetDefault < MyVariant > (anotherVariant.ParentLink, anotherVariant.ContentTypeID, anotherVariant.Language);
            .... setting Name, Code etc.
            Price existingPrice = anotherVariant.GetDefaultPrice();
            PriceDetailValue priceValue = new PriceDetailValue
            {
                CatalogKey = new CatalogKey(newVariant .Code),
                MarketId = existingPrice.MarketId,
                CustomerPricing = CustomerPricing.AllCustomers,
                ValidFrom = DateTime.Now,
                MinQuantity = 0,
                UnitPrice = new Money(existingPrice.UnitPrice.Amount + 1, existingPrice.UnitPrice.Currency)
            };
            _priceDetailService.Save(priceValue);
            _contentRepository.Publish(newVariant);

I get a foreign key violation exception at the IPriceDetailService.Save() call, that the code does not exist in DB. I could not find any other way to get a hold on to a PriceReference. There must be a way how the Commerce UI creates a new variant in one step, because that one is working fine. Using Commerce 12.14.


Viewing all articles
Browse latest Browse all 8020

Trending Articles