private static void PopulateInventory(ref Variant variant, Data.CatalogItem item, Data.InventoryInfo inventory) { if (item.IsBuyable.HasValue && item.IsBuyable.Value && item.StartDate < DateTime.UtcNow && (!item.EndDate.HasValue || item.EndDate.Value > DateTime.UtcNow)) { if (item.TrackInventory.HasValue && item.TrackInventory.Value) { if (inventory != null && inventory.Status == Data.InventoryStatus.Enabled) { variant.InventoryManagement = inventory.FulfillmentCenterId; variant.InventoryPolicy = "deny"; variant.InventoryQuantity = inventory.InStockQuantity - inventory.ReservedQuantity; if (inventory.AllowBackorder && inventory.BackorderAvailabilityDate.HasValue && inventory.BackorderAvailabilityDate.Value > DateTime.UtcNow) { variant.InventoryPolicy = "continue"; } if (inventory.AllowPreorder && inventory.PreorderAvailabilityDate.HasValue && inventory.PreorderAvailabilityDate.Value > DateTime.UtcNow) { variant.InventoryPolicy = "continue"; } } } } }
public static Variant AsVariantWebModel(this Data.CatalogItem variation, Data.Price price, string[] options, IEnumerable <Data.Marketing.PromotionReward> rewards) { var variantModel = new Variant(); var variationImage = variation.PrimaryImage ?? (variation.Images != null ? variation.Images.FirstOrDefault() : null); string variantlUrlParameter = null;// HttpContext.Current.Request.QueryString["variant"]; string pathTemplate; if (variation is Data.Product) { pathTemplate = VirtualPathUtility.ToAbsolute("~/products/{0}"); } else { pathTemplate = VirtualPathUtility.ToAbsolute("~/products/{0}?variant={1}"); } var reward = rewards.FirstOrDefault(); variantModel.Barcode = null; // TODO variantModel.CompareAtPrice = price != null ? (price.Sale.HasValue ? price.Sale.Value : price.List) : 0; //variantModel.Id = variation.Id; variantModel.Id = variation.Code; variantModel.Image = variationImage != null?variationImage.AsWebModel(variation.Name, variation.MainProductId) : null; PopulateInventory(ref variantModel, variation); variantModel.Options = GetOptionValues(options, variation.VariationProperties); variantModel.Price = price != null ? (price.Sale.HasValue ? price.Sale.Value : price.List) : 0; if (reward != null) { if (reward.AmountType.Equals("absolute", StringComparison.OrdinalIgnoreCase)) { variantModel.Price -= reward.Amount; } if (reward.AmountType.Equals("relative", StringComparison.OrdinalIgnoreCase)) { variantModel.Price -= variantModel.Price * reward.Amount / 100; } } variantModel.Selected = variantlUrlParameter != null; variantModel.Sku = variation.Properties.ContainsKey("sku") ? variation.Properties["sku"] as string : variation.Code; variantModel.Title = variation.Name; variantModel.Url = string.Format(pathTemplate, variation.MainProductId, variation.Id); variantModel.Weight = variation.Weight.HasValue ? variation.Weight.Value : 0; variantModel.WeightInUnit = null; // TODO variantModel.WeightUnit = variation.WeightUnit; return(variantModel); }