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.
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?
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());
}