Try our conversational search powered by Generative AI!

Steven Galton
Apr 3, 2018
  1721
(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
Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024

Optimizely Search and Navigation - Part 2 - Filter Tips

Introduction Continuing from Part 1 – Search Tips , today I will share the next part – filter tips. The platform versions used for this article are...

Binh Nguyen Thi | Jul 1, 2024

Integrating HubSpot CRM without the MA Connector

Have HubSpot CRM? Want to push user data into it from Optimizely? Don’t have any personalisation requirements with that data? Don’t want to pay $80...

Matt Pallatt | Jun 27, 2024

Keeping the website secure by updating external packages

Did you see the latest warning from Optimizely to update this package with a critical security warning? https://world.optimizely.com/documentation/...

Daniel Ovaska | Jun 27, 2024