Product options and variants
List of examples:
Product option categories
Creating an option category
// Creates a new option category object
OptionCategoryInfo newCategory = new OptionCategoryInfo();
// Sets the option category properties
newCategory.CategoryDisplayName = "New category";
newCategory.CategoryName = "NewCategory";
newCategory.CategoryType = OptionCategoryTypeEnum.Products;
newCategory.CategorySelectionType = OptionCategorySelectionTypeEnum.Dropdownlist;
newCategory.CategoryDisplayPrice = true;
newCategory.CategoryEnabled = true;
newCategory.CategoryDefaultRecord = "";
newCategory.CategorySiteID = SiteContext.CurrentSiteID;
// Saves the option category to the database
OptionCategoryInfo.Provider.Set(newCategory);
Updating an option category
// Gets the option category
OptionCategoryInfo updateCategory = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if (updateCategory != null)
{
// Updates the option category properties
updateCategory.CategoryDisplayName = updateCategory.CategoryDisplayName.ToLowerCSafe();
// Saves the changes to the database
OptionCategoryInfo.Provider.Set(updateCategory);
}
Updating multiple option categories
// Gets all option categories whose name starts with 'New'
var categories = OptionCategoryInfo.Provider.Get().WhereStartsWith("CategoryName", "New");
// Loops through the option categories
foreach (OptionCategoryInfo modifyCategory in categories)
{
// Updates the option category properties
modifyCategory.CategoryDisplayName = modifyCategory.CategoryDisplayName.ToUpperCSafe();
// Saves the changes to the database
OptionCategoryInfo.Provider.Set(modifyCategory);
}
Adding option categories to a product
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
// Gets the option category
OptionCategoryInfo category = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if ((product != null) && (category != null))
{
// Adds the category to the product
SKUOptionCategoryInfo.Provider.Add(category.CategoryID, product.SKUID);
}
Removing option categories from a product
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.TopN(1)
.FirstOrDefault();
// Gets the option category
OptionCategoryInfo category = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if ((product != null) && (category != null))
{
// Removes the option category from the product
ProductHelper.RemoveOptionCategory(product.SKUID, category.CategoryID);
}
Deleting an option category
// Gets the option category
OptionCategoryInfo deleteCategory = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if (deleteCategory != null)
{
// Deletes the option category
OptionCategoryInfo.Provider.Delete(deleteCategory);
}
Product options
Creating a product option
// Gets a department
DepartmentInfo department = DepartmentInfo.Provider.Get("NewDepartment", SiteContext.CurrentSiteID);
// Gets an option category
OptionCategoryInfo category = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if ((department != null) && (category != null))
{
// Creates a new product option object
SKUInfo newOption = new SKUInfo();
// Sets the product option properties
newOption.SKUName = "NewProductOption";
newOption.SKUPrice = 199;
newOption.SKUEnabled = true;
newOption.SKUDepartmentID = department.DepartmentID;
newOption.SKUOptionCategoryID = category.CategoryID;
newOption.SKUSiteID = SiteContext.CurrentSiteID;
newOption.SKUProductType = SKUProductTypeEnum.Product;
// Saves the product option to the database
SKUInfo.Provider.Set(newOption);
}
Updating a product option
// Gets the product option
SKUInfo option = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProductOption")
.WhereNotNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (option != null)
{
// Updates the product option properties
option.SKUName = option.SKUName.ToLowerCSafe();
// Saves the changes to the database
SKUInfo.Provider.Set(option);
}
Updating multiple product options
// Gets a product option category
OptionCategoryInfo optionCategory = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
// Gets all product options with the option category
var options = SKUInfo.Provider.Get().WhereEquals("SKUOptionCategoryID", optionCategory.CategoryID);
// Loops through the product options
foreach (SKUInfo modifyOption in options)
{
// Updates the product option properties
modifyOption.SKUName = modifyOption.SKUName.ToUpperCSafe();
// Saves the changes to the database
SKUInfo.Provider.Set(modifyOption);
}
Allowing product options for a product
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
// Prepares a list for holding product option IDs
List<int> optionIds = new List<int>();
// Gets all product options whose name starts with 'New'
var options = SKUInfo.Provider.Get()
.WhereStartsWith("SKUName", "New")
.WhereNotNull("SKUOptionCategoryID");
// Adds the IDs of the options to the list
foreach (SKUInfo option in options)
{
optionIds.Add(option.SKUID);
}
// Gets the option category
OptionCategoryInfo category = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if ((product != null) && (optionIds.Count > 0) && (category != null))
{
// Allows the options for the product
ProductHelper.AllowOptions(product.SKUID, category.CategoryID, optionIds);
}
Removing product options from a product
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
// Prepares a list for holding product option IDs
List<int> optionIds = new List<int>();
// Gets all product options whose name starts with 'New'
var options = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "New")
.WhereNotNull("SKUOptionCategoryID");
// Adds the IDs of the options to the list
foreach (SKUInfo option in options)
{
optionIds.Add(option.SKUID);
}
// Gets the option category
OptionCategoryInfo category = OptionCategoryInfo.Provider.Get("NewCategory", SiteContext.CurrentSiteID);
if ((product != null) && (category != null) && (optionIds.Count > 0))
{
// Removes the options in the list from the product
ProductHelper.RemoveOptions(product.SKUID, category.CategoryID, optionIds);
}
Deleting a product option
// Gets the product option
SKUInfo option = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProductOption")
.WhereNotNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (option != null)
{
// Deletes the product option
SKUInfo.Provider.Delete(option);
}
Product variants
Creating a product variant
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (product != null)
{
// Prepares a list of option category IDs
List<int> categoryIDs = new List<int>();
// Creates two attribute option categories with product options
for (int i = 1; i <= 2; i++)
{
// Creates a new option category object and sets its properties
OptionCategoryInfo newCategory = new OptionCategoryInfo
{
CategoryDisplayName = "New attribute category " + i,
CategoryName = "NewAttributeCategory" + i,
CategoryType = OptionCategoryTypeEnum.Attribute,
CategorySelectionType = OptionCategorySelectionTypeEnum.Dropdownlist,
CategoryDisplayPrice = true,
CategoryEnabled = true,
CategoryDefaultRecord = "",
CategorySiteID = SiteContext.CurrentSiteID
};
// Saves the category to the database
OptionCategoryInfo.Provider.Set(newCategory);
// Assigns the option category to the product
SKUOptionCategoryInfo.Provider.Add(newCategory.CategoryID, product.SKUID);
categoryIDs.Add(newCategory.CategoryID);
// Creates two product options for the category
foreach (String color in new[] { "Black", "White" })
{
// Creates a product option object and sets its properties
SKUInfo newOption = new SKUInfo
{
SKUName = "NewColorOption" + color,
SKUPrice = 0,
SKUEnabled = true,
SKUOptionCategoryID = newCategory.CategoryID,
SKUSiteID = SiteContext.CurrentSiteID,
SKUProductType = SKUProductTypeEnum.Product
};
// Saves the product option
SKUInfo.Provider.Set(newOption);
// Assigns the product option to the product
SKUAllowedOptionInfo.Provider.Add(product.SKUID, newOption.SKUID);
}
}
// Generates the product variants
List<ProductVariant> variants = VariantHelper.GetAllPossibleVariants(product.SKUID, categoryIDs);
// Saves the variants to the database
foreach (ProductVariant variant in variants)
{
VariantHelper.SetProductVariant(variant);
}
}
Updating product variants
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (product != null)
{
// Gets the product's variants
var variants = VariantHelper.GetVariants(product.SKUID);
// Loops through the product variants
foreach (SKUInfo updateVariant in variants)
{
// Updates the variant properties and saves them to the database
updateVariant.SKUName = updateVariant.SKUName.ToLowerCSafe();
SKUInfo.Provider.Set(updateVariant);
}
}
Deleting product variants
// Gets the product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (product != null)
{
// Deletes all variants
VariantHelper.DeleteAllVariants(product.SKUID);
// Gets the product options
var options = SKUInfo.Provider.Get().WhereStartsWith("SKUName", "NewColorOption");
// Loops through the product options
foreach (SKUInfo option in options)
{
// Deletes the product option
SKUInfo.Provider.Delete(option);
}
// Gets the option categories
var categories = OptionCategoryInfo.Provider.Get().WhereStartsWith("CategoryName", "NewAttributeCategory");
// Loops through the option categories
foreach (OptionCategoryInfo category in categories)
{
// Deletes the categories
OptionCategoryInfo.Provider.Delete(category);
}
}