Products
List of examples:
Products (SKUs)
Creating a product
// Gets a department
DepartmentInfo department = DepartmentInfo.Provider.Get("NewDepartment", SiteContext.CurrentSiteID);
// Creates a new product object
SKUInfo newProduct = new SKUInfo();
// Sets the product properties
newProduct.SKUName = "NewProduct";
newProduct.SKUPrice = 120;
newProduct.SKUEnabled = true;
if (department != null)
{
newProduct.SKUDepartmentID = department.DepartmentID;
}
newProduct.SKUSiteID = SiteContext.CurrentSiteID;
// Saves the product to the database
// Note: Only creates the SKU object. You also need to create a connected product page to add the product to the site.
SKUInfo.Provider.Set(newProduct);
Updating a product
// Gets the product
SKUInfo updateProduct = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (updateProduct != null)
{
// Updates the product properties
updateProduct.SKUName = updateProduct.SKUName.ToLowerCSafe();
// Saves the changes to the database
SKUInfo.Provider.Set(updateProduct);
}
Updating multiple products
// Gets all products whose name starts with 'New'
var products = SKUInfo.Provider.Get()
.WhereStartsWith("SKUName", "New")
.WhereNull("SKUOptionCategoryID");
// Loops through the products
foreach (SKUInfo modifyProduct in products)
{
// Updates the product properties
modifyProduct.SKUName = modifyProduct.SKUName.ToUpperCSafe();
// Saves the changes to the database
SKUInfo.Provider.Set(modifyProduct);
}
Deleting a product
// Gets the product
SKUInfo deleteProduct = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (deleteProduct != null)
{
// Deletes the product
SKUInfo.Provider.Delete(deleteProduct);
}
Product pages
Creating a product page
// Gets the product (SKU)
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.TopN(1)
.FirstOrDefault();
// Gets the root page of the products section in the content tree
TreeNode parent = new DocumentQuery<TreeNode>()
.Path("/Products", PathTypeEnum.Single)
.OnSite("MySite")
.TopN(1)
.Published()
.PublishedVersion()
.FirstOrDefault();
if ((parent != null) && (product != null))
{
// Creates a new product page of the 'Custom.Product' type
SKUTreeNode node = (SKUTreeNode)TreeNode.New("Custom.Product");
// Sets the product page properties
node.DocumentCulture = LocalizationContext.PreferredCultureCode;
string name = "Product page";
node.DocumentName = name;
// Synchronize SKU name with document name in a default culture
node.DocumentSKUName = name;
// Sets a value for a field of the given product page type
node.SetValue("ProductColor", "Blue");
// Assigns the product to the page
node.NodeSKUID = product.SKUID;
// Saves the product page to the database
node.Insert(parent);
}
Updating a product page
// Gets the product page
SKUTreeNode page = new DocumentQuery<SKUTreeNode>()
.Path("/Products/NewProduct", PathTypeEnum.Single)
.OnSite("MySite")
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
if (page != null)
{
// Updates the product page properties
page.DocumentSKUDescription = "Product was updated.";
page.DocumentName = page.DocumentName.ToLowerCSafe();
// Saves the product page to the database
page.Update();
}
Deleting a product page
// Gets the product page
SKUTreeNode page = new DocumentQuery<SKUTreeNode>()
.Path("/Products/NewProduct")
.OnSite("MySite")
.Culture("en-us")
.TopN(1)
.FirstOrDefault();
if (page != null)
{
// Deletes the product page
page.Delete(deleteAllCultures: true, destroyHistory: true);
}
Membership products
Creating a membership product
// Gets a department
DepartmentInfo department = DepartmentInfo.Provider.Get("NewDepartment", SiteContext.CurrentSiteID);
// Gets a membership
MembershipInfo membership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);
// Creates a membership if "NewMembership" does not exist
if (membership == null)
{
// Creates a membership object and sets its properties
membership = new MembershipInfo
{
MembershipDisplayName = "New Membership",
MembershipName = "NewMembership",
MembershipSiteID = SiteContext.CurrentSiteID
};
// Saves the membership
MembershipInfo.Provider.Set(membership);
}
// Creates a new product object
SKUInfo newProduct = new SKUInfo();
// Sets the product properties (e.g. marks the product as a membership)
if (department != null)
{
newProduct.SKUDepartmentID = department.DepartmentID;
}
newProduct.SKUName = "NewMembershipProduct";
newProduct.SKUPrice = 69;
newProduct.SKUEnabled = true;
newProduct.SKUSiteID = SiteContext.CurrentSiteID;
newProduct.SKUProductType = SKUProductTypeEnum.Membership;
newProduct.SKUMembershipGUID = membership.MembershipGUID;
newProduct.SKUValidity = ValidityEnum.Months;
newProduct.SKUValidFor = 3;
// Creates the product
SKUInfo.Provider.Set(newProduct);
Deleting membership products
// Gets all membership products whose name starts with 'NewMembership'
var products = SKUInfo.Provider.Get()
.WhereStartsWith("SKUName", "NewMembership")
.WhereEquals("SKUProductType", SKUProductTypeEnum.Membership);
// Loops through the membership products
foreach (SKUInfo deleteProduct in products)
{
// Deletes the membership product
SKUInfo.Provider.Delete(deleteProduct);
}
// Gets the membership
MembershipInfo membership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);
if (membership != null)
{
// Deletes the membership
MembershipInfo.Provider.Delete(membership);
}
E-products
Creating an e-product
// Gets a department
DepartmentInfo department = DepartmentInfo.Provider.Get("NewDepartment", SiteContext.CurrentSiteID);
// Creates a new product object
SKUInfo newProduct = new SKUInfo();
// Sets the product properties (e.g. marks the product as an e-product)
if (department != null)
{
newProduct.SKUDepartmentID = department.DepartmentID;
}
newProduct.SKUName = "NewEProduct";
newProduct.SKUPrice = 169;
newProduct.SKUEnabled = true;
newProduct.SKUSiteID = SiteContext.CurrentSiteID;
newProduct.SKUProductType = SKUProductTypeEnum.EProduct;
newProduct.SKUValidity = ValidityEnum.Until;
newProduct.SKUValidUntil = DateTime.Today.AddDays(10d);
// Saves the product to the database
SKUInfo.Provider.Set(newProduct);
// Prepares a path of a file to be uploaded as the e-product file
string eProductFile = System.Web.HttpContext.Current.Server.MapPath("files/file.png");
// Creates a metafile object storing the e-product file
MetaFileInfo metafile = new MetaFileInfo(eProductFile, newProduct.SKUID, "ecommerce.sku", "E-product");
// Saves the metafile to the database
MetaFileInfo.Provider.Set(metafile);
// Creates an object representing the e-product's assigned downloadable file (SKUFile)
SKUFileInfo skuFile = new SKUFileInfo();
// Sets the SKUFile properties
skuFile.FileSKUID = newProduct.SKUID;
skuFile.FilePath = "~/getmetafile/" + metafile.MetaFileGUID.ToString().ToLowerCSafe() + "/" + metafile.MetaFileName + ".aspx";
skuFile.FileType = "MetaFile";
skuFile.FileName = metafile.MetaFileName;
skuFile.FileMetaFileGUID = metafile.MetaFileGUID;
// Saves the SKUFile to the database
SKUFileInfo.Provider.Set(skuFile);
Deleting e-products
// Gets all e-products whose name starts with 'New'
var products = SKUInfo.Provider.Get()
.WhereStartsWith("SKUName", "New")
.WhereEquals("SKUProductType", SKUProductTypeEnum.EProduct);
// Loops through the e-products
foreach (SKUInfo deleteEProduct in products)
{
// Gets the objects representing the files assigned to the e-product (SKUFiles)
var deleteFiles = SKUFileInfo.Provider.Get().WhereEquals("FileSKUID", deleteEProduct.SKUID);
// Loops through the SKUFiles
foreach (SKUFileInfo deleteSKUFile in deleteFiles)
{
// Deletes the SKUFile
SKUFileInfo.Provider.Delete(deleteSKUFile);
}
// Deletes the metafiles assigned to the e-product
MetaFileInfoProvider.DeleteFiles(deleteEProduct.SKUID, "ecommerce.sku");
// Deletes the e-product
SKUInfo.Provider.Delete(deleteEProduct);
}
Bundle products
Creating a bundle
// Gets a set of products to be included in the bundle
var products = SKUInfo.Provider.Get().WhereEquals("SKUName", "New");
if (products.Count > 0)
{
// Gets a department
DepartmentInfo department = DepartmentInfo.Provider.Get("NewDepartment", SiteContext.CurrentSiteID);
// Creates a new product object
SKUInfo newBundle = new SKUInfo();
// Sets the product object properties (e.g. marks the product as a bundle)
newBundle.SKUName = "NewBundleProduct";
newBundle.SKUPrice = 50;
newBundle.SKUEnabled = true;
newBundle.SKUSiteID = SiteContext.CurrentSiteID;
newBundle.SKUProductType = SKUProductTypeEnum.Bundle;
newBundle.SKUBundleInventoryType = BundleInventoryTypeEnum.RemoveBundle;
if (department != null)
{
newBundle.SKUDepartmentID = department.DepartmentID;
}
// Saves the bundle to the database
SKUInfo.Provider.Set(newBundle);
// Loops through the products and adds them to the bundle
foreach (SKUInfo product in products)
{
BundleInfo.Provider.Add(newBundle.SKUID, product.SKUID);
}
}
Deleting a bundle
// Gets the bundle product
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewBundleProduct")
.WhereEquals("SKUProductType", SKUProductTypeEnum.Bundle)
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (product != null)
{
// Gets the relationships between the bundle and its products
var bundleProductBindings = BundleInfo.Provider.Get().WhereEquals("BundleID", product.SKUID);
// Loops through the bundle-product relationships
foreach (BundleInfo bundleProduct in bundleProductBindings)
{
// Removes the product from the bundle
BundleInfo.Provider.Delete(bundleProduct);
}
// Deletes the bundle product
SKUInfo.Provider.Delete(product);
}