Group variants on category page

How do you group variants into “one product” (or similar) on the category page but keeping the UseVariantUrl?

My customer wants to have individual variants but on the category page show only the first variant (baseproduct) name and “has more articlenumbers”-label.

But still support when there is only one variant to show that article number.

Litium version: 6.2

Look at Litium.Accelerator.Search.VariantIndexDocumentMerger in the Accelerator, there you can modify the grouping of variants in listings.

In standard you use the view below to define per template which variant field that should be used to group by:

The solution is an old upgraded Litium 4.8. Not to sure that Litium.Accelerator.Search.VariantIndexDocumentMerger is implemented correctly.

Is there any documentation how to implement it from scratch?

No, easiest is probably to look at the implementation on a more recent accelerator for reference.

I’m interested in the VariantIndexDocumentMerger, it might be of help to us but since there is no documentation it’s hard to tell. Would you be able to create some documentation; when to use it, how to implement it, etc.?

Hi,
I was in need to group variants by base product. I checked this settings. It has just fields and didn’t find an option to group variants by its base product

@marten could you please expand on this and clarify how the VariantIndexDocumentMerger is supposed to be used?

Is there a supported best practice of having specific variant URLs but only displaying one / the base product in search results and in category listings?

Hi,

Did you find any solution to group by base product on the category page while still keeping the url variant version?

Litium version 7.4.2

Just tested this and modified the Merge-method of VariantIndexDocumentMerger to group all variant documents into one for the baseproduct (this will need some more testing):

public IEnumerable<IndexDocument> Merge(IEnumerable<IndexDocument> indexDocuments)
{
    var documents = indexDocuments as IList<IndexDocument> ?? indexDocuments.ToList();
    var fieldTemplateId = documents.FirstOrDefault()?.Tags.Where(x => x.Name == TagNames.TemplateId)
        .Select(x => (string) x.OriginalValue).FirstOrDefault();
    if (string.IsNullOrEmpty(fieldTemplateId)) return documents;

    // TODO - add conditions for which tags to group

    const string group = "GroupByBaseProduct";
    return documents
        .GroupBy(doc => group)
        .Select(x => new IndexDocument
        {
            Id = x.First().Id,
            Tags = new Collection<DocumentTag>(new[] {new ReadableDocumentTag(MergeTagName, group)}
                .Concat(x.SelectMany(z => z.Tags)).ToList())
        });
}

And if you want to build product in result, adjust ProductSearchService.CreateProductModels:

 if (hit.Tags.Any(x => x.Name == VariantIndexDocumentMerger.MergeTagName))
{
    // model = CreateProductModel(searchQuery, hit.Tags.Where(x => x.Name == TagNames.ArticleNumber).Select(x => _variantService.Get(x.Value)));
    model = _productModelBuilder.BuildFromBaseProduct(_variantService.Get(systemId).GetBaseProduct());
}