Try our conversational search powered by Generative AI!

Steven Galton
Apr 3, 2018
  1681
(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
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog