/// <summary> /// Maps a <see cref="ProductDisplay"/> to <see cref="IProduct"/> /// </summary> /// <param name="productDisplay"> /// The product display. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <returns> /// The <see cref="IProduct"/>. /// </returns> internal static IProduct ToProduct(this ProductDisplay productDisplay, IProduct destination) { if (productDisplay.Key != Guid.Empty) { destination.Key = productDisplay.Key; } productDisplay.CatalogInventories = productDisplay.CatalogInventories ?? Enumerable.Empty<CatalogInventoryDisplay>(); productDisplay.ProductOptions = productDisplay.ProductOptions ?? Enumerable.Empty<ProductOptionDisplay>(); destination.Name = productDisplay.Name; destination.Sku = productDisplay.Sku; destination.Price = productDisplay.Price; destination.CostOfGoods = productDisplay.CostOfGoods; destination.SalePrice = productDisplay.SalePrice; destination.OnSale = productDisplay.OnSale; destination.Manufacturer = productDisplay.Manufacturer; destination.ManufacturerModelNumber = productDisplay.ManufacturerModelNumber; destination.Weight = productDisplay.Weight; destination.Length = productDisplay.Length; destination.Width = productDisplay.Width; destination.Height = productDisplay.Height; destination.Barcode = productDisplay.Barcode; destination.Available = productDisplay.Available; destination.TrackInventory = productDisplay.TrackInventory; destination.OutOfStockPurchase = productDisplay.OutOfStockPurchase; destination.Taxable = productDisplay.Taxable; destination.Shippable = productDisplay.Shippable; destination.Download = productDisplay.Download; destination.DownloadMediaId = productDisplay.DownloadMediaId; // We need to refactor the CatalogInventories to not be immutable if we are // going to need to do operations like this. In the UI, the user "unchecks" a catalog that was // previously checked - so we need to remove it. var deletedCatalogKeys = destination.CatalogInventories.Where( x => !productDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray(); foreach (var deletedCatalogKey in deletedCatalogKeys) { ((Product)destination).MasterVariant.RemoveFromCatalogInventory(deletedCatalogKey); } foreach (var catalogInventory in productDisplay.CatalogInventories) { var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey); if (catInv != null) { var destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } else { //// Add to a new catalog ((Product)destination).MasterVariant.AddToCatalogInventory(new CatalogInventory(catalogInventory.CatalogKey, catalogInventory.ProductVariantKey) { Location = catalogInventory.Location, Count = catalogInventory.Count, LowCount = catalogInventory.LowCount }); } } // Fix option deletion here #M-161 // remove any product options that exist in destination and do not exist in productDisplay var removers = destination.ProductOptions.Where(x => !productDisplay.ProductOptions.Select(pd => pd.Key).Contains(x.Key)).Select(x => x.Key).ToList(); foreach (var remove in removers) { destination.ProductOptions.RemoveItem(remove); } foreach (var option in productDisplay.ProductOptions) { IProductOption destinationProductOption; if (destination.ProductOptions.Contains(option.Key)) { destinationProductOption = destination.ProductOptions[option.Key]; destinationProductOption = option.ToProductOption(destinationProductOption); } else { destinationProductOption = new ProductOption(option.Name, option.Required); destinationProductOption = option.ToProductOption(destinationProductOption); } destination.ProductOptions.Add(destinationProductOption); } destination.AddOrUpdateDetachedContent(productDisplay); return destination; }
/// <summary> /// Maps a <see cref="ProductDisplay"/> to <see cref="IProduct"/> /// </summary> /// <param name="productDisplay"> /// The product display. /// </param> /// <param name="destination"> /// The destination. /// </param> /// <returns> /// The <see cref="IProduct"/>. /// </returns> internal static IProduct ToProduct(this ProductDisplay productDisplay, IProduct destination) { if (productDisplay.Key != Guid.Empty) { destination.Key = productDisplay.Key; } productDisplay.CatalogInventories = productDisplay.CatalogInventories ?? Enumerable.Empty <CatalogInventoryDisplay>(); productDisplay.ProductOptions = productDisplay.ProductOptions ?? Enumerable.Empty <ProductOptionDisplay>(); destination.Name = productDisplay.Name; destination.Sku = productDisplay.Sku; destination.Price = productDisplay.Price; destination.CostOfGoods = productDisplay.CostOfGoods; destination.SalePrice = productDisplay.SalePrice; destination.OnSale = productDisplay.OnSale; destination.Manufacturer = productDisplay.Manufacturer; destination.ManufacturerModelNumber = productDisplay.ManufacturerModelNumber; destination.Weight = productDisplay.Weight; destination.Length = productDisplay.Length; destination.Width = productDisplay.Width; destination.Height = productDisplay.Height; destination.Barcode = productDisplay.Barcode; destination.Available = productDisplay.Available; destination.TrackInventory = productDisplay.TrackInventory; destination.OutOfStockPurchase = productDisplay.OutOfStockPurchase; destination.Taxable = productDisplay.Taxable; destination.Shippable = productDisplay.Shippable; destination.Download = productDisplay.Download; destination.DownloadMediaId = productDisplay.DownloadMediaId; // We need to refactor the CatalogInventories to not be immutable if we are // going to need to do operations like this. In the UI, the user "unchecks" a catalog that was // previously checked - so we need to remove it. var deletedCatalogKeys = destination.CatalogInventories.Where( x => !productDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray(); foreach (var deletedCatalogKey in deletedCatalogKeys) { ((Product)destination).MasterVariant.RemoveFromCatalogInventory(deletedCatalogKey); } foreach (var catalogInventory in productDisplay.CatalogInventories) { var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey); if (catInv != null) { var destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } else { //// Add to a new catalog ((Product)destination).MasterVariant.AddToCatalogInventory(new CatalogInventory(catalogInventory.CatalogKey, catalogInventory.ProductVariantKey) { Location = catalogInventory.Location, Count = catalogInventory.Count, LowCount = catalogInventory.LowCount }); } } // Fix option deletion here #M-161 // remove any product options that exist in destination and do not exist in productDisplay var removers = destination.ProductOptions.Where(x => !productDisplay.ProductOptions.Select(pd => pd.Key).Contains(x.Key)).Select(x => x.Key).ToList(); foreach (var remove in removers) { destination.ProductOptions.RemoveItem(remove); } foreach (var option in productDisplay.ProductOptions) { IProductOption destinationProductOption; if (destination.ProductOptions.Contains(option.Key)) { destinationProductOption = destination.ProductOptions[option.Key]; destinationProductOption = option.ToProductOption(destinationProductOption); } else { destinationProductOption = new ProductOption(option.Name, option.Required); destinationProductOption = option.ToProductOption(destinationProductOption); } destination.ProductOptions.Add(destinationProductOption); } destination.AddOrUpdateDetachedContent(productDisplay); return(destination); }