Developer forum

Forum » Ecommerce - Standard features » Variant Group Options are not sorted

Variant Group Options are not sorted

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

When we're loading Variant Options, my expectation is that the options by default would honor the sort order of the UI/database. Looking into the private FillVariants() method I see no specific instruction for that. Can that be added?

Below is the suggested fix for it. We can also apply that fix in the Swift templates, but seems like having it in the Core product is the proper way to do it.

        private static void FillVariants(IList<VariantGroupViewModel> variants, VariantInfoViewModel variantInfo, Dictionary<string, VariantGroupViewModel> variantGroups, Dictionary<string, HashSet<string>> groupOptions)
        {
            if (variantInfo.VariantInfo != null)
            {
                VariantGroupViewModel variantGroup;
                if (!variantGroups.TryGetValue(variantInfo.VariantInfoGroupName, out variantGroup))
                {
                    variantGroup = new VariantGroupViewModel { Name = variantInfo.VariantInfoGroupName, Description = variantInfo.VariantInfoGroupDescription, Id = variantInfo.VariantInfoGroupId, Options = new List<VariantOptionViewModel>(), DisplayType = variantInfo.VariantGroupDisplayType };
                    variantGroups.Add(variantInfo.VariantInfoGroupName, variantGroup);
                    variants.Add(variantGroup);
                }

 

                HashSet<string> variantGroupOptions;
                if (!groupOptions.TryGetValue(variantGroup.Name, out variantGroupOptions))
                {
                    variantGroupOptions = new HashSet<string>();
                    groupOptions.Add(variantGroup.Name, variantGroupOptions);
                }

 

                foreach (var variantoption in variantInfo.VariantInfo.OrderBy(vo => vo.OptionSort))
                {
                    if (!variantGroupOptions.Contains(variantoption.OptionID))
                    {
                        variantGroup.Options.Add(new VariantOptionViewModel()
                        {
                            Id = variantoption.OptionID,
                            Name = variantoption.OptionName,
                            Color = variantoption.OptionColor,
                            SortOrder = variantoption.OptionSort,
                            OptionImage = variantoption.OptionImage,
                            Image = variantoption.Image
                        });
                        variantGroupOptions.Add(variantoption.OptionID);
                    }
                    FillVariants(variants, variantoption, variantGroups, groupOptions);
                }
            }
        }

 

Best Regards,

Nuno Aguiar


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Nuno

The VariantInfo property is already sorted as I see it - so adding this additional sort should not affect things:

Can you explain where you see the wrong sortorder?

Thanks, Nicolai

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

It seems that way to me too, but it's not looking like that on a customer project. I'll send you the link over email.

 

This does not seem to happen for all variant options, which is really odd too.

 

Best Regards,

Nuno Aguiar

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

Did you ever get the path to the site I sent you over email? By now the template patch may already be there, but I can remove it, because as odd as it seems, we could reproduce it there. Not sure what triggered it.

 

Nuno

 

You must be logged in to post in the forum