Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Changing route for variation and category

Vote:
 

Hello

I am trying to change the routes of variations and categories.

I would like categories to have the following route:

sitehost.com/{language}/{category1-routesegment}/{category2-routesegment} etc.

I would like variations to have the following route:

sitehost.com/{language}/{variation-routesegment}/p/{variation-code}

I have done the following:

    public class TestPartialRouter : HierarchicalCatalogPartialRouter
    {
        private readonly IContentLoader _contentLoader;
        private readonly ReferenceConverter _referenceConverter;

        public TestPartialRouter(Func<ContentReference> routeStartingPoint, CatalogContentBase commerceRoot, bool enableOutgoingSeoUri, IContentLoader contentLoader, ReferenceConverter referenceConverter) : base(routeStartingPoint, commerceRoot, enableOutgoingSeoUri)
        {
            _contentLoader = contentLoader;
            _referenceConverter = referenceConverter;
        }

        protected override bool TryGetVirtualPath(HttpContextBase context, CatalogContentBase content, string language, out string virtualPath)
        {
            var variationExists = _contentLoader.TryGet(content.ContentLink, out Variation variation);
            if (variationExists)
            {
                virtualPath = $"{variation.RouteSegment}/dp/{variation.ProductCode}";
                return true;
            }
          
            return base.TryGetVirtualPath(context, content, language, out virtualPath);
        }

        protected override CatalogContentBase FindNextContentInSegmentPair(CatalogContentBase catalogContent, SegmentPair segmentPair,
            SegmentContext segmentContext, CultureInfo cultureInfo)
        {
            return base.FindNextContentInSegmentPair(catalogContent, segmentPair, segmentContext, cultureInfo);
        }
    }

Currently the routes are correct in the Commerce, but I get a 404 when I access the variation URL. The category URLs are working fine! I've tried a variety of stuff in FindNextContentInSegmentPair, but I haven't been able to hit the variation page as intended.

The route is registered as follows:

var referenceConverter = context.Locate.Advanced.GetInstance<ReferenceConverter>();
var contentLoader = context.Locate.Advanced.GetInstance<IContentLoader>();
var commerceRootContent = contentLoader.GetChildren<CatalogContent>(referenceConverter.GetRootLink()).FirstOrDefault();
Func<ContentReference> startingPoint = () => ContentReference.IsNullOrEmpty(SiteDefinition.Current.StartPage) ?
                SiteDefinition.Current.RootPage :
                SiteDefinition.Current.StartPage;
RouteTable.Routes.RegisterPartialRouter(new TestPartialRouter(startingPoint, commerceRootContent, false, contentLoader, referenceConverter));

Any help is appreciated, thank you!

Regards,

Benjamin

#206558
Edited, Aug 24, 2019 0:04
Vote:
 

That's what your FindNextContentInSegmentPair has to do - it has to return the VariationContent when it hits 

sitehost.com/{language}/{variation-routesegment}/p/{variation-code}

However it'll be tricky because you would need identify the routesegment here, and that'd be a very expensive task unless you hit database directly (which is generally advised against), or if you work it backwardly...  

#206584
Aug 26, 2019 7:45
Vote:
 

I've tried hardcoding a return of a random variation in my catalogue, but I still get 404:

Maybe I'm not sure what FindNextContentSegmentPair is suppose to do...

#206588
Aug 26, 2019 9:21
Vote:
 

Does your variation content type have a view associated with it ? 

#206591
Aug 26, 2019 10:36
Vote:
 

It does yes:

    public class VariationController : ContentController<Variation>
    {
        public ActionResult Index(Variation currentContent)
        {
            var viewModel = new ContentViewModel<Variation>(currentContent);
            return View(viewModel);
        }
    }

Index.cshtml:

@model GenericStore.Models.ViewModels.ContentViewModel<GenericStore.Models.Commerce.Variation>

<h3>@Model.CurrentContent.DisplayName</h3>
#206603
Aug 26, 2019 13:51
Vote:
 

Hi Quan Mai,

It has been a while, but I was hoping you could give your insight on my issue here, since I have not been able to resolve it.

Any idea or suggestion on how to approach here?

Br,

Benjamin

#288566
Oct 03, 2022 15:41
Vote:
 

It's been 3 years so I have no idea what I was talking about. I will try to dig it up but it'll take time 

#288623
Oct 04, 2022 10:10
* 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.