Setting a NodeContent as the catalog root using the HierarchicalCatalogPartialRouter

Vote:
 

Hi,

I am trying to set the Node content as the catalog root. Below is my catalog structure:

https://www.screencast.com/t/5tMhOWSBJDk

I want to make the Test Node the root for all the children Node Contents under it. And below is my code 

using System;
using System.Configuration;
using System.Linq;
using System.Web.Routing;
using EPiServer;
using EPiServer.Commerce.Routing;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Web;
using EPiServer.Web.Routing;
using Mediachase.Commerce.Catalog;

namespace Foundation.Infrastructure
{
    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
    public class Initialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var referenceConverter = context.Locate.Advanced.GetInstance<ReferenceConverter>();
            var contentRepository = context.Locate.Advanced.GetInstance<IContentRepository>();
            var rootLink = referenceConverter.GetRootLink();
            var root = contentRepository.GetChildren<EPiServer.Commerce.Catalog.ContentTypes.CatalogContent>(rootLink).FirstOrDefault();
            var rootNodeContent = contentRepository.GetChildren<EPiServer.Commerce.Catalog.ContentTypes.NodeContent>(root.ContentLink).FirstOrDefault();

            var hierarchicalCatalogPartialRouter = new HierarchicalCatalogPartialRouter(() => SiteDefinition.Current.StartPage, rootNodeContent, false);

            RouteTable.Routes.RegisterPartialRouter(hierarchicalCatalogPartialRouter);
        }

        public void Uninitialize(InitializationEngine context)
        {
            //Add uninitialization logic
        }
    }
}

But I still see the routing has not changed, it still is for exmaple: www.example.com/shopping/testnode/cookware/

Since I made the the first NodeContent the root, I was expecting it to be like: www.example.com/testnode/cookware/

Am I missing something here?

#222253
May 01, 2020 3:45
Vote:
 

Hi Siddharth,

I have created another entry for Node in the RouteTable which works for both cases catalog and node. Please try.

e.g.

www.example.com/shopping/testnode/cookware/

www.example.com/testnode/cookware/

[InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    [ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
    public class CatalogRoutingInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            // Services
            ReferenceConverter referenceConverter = context.Locate.Advanced.GetInstance<ReferenceConverter>();
            IContentLoader contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();

            // Register catalog router (removes catalog string from URL)
            ContentReference rootLink = referenceConverter.GetRootLink();
            CatalogContent catalog = contentLoader.GetChildren<CatalogContent>(rootLink).FirstOrDefault();

            Func<ContentReference> startLink = () =>
                ContentReference.IsNullOrEmpty(SiteDefinition.Current.StartPage)
                    ? SiteDefinition.Current.RootPage
                    : SiteDefinition.Current.StartPage;

            // catalog root content
            RouteTable.Routes.RegisterPartialRouter(new HierarchicalCatalogPartialRouter(startLink, catalog, false));


            // node root content 
            var rootNodeContent = contentLoader.GetChildren<NodeContent>(catalog.ContentLink).FirstOrDefault();
            var nodeCatalogContent = contentLoader.Get<CatalogContentBase>(referenceConverter.GetContentLink(rootNodeContent.Code));
            RouteTable.Routes.RegisterPartialRouter(new HierarchicalCatalogPartialRouter(() => rootNodeContent.ContentLink, nodeCatalogContent, false));
        }

        public void Uninitialize(InitializationEngine context)
        {
        }
    }

For more customization please follow Jakejon's post. 

#222275
May 01, 2020 19:18
Siddharth Gupta - May 03, 2020 6:27
Hi Sanjay, I tried this out but it did not work as well. Have you tried this on Foundation? Looks like this is not working on Foundation.
Sanjay Kumar - May 03, 2020 16:54
No, I tried in QuickSilver solution.
Vote:
 

It looks like you are using Foundation? Then you might want to remove this line

https://github.com/episerver/Foundation/blob/0604b691053359f7640ff4869494d2fe322b081e/src/Foundation.Commerce/Extensions/InitializationEngineExtensions.cs#L38

this registers the default router which might interfere with your set up

#222327
May 03, 2020 20:34
Siddharth Gupta - May 03, 2020 23:36
Thanks Quan! :)
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.