/// <summary> /// Removes a product varaint from a catalog inventory. /// </summary> /// <param name="productVariant"> /// The product variant. /// </param> /// <param name="catalog"> /// The catalog. /// </param> public static void RemoveFromCatalogInventory(this IProductVariant productVariant, IWarehouseCatalog catalog) { if (productVariant.CatalogInventories.All(inv => inv.CatalogKey != catalog.Key)) { return; } productVariant.RemoveFromCatalogInventory(catalog.Key); }
internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination) { if (productVariantDisplay.Key != Guid.Empty) { destination.Key = productVariantDisplay.Key; } if( !String.IsNullOrEmpty(productVariantDisplay.Name) ) { destination.Name = productVariantDisplay.Name; } if( !String.IsNullOrEmpty(productVariantDisplay.Sku) ) { destination.Sku = productVariantDisplay.Sku; } destination.Price = productVariantDisplay.Price; destination.CostOfGoods = productVariantDisplay.CostOfGoods; destination.SalePrice = productVariantDisplay.SalePrice; destination.OnSale = productVariantDisplay.OnSale; destination.Manufacturer = productVariantDisplay.Manufacturer; destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber; destination.Weight = productVariantDisplay.Weight; destination.Length = productVariantDisplay.Length; destination.Width = productVariantDisplay.Width; destination.Height = productVariantDisplay.Height; destination.Barcode = productVariantDisplay.Barcode; destination.Available = productVariantDisplay.Available; destination.TrackInventory = productVariantDisplay.TrackInventory; destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase; destination.Taxable = productVariantDisplay.Taxable; destination.Shippable = productVariantDisplay.Shippable; destination.Download = productVariantDisplay.Download; destination.DownloadMediaId = productVariantDisplay.DownloadMediaId; destination.ProductKey = productVariantDisplay.ProductKey; // 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 => !productVariantDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray(); foreach (var deletedCatalogKey in deletedCatalogKeys) { destination.RemoveFromCatalogInventory(deletedCatalogKey); } foreach (var catalogInventory in productVariantDisplay.CatalogInventories) { var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey); if (catInv != null) { var destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } else if (!Guid.Empty.Equals(catalogInventory.CatalogKey) && destination.HasIdentity) { //// Add to a new catalog destination.AddToCatalogInventory(catalogInventory.CatalogKey); } } foreach (var attribute in productVariantDisplay.Attributes) { IProductAttribute destinationProductAttribute; var attr = destination.Attributes.FirstOrDefault(x => x.Key == attribute.Key); if (attr != null) { destinationProductAttribute = attr; destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); } else { destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku); destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection; variantAttributes.Add(destinationProductAttribute); } } destination.AddOrUpdateDetachedContent(productVariantDisplay); return destination; }
internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination) { if (productVariantDisplay.Key != Guid.Empty) { destination.Key = productVariantDisplay.Key; } if (!String.IsNullOrEmpty(productVariantDisplay.Name)) { destination.Name = productVariantDisplay.Name; } if (!String.IsNullOrEmpty(productVariantDisplay.Sku)) { destination.Sku = productVariantDisplay.Sku; } destination.Price = productVariantDisplay.Price; destination.CostOfGoods = productVariantDisplay.CostOfGoods; destination.SalePrice = productVariantDisplay.SalePrice; destination.OnSale = productVariantDisplay.OnSale; destination.Manufacturer = productVariantDisplay.Manufacturer; destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber; destination.Weight = productVariantDisplay.Weight; destination.Length = productVariantDisplay.Length; destination.Width = productVariantDisplay.Width; destination.Height = productVariantDisplay.Height; destination.Barcode = productVariantDisplay.Barcode; destination.Available = productVariantDisplay.Available; destination.TrackInventory = productVariantDisplay.TrackInventory; destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase; destination.Taxable = productVariantDisplay.Taxable; destination.Shippable = productVariantDisplay.Shippable; destination.Download = productVariantDisplay.Download; destination.DownloadMediaId = productVariantDisplay.DownloadMediaId; destination.ProductKey = productVariantDisplay.ProductKey; // 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 => !productVariantDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray(); foreach (var deletedCatalogKey in deletedCatalogKeys) { destination.RemoveFromCatalogInventory(deletedCatalogKey); } foreach (var catalogInventory in productVariantDisplay.CatalogInventories) { var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey); if (catInv != null) { var destinationCatalogInventory = catInv; destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory); } else if (!Guid.Empty.Equals(catalogInventory.CatalogKey) && destination.HasIdentity) { //// Add to a new catalog destination.AddToCatalogInventory(catalogInventory.CatalogKey); } } foreach (var attribute in productVariantDisplay.Attributes) { IProductAttribute destinationProductAttribute; var attr = destination.Attributes.FirstOrDefault(x => x.Key == attribute.Key); if (attr != null) { destinationProductAttribute = attr; destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); } else { destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku); destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute); ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection; variantAttributes.Add(destinationProductAttribute); } } destination.AddOrUpdateDetachedContent(productVariantDisplay); return(destination); }