Developer forum

Forum » Ecommerce - Standard features » Facet options for variant - show 'without variant' option

Facet options for variant - show 'without variant' option

Steve Knutson
Reply

Hi everyone,

We're back again with our second question on the forum. This time it's also related to Facets.

  • We have a Variant Group set-up which contains 4 Variants.
  • We have created a Field Facet for this Variant Group
  • We have this Facet showing on a page to filter a Product List
  • The 4 Facet Options show as Check Boxes that you can select and filter the Product List

The problem:

We need to add another option to the Facet Options which would show as: "[Check Box] Without Variants".
When a user clicks this checkbox it would show all Products that don't have any options in our Variant Group selected.

What is the 'best practice' for doing this? Perhaps there are some resources that you could point us to?

Thank we really appreciate it!


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Steve

Facets is a list of possible values of a given field in the index for each document (product) that has been found. If a field does not have a value for given product (document), it will not be indexed and cannot show up as a facet. So that leaves you with 2 options I can think of to get products with no variants.

  1. You can add a parameter and an expression on the query where you search for products where IsVariant = False. This is not a facet - that would give you a seperate search parameter that you would have to put into the template or so.
  2. You can make an IndexBuilderExtender that will change the value of the variant group field when a product does not have variants (or create a new field for this facet). In this way you can index a value "No variants" in the variant field if none is present. That will give you an extra facet option for products with no variant - something like this (untested)

class IndexBuilderExtenderExample : IndexBuilderExtenderBase<ProductIndexBuilder>
    {
        public override void ExtendDocument(IndexDocument document)
        {
            if (document.ContainsKey("ID"))
            {
                string productid = (string)document["ID"];
            }

            if (document.ContainsKey("VariantGroupSystemName"))
            {
                string variantName = (string)document["VariantGroupSystemName"];

                document.Add("VariantGroupSystemName_ForFacet", variantName);
            }else{
               
document.Add("VariantGroupSystemName_ForFacet", "No variant");
                           }

            //document.Add("CustomField", DateTime.Now);
        }
    }

Votes for this answer: 1
 
Steve Knutson
Reply

Thanks for that Nicolai.  smiley

I think we'll give the IndexBuilderExtender approach a go.

Cheers!

 
Steve Knutson
Reply
This post has been marked as an answer

Hi Nicolai, 

Thanks again for your response that was the advice that we needed. We used the info from this page: https://doc.dynamicweb.com/training-certification/t3-platform-developer/t3-platform-developer/3-5-extending-indexing#sideNavTitle1-1-1-3, combined with your example to extend the index and show a custom facet option. Very cool.

Cheers yes

Votes for this answer: 1
 
Nicolai Pedersen
Reply

Perfect! Well done without any prior training. 

 

You must be logged in to post in the forum