Catalog Library
The catalog Library in Ucommerce allows developers to retrieve any information required to display in browse context of a webhop concering navigation, price information url's for products and related products. This article describes every method available and when to use them.
Getting all catalogs
If you have more than one catalog in your solution, you might want to display them in various ways instead of just resolving the current one from the CatalogContext. To achive this simply call GetCatalogs, which will fetch them all for you from the current shop, resolved from the CatalogContext.
List<ProductCatalog> catalogs = UCommerce.Api.CatalogLibrary.GetAllCatalogs(); foreach (ProductCatalog productCatalog in catalogs) { // ... }
Getting a specific Catalog
You can get the current catalog by calling the code below. The catalog is resolved by the CatalogContext.
ProductCatalog catalog = UCommerce.Api.CatalogLibrary.GetCatalog();
If you want a specific one you can use the code below, which resolves a catalog with the id of the input.
catalog = UCommerce.Api.CatalogLibrary.GetCatalog(1337);
Getting root categories for navigation
A common feature of a webshop is to display categories and sub categories in various ways. In all cases you might want to display all categories containing sub categories and / or products.
ICollection<Category> rootCategories = UCommerce.Api.CatalogLibrary.GetRootCategories(); foreach (Category rootCategory in rootCategories) { // ... }
The code above gives you every category in the current catalog which is resolved from the CatalogContext in Ucommerce. If you want to display rootCategories from another catalog than the current one, this can be achieved by specifying an id for that specific catalog:
rootCategories = UCommerce.Api.CatalogLibrary.GetRootCategories(1337);
Getting products for a category
To get all prodcts in a category, use the code below. It resolves every product in the category and eager loads product properties, product prices and the definition.
//category resolved from the CatalogContext Category category = SiteContext.Current.CatalogContext.CurrentCategory; ICollection<Product> products = UCommerce.Api.CatalogLibrary.GetProducts(category); foreach (Product product in products) { // ... }
Getting related products for a product
Getting related products for a product takes either the SKU or the Id of the product. It returns a dictionary with relation type as the key and every related product of that type as the value.
Both methods takes a relation type as an optional parameter to retrieve specefic relations if those are grouped into different ones.
Dictionary<ProductRelationType, List<Product>> productRelationsBySku = UCommerce.Api.CatalogLibrary.GetRelatedProducts("MySku"); Dictionary<ProductRelationType, List<Product>> productRelationsById = UCommerce.Api.CatalogLibrary.GetRelatedProducts(1337); Dictionary<ProductRelationType, List<Product>> productRelationsByIdAndRelationType = UCommerce.Api.CatalogLibrary.GetRelatedProducts(1337,"Up-sale"); foreach (ProductRelationType relationType in productRelationsBySku.Keys) { List<Product> products = productRelationsBySku[relationType]; }
Getting a specific product by Sku or Id
A specific product can be retrieved by Sku or Id. It resolves the entire product with all associations and properties.
Product productById = UCommerce.Api.CatalogLibrary.GetProduct(1337); Product productBySku = UCommerce.Api.CatalogLibrary.GetProduct("MySku");
Getting Nice Urls for catalog objects
In Ucommerce There's a nice Url for every catalog object in the catalog section. The URls are created in three different levels:
- Url for a catalog
- Url for a category
- Url for a product
To retrieve those use the following approach:
ProductCatalog catalog = SiteContext.Current.CatalogContext.CurrentCatalog; Category category = SiteContext.Current.CatalogContext.CurrentCategory; Product product = SiteContext.Current.CatalogContext.CurrentProduct; string urlForCatalog = UCommerce.Api.CatalogLibrary.GetNiceUrlForCatalog(catalog); string urlForCategory = UCommerce.Api.CatalogLibrary.GetNiceUrlForCategory(category); urlForCategory = UCommerce.Api.CatalogLibrary.GetNiceUrlForCategory(category,catalog); string urlForProduct = UCommerce.Api.CatalogLibrary.GetNiceUrlForProduct(product); urlForProduct = UCommerce.Api.CatalogLibrary.GetNiceUrlForProduct(product,category); urlForProduct = UCommerce.Api.CatalogLibrary.GetNiceUrlForProduct(product, category,catalog);
The above code shows how to retrieve urls for catalog,categories and products. For a category and product, optional parameters is available if you want to control the full Url. Otherwise the first catalog and category is specified.
Getting a Price calculation for a product
In Ucommerce a price is associated with a product and a pricegroup. The priceGroup is resolved from CurrentPriceGroup in the CatalogContext, which as out-of-the-box is configured on a Catalog. This is why CalculatePrice takes a product and a catalog.
The object returned is a PriceCalculation which holds Discounts, ListPrices, the price the user should pay and whether it is discounted or not. The actual price of the different price calculations is a money object, which is rendered in the frontend with the correct currency symbol.
ProductCatalog catalog = SiteContext.Current.CatalogContext.CurrentCatalog; Product product = SiteContext.Current.CatalogContext.CurrentProduct; PriceCalculation priceCalculation = UCommerce.Api.CatalogLibrary.CalculatePrice(product, catalog); PriceCalculation.Price listPrice = priceCalculation.ListPrice; PriceCalculation.Price discount = priceCalculation.Discount; PriceCalculation.Price yourPrice = priceCalculation.YourPrice; Money tax = priceCalculation.YourTax; Money amountInclTax = yourPrice.AmountInclTax; Money amountWithoutTax = yourPrice.AmountExclTax; Money amount = yourPrice.Amount; bool isDiscounted = priceCalculation.IsDiscounted;
Changing pricegroup dynamically
The catalogLibrary allows for changing PriceGroup dynamically. This can be achieved by Specifying a new one.
PriceGroup newPriceGroup = PriceGroup.FirstOrDefault(x => x.Name == "new pricegroup"); UCommerce.Api.CatalogLibrary.ChangePriceGroup(newPriceGroup, changeBasketBillingCurrency: true);
The optinal parameter changeBasketBillingCurrency, will as default try to update the basket for the user, reflecting new prices for the pricegroup. It is required that every product in the basket has prices for the pricegroup that the basket is being changed into.