Create a product using the .NET API
Creating a product using the .NET API can come in handy in many situations. Usually because you want to import products from a 3rd party system. Luckily this is quite straight forward using the Entities in Ucommerce. This article will cover how to achieve this.
A word on performance
Creating a product using the following techniques will get you going fast and easy. But you need to consider performance and the amount of data you have. If you know that you're working with a lot of data, you might want to consider important using the stateless API which is a little harder to manage but will come with a greater benefit in performance.
Creating the product
The product itself will contain some base properties needed before you can save it. Once this is done you will be able to save it, but before you do, you're probably interested in setting additional data.
NHibernate will automatically cascade-save related data if you're using the stateful API, which is why we're waiting with the save until the end. If you're interested in the most performant way, you need to look up how to use the bulk import API.
Ucommerce.EntitiesV2.Product product = new Product() { Sku = "SKU-123456", Name = "My Product", AllowOrdering = false, DisplayOnSite = false, };
Setting the Definition
Once you have your product created you need to set the definition of the product. This determines the custom data you will be able to set for your products.
We will just assume that the definition already exists in the system for simplicity. You can query the definition by name, or rather id or guid if you know this in advance.
You can read about definitions if you are unsure what to do with them for your products.
Ucommerce.EntitiesV2.IRepository<ProductDefinition> productDefinitionRepository = ObjectFactory.Instance.Resolve<IRepository<Ucommerce.EntitiesV2.ProductDefinition>>(); Ucommerce.EntitiesV2.ProductDefinition productDefinition = productDefinitionRepository.SingleOrDefault(x => x.Name == "Shirt"); product.ProductDefinition = productDefinition;
Language Invariant Properties
Setting invariant properties can be done using the following approach. Please note it is important that the definition is set in advance like above. Ucommerce will Automatically figure out the rest.
product.GetProperty("PropertyName").SetValue("PropertyValue");
Language specific properties
Setting language specific properties are done in a similar fashion, however a culture code needs to be specified as well. Languages in Ucommerce are determined by the CMS and as such you can specify any culture code.
product.GetProperty("PropertyName", "en-US").SetValue("PropertyValue");
Localized Product Description
To add a localized description for the product, create a new instance of the UCommerce.EntitiesV2.ProductDescription class.
Product descriptions in Ucommerce are localized descriptions of the product with Display name, long and short description.
product.AddProductDescription(new ProductDescription { Product = product, CultureCode = "en-US", // or en-GB, da-DK etc. DisplayName = "My display name", ShortDescription = "My short description", LongDescription = "My long description" });
Pricing Information
To save a price for a product, you find the ID of the price group you want the price to belong to. After that, you can create a new Price and ProductPrice.
Ucommerce.EntitiesV2.PriceGroup priceGroup = Ucommerce.EntitiesV2.PriceGroup.SingleOrDefault(x => x.Name == "DKK"); decimal yourPrice = 1337m; var productPrice = new Ucommerce.EntitiesV2.ProductPrice() { MinimumQuantity = 1, //determine the tier Guid = Guid.NewGuid(), Product = product, Price = new Price() { Amount = yourPrice, Guid = Guid.NewGuid(), PriceGroup = priceGroup } }; product.ProductPrices.Add(productPrice);
Note the MinimumQuantity property on ProductPrice! This is due to our Tier Pricing engine. If you are not utilizing this, and you want to set a default price, simply set the value to 1 as in the example above. If you are looking to create tier prices, simply create multiple ProductPrices and Prices with the same Product and PriceGroup only a different MinimumQuantity and Amount. Please read about the Tier Pricing and pricing structure to learn more.
Category Association
Last, but not least, you probably want to add the product to one or more categories. This is done using the CategoryProductRelation class.
Ucommerce.EntitiesV2.Category category = Ucommerce.EntitiesV2.Category.SingleOrDefault(x => x.Name == "Shoes"); var sortOrder = 0; product.AddCategory(category,sortOrder);