Create assortments using API
This section explains how to create an assortment using the API.
Languages associated with an assortment defines in which languages the values of multilingual property fields will be for the products that are published in this assortment.
Following code creates an assortment with a given external id.
using Litium.Foundation;
using Litium.Foundation.Modules.ProductCatalog;
using Litium.Foundation.Modules.ProductCatalog.Assortments;
using Litium.Foundation.Modules.ProductCatalog.Carriers;
using Litium.Foundation.Security;
using System;
using System.Collections.Generic;
namespace Litium.Studio.KC.Samples.ProductCatalog
{
/// <summary>
/// Assortments
/// </summary>
public class Assortments
{
/// <summary>
/// Creates the assortment for KC samples.
/// </summary>
/// <param name="assortmentName">Name of the assortment.</param>
/// <param name="languageId">The language id.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
public static Assortment CreateAssortment(string externalId, SecurityToken token)
{
Assortment assortment = null;
if (Assortment.TryGet(externalId,out assortment, token))
return assortment;
//create the names, for this example we set the external id as name for all languages.
var names = new Dictionary<Guid, string>();
Solution.Instance.Languages.GetAllLanguages().ForEach(x=> names.Add(x.ID, externalId));
var assortmentCarrier = new AssortmentCarrier(names) { ExternalId = externalId };
assortment = ModuleProductCatalog.Instance.Assortments.Create(assortmentCarrier, token);
return assortment;
}
/// <summary>
/// Gets the assortment by the name.
/// </summary>
/// <param name="assortmentName">Name of the assortment.</param>
/// <param name="languageId">The language id.</param>
/// <param name="token">The token.</param>
/// <returns></returns>
public static Assortment GetAssortmentByName(string assortmentName, Guid languageId, SecurityToken token)
{
Assortment assortment = null;
//check whether an assortment with same name exists.
//alternatively, you may use search to search for an assortment.
foreach (var item in ModuleProductCatalog.Instance.Assortments.GetAllAssortments(token))
{
if (item.GetDisplayName(languageId) == assortmentName)
assortment = item;
}
return assortment;
}
}
}