Strongly Type Models in EPiServer Commerce

We can create Strongly typed models just by inheriting a proper type from EPiServer.Commerce.Catalog.ContentTypes and and decorating it with the ContentTypeAttribute attribute. that means those types will be available as ContentData.

To create models for commerce content following types are available.
VariationContent: A type for variation/SKU models.
ProductContent: A type for product models.
BundleContent: A type for bundle models.
PackageContent: A type for package models.
DynamicPackageContent: A type for dynamic package models.
NodeContent: A type for category/node models.

Examples:
using EPiServer.Commerce.Catalog.ContentTypes;
using EPiServer.Commerce.Catalog.DataAnnotations;

namespace EPiServer.Commerce.XYZ.Models.MetaDataClasses
{
    [CatalogContentType(GUID = "BE40A3E0-49BC-48DD-9C1D-819C2661C9BC", MetaClassName = "Fashion_Item_Class")]
    public class FashionItemContent : VariationContent
    {
        public virtual string Description { get; set; }
    }
}
namespace EPiServer.Commerce.XYZ.Models.MetaDataClasses
{
    [CatalogContentType(GUID = "35A29D99-3531-4E4B-A40E-EF262E9DB8B5", MetaClassName = "FashionStoreLandingNode")]
    [AvailableContentTypes(Include = new Type[] { typeof(FashionProductContent), typeof(FashionItemContent) })]
    public class FashionStoreLandingNodeContent : NodeContent
    {
        public virtual string Description { get; set; }
    }
}
namespace EPiServer.Commerce.XYZ.Models.MetaDataClasses
{
    /// <summary>
    /// FashionProductContent class, map to Fashion_Product_Class metadata class
    /// </summary>
    [CatalogContentType(GUID = "18EA436F-3B3B-464E-A526-564E9AC454C7", MetaClassName = "Fashion_Product_Class")]
    public class FashionProductContent : ProductContent
    {
        public virtual string Description { get; set; }
    }

}

Furthermore EPiServer Commerce provides a content provider that can serve any catalog content as IContent. That means an IContentRepository can be used to work with the content the same way we do in EPiServer CMS. 

public ContentReference CreateNewSku(ContentReference linkToParentNode)
{
    var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
    //Create a new instance of CatalogContentTypeSample that will be a child to the specified parentNode.
    var newSku = contentRepository.GetDefault<CatalogContentTypeSample>(linkToParentNode);
    //Set some required properties.
    newSku.Description = "Great";
    //Publish the new content and return its ContentReference.
    return contentRepository.Save(newSku, SaveAction.Publish, AccessLevel.NoAccess);
}

Comments

  1. How do you set up a new product using c# for Episerver Commerce 7.5 - im using you newSku but not sure how to do a new Product and to link the Inventory and pricing to each variant.
    Jon

    ReplyDelete

Post a Comment

Popular posts from this blog

EPiServer CMS 11 Useful SQL Queries - 1

EPiServer CMS 11 Useful SQL Queries - 2