https://world.episerver.com/documentation/developer-guides/commerce/catalogs/Importing-catalog-data/
Some methods used in this code are not working(like:- _linksRepository.UpdateRelation)
private readonly IContentRepository contentRepository;
private readonly ILinksRepository linksRepository;
private readonly IInventoryService inventoryService;
private readonly IPriceDetailService priceDetailService;
private void AddEntry()
{
ContentReference parentCategoryLink; // Get a reference to the node you want to add the product to.
// Add product
var product = contentRepository.GetDefault<MyCustomProduct>(parentCategoryLink, CultureInfo.GetCultureInfo("en-US"));
product.Name = "Product name";
product.Code = "123-456";
product.Publisher = "Episerver";
contentRepository.Save(product, SaveAction.Publish, AccessLevel.NoAccess);
// Add variation
var variation = contentRepository.GetDefault<MyCustomVariation>(parentCategoryLink, CultureInfo.GetCultureInfo("en-US"));
variation.Name = "Variation name";
variation.Code = "654-321";
variation.Type = "Paperback";
variation.MaxQuantity = 50;
contentRepository.Save(variation, SaveAction.Publish, AccessLevel.NoAccess);
// Add relation between product and variant
var newVariation = new ProductVariation
{
SortOrder = 1,
Source = product.ContentLink,
Target = variation.ContentLink
};
linksRepository.UpdateRelation(newVariation);
// Add inventory information
var inventoryRecord = new InventoryRecord
{
CatalogEntryCode = variation.Code,
PurchaseAvailableQuantity = 10,
PurchaseAvailableUtc = DateTime.UtcNow,
WarehouseCode = "default"
};
inventoryService.Insert(new[] { inventoryRecord });
// Add price information
var priceDetailValue = new PriceDetailValue
{
CatalogKey = new CatalogKey(AppContext.Current.ApplicationId, variation.Code),
MarketId = new MarketId("US"),
CustomerPricing = CustomerPricing.AllCustomers,
ValidFrom = DateTime.UtcNow,
UnitPrice = new Money(1m, Currency.USD)
};
priceDetailService.Save(priceDetailValue);
}