Developer forum

Forum » Ecommerce - Standard features » Before rendering facets notification

Before rendering facets notification

Thomas Larsen
Reply

Is there a notification before facets, from the repository index,  are rendered on the product list.

Where I can change the sorting, remove facets etc.?


Replies

 
Nicolai Høeg Pedersen
Reply

The easiest is to do it in the template in Razor.

You have this notification that can also be used: Notifications.eCommerce.Querying.AfterQuery

Its arguments contains a property with the result (Dynamicweb.Querying.IQueryResult)

NP

 
Thomas Larsen
Reply

I would like to use the AfterQuery notification.

But the FacetGroupResult do not allow me to change the sorting on the facets or removing facets, because the facetResult is private. The only thing that I can do is adding facets with the AddFacetResult method?

 
Nicolai Høeg Pedersen
Reply

Hi Thomas

There should be nothing private...

Here is a simple sample of accessing the information:

using System.Collections.Generic;

//Add reference to Dynamicweb.Querying and Dynamicweb.Indexing.Lucene

namespace Dynamicweb.Examples.CSharp.eCommerce
{
    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Querying.AfterQuery)]
    public class FacetChangeObserver : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            Dynamicweb.Notifications.eCommerce.Querying.AfterQueryArgs myArgs = args as Dynamicweb.Notifications.eCommerce.Querying.AfterQueryArgs;

            Dynamicweb.Indexing.Lucene.LuceneQueryResult result = (Dynamicweb.Indexing.Lucene.LuceneQueryResult)myArgs.Result;

            Dictionary<string, Querying.Facetting.FacetGroupResult> facets = new Dictionary<string, Querying.Facetting.FacetGroupResult>();
            foreach (Querying.Facetting.IFacetGroup facetGroup in myArgs.Settings.Facets)//Loop the facets, i.e. Color, Variants etc.
            {
                foreach(var something in facetGroup.Items)//Loop the items in the facet group
                {
                    //Do magic here...
                }
            }
            

            myArgs.Result = result;
        }
    }
}

But it is not that simple.

In the attached code file (yes, it is VB), you can check out HandleFacetGroupResult method for how we handle the facets.

Also, it would be awesome if you post the result of your efforts so we can add it to the examples.

Thanks, Nicolai

 
Thomas Larsen
Reply
This post has been marked as an answer

Hi Nicolai,

I endend up using the BeforeQuery notification instead

 [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Querying.BeforeQuery)]
    public class BeforeQuery : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {

            Dynamicweb.Notifications.eCommerce.Querying.BeforeQueryArgs beforeQueryArgs = (Dynamicweb.Notifications.eCommerce.Querying.BeforeQueryArgs)args;

            //Only apply on filter facet group
            var filterFacetGroup = beforeQueryArgs.Settings.Facets.FirstOrDefault(fg => fg.Name == "FilterFacet.facets");

            if (filterFacetGroup == null || filterFacetGroup.Items.Count() == 0)
                return;

            var groupFacets = new Dictionary<string, int>();
            groupFacets.Add("Filter_100", 1); //Filter_100, sorting 1
            groupFacets.Add("Filter_90", 2); //Filter_90, sorting 2
            groupFacets.Add("Filter_120", 3); //Filter_120, sorting 3
            
            var groupVisiblefacetsSorted = (from facets in filterFacetGroup.Items
                                            join groupfacets in groupFacets
                                                 on facets.Name equals groupfacets.Key 
                                            orderby groupfacets.Value ascending
                                            select facets).ToList();

            filterFacetGroup.Items = groupVisiblefacetsSorted;

        }
    }

 

Votes for this answer: 1
 
Nicolai Høeg Pedersen
Reply

Hi Thomas

Nice! Thanks for sharing.

Have a nice weekend, Nicolai

 

You must be logged in to post in the forum