SaaS CMS has officially launched! Learn more now.

Steven Galton
Apr 3, 2018
  1728
(2 votes)

Indexing extra Episerver Find properties onto a single variant for Episerver Commerce using Extension Methods

I have been working on a project that uses Episerver Find heavily on Episerver Commerce Items. Previously I have posted about indexing extra properties on a single variant and got some responses back about using extension methods to index the information. Previously I had some issues with approaching the problem this way, but I have been able to do this now.

First, I needed to make an Initialization Module with a reference for the Search Client. Next, I created a variable that has the Search Client Conventions. From this, I was able to make it that every Supplier Product in my Commerce catalogue had an extra field that is only needed in the search result. This was the variant URL that we need for the search pages.

    [InitializableModule]    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EpiserverFindInitilizationModule : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var conventionIndexer = SearchClient.Instance.Conventions;
            conventionIndexer.ForInstancesOf<SupplierProduct>().IncludeField(x => x.VariantUrl());
         }
     }

After this, I had an Extension Method that would use our resolver to pass in the current Supplier Product and then return the Variant URL for the Search Results.

public static string VariantUrl(this SupplierProduct supplierProduct)
{
    var contentLoader = ServiceLocator.Current.GetInstance<SupplierProductSummaryResolver>();
    var variant = contentLoader.GetViewModel(supplierProduct);
    return variant.Url;
}

Before, I could not figure out how to retrieve this value from the Index and return it in my search results. I got some help and all I needed to do was call the extension method again on the search item.

private List<ShopItemInformationModel> PopulateShopItems(IEnumerable<SupplierProduct> shopResults)
{
   var shopResponseList = new List<ShopItemInformationModel>();

    foreach (var supplierProduct in shopResults)
    {
        var item =  new ShopItemInformationModel
        {
            Title = supplierProduct.Name,
            Code = supplierProduct.Code,
            Url = supplierProduct.VariantUrl()
        };

        shopResponseList.Add(item);
    }

   return shopResponseList;
}

One thing to note with this method though. This does call the extension method again and recalls the logic. We will be exploring this over the coming weeks and will get an update up over the next couple of weeks.

Hope this helps someone!

Apr 03, 2018

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely Developer - London Meetup 2024

Hello and welcome to another instalment of A Day In The Life Of An Optimizely Developer. Last night (11th July 2024) I was excited to have attended...

Graham Carr | Jul 16, 2024

Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024