public CartItemsDescriptionResponse(List <CartItem> cartItems, ProductResult productResult) : this() { CartItems = cartItems; foreach (var product in productResult.Products) { var matchingCartItem = CartItems.Find(x => x.ProductId == product.Id.ToString()); if (matchingCartItem != null) { var isGiftCard = product.Brand == "GIFT CARD"; var isOneSizeOnly = false; var sizeVariations = product.VariationAttributes.Find(x => x.Id == "size" && x.Values.Any()); var colorVariations = product.VariationAttributes.Find(x => x.Id == "color" && x.Values.Any()); if (sizeVariations != null && sizeVariations.Values.Count.Equals(1)) { isOneSizeOnly = sizeVariations.Values.First().Name == "One Size"; } DWImage image = null; var colorImages = product.ImageGroups.Where(z => z.ViewType == "hi-res" && z.VariationAttributes.Select(e => e.values.Where(v => v.value.Equals(product.Color)).FirstOrDefault()).Any() && z.Images.Any()) .Select(z => z.Images) .FirstOrDefault(); if (colorImages != null) { image = colorImages.FirstOrDefault(); } else { var genericImages = product.ImageGroups.Where(z => z.ViewType == "hi-res") .Select(z => z.Images) .FirstOrDefault(); if (genericImages != null) { image = genericImages.FirstOrDefault(); } } if (image != null) { matchingCartItem.Image = new Image { Src = APIHelper.GetOptimizedImageSrc(image.Link), Title = image.Title }; } else { matchingCartItem.Image = new Image { Src = "/assets/img/image_not_available.gif" }; } if (!string.IsNullOrEmpty(product.Size) && !isOneSizeOnly && !isGiftCard) { var decimalSize = 0.0M; Decimal.TryParse(product.Size, out decimalSize); if (decimalSize > 0) { matchingCartItem.Size = string.Format("{0:N1}", decimalSize / 10.0M); } else { matchingCartItem.Size = product.Size; } } if (!isOneSizeOnly && !isGiftCard) { matchingCartItem.Width = Config.Params.USTextInfo.ToTitleCase(product.Width); } if (colorVariations != null) { var itemColorVariant = colorVariations.Values.Find(x => x.Value == product.Color); if (itemColorVariant != null) { matchingCartItem.Color = itemColorVariant.Name; } } matchingCartItem.Description = product.Brand; } } }
public ProductListExtensionResponse(ProductResult productResult, List <string> colors, string htmlRaw = "") : this() { colors.Reverse(); foreach (var product in productResult.Products) { var productId = product.Id; if (!ProductIdToExtension.ContainsKey(productId)) { var extension = new ProductListItemExtension(); var availableVariations = product.VariationAttributes; availableVariations.RemoveAll(a => a.Values == null); List <VariationAttribute> outVA = new List <VariationAttribute>(); foreach (VariationAttribute va in availableVariations) { VariationAttribute attr = new VariationAttribute(); attr.Id = va.Id; attr.Name = va.Name; foreach (VariationAttributeValue innerVa in va.Values.ToList()) { if (va.Id == "color") { attr.Values.Add(new VariationAttributeValue() { Image = GetImagesForColor(product, innerVa.Value), Swatch = GetSwatchImage(product, innerVa.Value), Name = innerVa.Name, Value = innerVa.Value.ToLower(), IsOrderable = innerVa.IsOrderable }); } } outVA.Add(attr); } var colorVariations = availableVariations.Where(a => a.Id == "color").FirstOrDefault() ?? new VariationAttribute(); var applicableImageGroups = product.ImageGroups.Where(z => colorVariations.Values.Any(x => z.VariationAttributes.Select(e => e.values.Where(v => v.value.Contains(x.Value)).FirstOrDefault()).Any())); DWImage image = null; var colorImages = applicableImageGroups.Where(z => z.ViewType == "hi-res" && colors.Find(x => z.VariationAttributes.Select(e => e.values.Where(v => v.value.Contains(x)).FirstOrDefault()).Any()) != null && z.Images.Any()) .Select(z => z.Images) .FirstOrDefault(); if (colorImages != null) { image = colorImages.FirstOrDefault(); } else { var genericImages = (applicableImageGroups.Count() > 0 ? applicableImageGroups : product.ImageGroups).Where(z => z.ViewType == "hi-res") .Select(z => z.Images) .FirstOrDefault(); if (genericImages != null) { image = genericImages.FirstOrDefault(); } } if (image != null) { extension.Image.Src = APIHelper.GetOptimizedImageSrc(image.Link); extension.Image.Title = image.Title; } extension.Brand = product.Brand; extension.Rating = product.AverageRating; extension.IsAvailableInMultipleColors = colorVariations.Values.Count > 1; extension.AvailableVariations = outVA; extension.ProductFlags = product.ProductFlags; ProductIdToExtension.Add(productId, extension); extension.Callout = product.Promotions.Where(promo => promo.Callout != null).Select(a => a.Callout).ToList(); } SetProductBrands(productResult.Products, htmlRaw); } }