public static VariationsCollection.Variation FirstOrDefault(this VariationsCollection.Variation variations, string levelId)
        {
            if (variations != null)
            {
                if (string.Equals(variations.Id, levelId, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(variations);
                }

                if (variations.Options.Length > 0)
                {
                    return(FirstOrDefault(variations.Options[0].Variations, levelId));
                }
            }

            return(null);
        }
        private static VariationsCollection.Variation GetVariationsByAttributes(Variations variations, string id, IList <ProductVariationsConfig.Variation> groupVariations, int variationLevel, List <string> attributeNames, List <string> attributeValues)
        {
            if (groupVariations.Count == variationLevel)
            {
                return(null);
            }

            var groupVariation    = groupVariations[variationLevel];
            var variationProducts = FilterVariationProductsByAttributes(variations.Product.ToList(), 0, attributeNames, attributeValues);
            var values            = variationProducts.Select(p => p.CustomAttributes.ValueByName <string>(groupVariation.AttributeName, null)).Distinct(StringComparer.InvariantCultureIgnoreCase);
            var options           = new List <VariationsCollection.Option>();

            foreach (var value in values)
            {
                var option   = CreateOption(variationProducts, variations, groupVariation, value);
                var attNames = new List <string>(attributeNames)
                {
                    groupVariation.AttributeName
                };
                var attValues = new List <string>(attributeValues)
                {
                    value
                };
                option.Variations = GetVariationsByAttributes(variations, id, groupVariations, variationLevel + 1, attNames, attValues);
                if (option.Variations == null)
                {
                    option.VariationProduct = GetProductByAttribute(variationProducts, groupVariation.AttributeName, value);
                }
                options.Add(option);
            }

            var result = new VariationsCollection.Variation
            {
                Id      = groupVariation.Id + "_" + id,
                Name    = groupVariation.Id,
                Options = options.ToArray()
            };

            return(result);
        }