November Happy Hour will be moved to Thursday December 5th.

Steven Galton
Apr 3, 2018
  1812
(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
Optimizely SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog