public static ProductModel Create(Product product, CultureInfo culture, CategoryTree categoryCache) { var doc = new ProductModel { Id = product.Id, Name = product.GetText("Name", culture) ?? product.Name }; // Brand if (product.Brand != null) { doc.Brand = product.Brand.GetText("Name", culture) ?? product.Brand.Name; doc.BrandId = product.Brand.Id; } // Categories var categoryNames = new HashSet <string>(); var categoryIds = new HashSet <int>(); foreach (var category in product.Categories) { foreach (var each in categoryCache.Find(category.Id).PathFromRoot()) { var name = Localizer.GetText(new EntityKey(typeof(Category), each.Id), "Name", culture); if (String.IsNullOrEmpty(name)) { name = each.Name; } categoryNames.Add(name); categoryIds.Add(each.Id); } } doc.Categories = categoryNames.ToList(); doc.CategoryIds = categoryIds.ToList(); // Prices foreach (var variant in product.Variants) { doc.Prices.Add(variant.Price); } doc.LowestPrice = product.LowestPrice; doc.HighestPrice = product.HighestPrice; // Variant fields var controls = FormControls.Controls().ToList(); foreach (var variant in product.Variants) { foreach (var fieldDef in product.ProductType.VariantFieldDefinitions) { var variantField = variant.VariantFields.FirstOrDefault(f => f.FieldName == fieldDef.Name); if (variantField != null) { string fieldValue = null; var control = controls.Find(c => c.Name == fieldDef.ControlType); if (control.IsSelectionList) { var item = fieldDef.SelectionItems.FirstOrDefault(i => i.Value == variantField.FieldValue); if (item != null) { fieldValue = product.ProductType.GetText("VariantFieldDefinitions[" + fieldDef.Name + "].SelectionItems[" + item.Value + "]", culture) ?? item.Text; } } else if (control.IsValuesPredefined) { fieldValue = product.ProductType.GetText("VariantFieldDefinitions[" + fieldDef.Name + "].DefaultValue", culture) ?? fieldDef.DefaultValue; } else { fieldValue = variant.GetText("VariantFields[" + fieldDef.Name + "]", culture) ?? variantField.FieldValue; } if (!String.IsNullOrEmpty(fieldValue)) { if (doc.VariantFieldValues.ContainsKey(fieldDef.Name)) { doc.VariantFieldValues[fieldDef.Name].Add(fieldValue); } else { doc.VariantFieldValues.Add(fieldDef.Name, new HashSet <string> { fieldValue }); } } } } } // Search text var searchText = new StringBuilder(); searchText.Append(doc.Name); searchText.Append(" ").Append(doc.Brand); searchText.Append(" ").Append(String.Join(" ", doc.Categories)); foreach (var each in doc.VariantFieldValues) { searchText.Append(" ").Append(String.Join(" ", each.Value)); } doc.SearchText = searchText.ToString(); return(doc); }