Monday, June 13, 2011

Mutiple brands in EPiServer Commerce

In EPiServer Commerce you map your products to a product template and your product listings to a product listing template this are then used to connect Commerce with EPiServer in the "CatalogPageProvider", all this is fine until you for example add a new catalog to Commerce that needs a different template then the default catalog.
This is what happened to us in our project when we added a new site with different design and product catalog into the EPiServer Commerce installation. To solve this problem we extended EPiServers CatalogPageProvider and did our own implementation of the function "GetPageType" that delivered different templates based on the catalog id of the page.

Here is our implementation of the GetPageType function,


public class AvalancheCatalogPageProvider : CatalogPageProvider
{
protected override string GetPageType(string key)
{
var store = new CommerceSiteMappingStore();
var dictionary = SplitKey(key);

int catalogId;
CommerceSiteMapping mapping;
if (int.TryParse(dictionary["CatalogId"], out catalogId) && (mapping = store.Find(catalogId)) != null)
{
var nodeType = (NodeType)Enum.Parse(typeof(NodeType), dictionary["NodeType"]);
switch (nodeType)
{
case NodeType.CatalogEntry:
return mapping.CatalogEntryPageType.Name;
case NodeType.CatalogNode:
return mapping.CatalogNodePageType.Name;
}
return mapping.FallbackPageType.Name;
}
return base.GetPageType(key);
}

private static IDictionary SplitKey(string key)
{
Dictionary dictionary = new Dictionary();
string[] strArray = MappedPPDB.SplitKey(key);
dictionary.Add("NodeId", strArray[0]);
dictionary.Add("CatalogName", strArray[1]);
dictionary.Add("ParentNodeId", strArray[2]);
dictionary.Add("CatalogId", strArray[3]);
dictionary.Add("NodeType", strArray[4]);
dictionary.Add("ID", strArray[5]);
dictionary.Add("ParentNodeCode", strArray[6]);
return dictionary;
}
}

The differences between this implementation and EPiServers is that we return different page types based on the catalog id of the page that is loading.
The mappings are saved in the dynamic data store and editable via a gui plugin in EPiServer, if we can't find a mapping that is configured in our tool we call EPiServers default implementation to let it handle the request.

As I said this is the solution for our project and the needs we had were each catalog is represented by only one page type.