Take the community feedback survey now.
AI OnAI Off
Take the community feedback survey now.
var newVariant = _contentRepository.GetDefault < MyVariant > (anotherVariant.ParentLink, anotherVariant.ContentTypeID, anotherVariant.Language);
_contentRepository.Publish(newVariant);
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);
This should work
public class VariationContentValidator : IValidate < VariationContent >
{
public IEnumerable < ValidationError > Validate(VariationContent instance)
{
if (instance.GetPrices().Count == 0)
{
yield return new ValidationError
{
Severity = ValidationErrorSeverity.Error,
ErrorMessage = Custom.MissingPrice,
PropertyName = "Price"
};
}
}
}
Well that is a strange rule to have. But you perhaps you can skip the check if VariationContent.ContentLink is null or empty (i.e. it's not created yet).
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.