/// <summary> /// Compares the <see cref="ProductAttributeCollection"/> with other <see cref="IProductVariant"/>s of the <see cref="IProduct"/> pass /// to determine if the a variant already exists with the attributes passed /// </summary> /// <param name="product">The <see cref="IProduct"/> to reference</param> /// <param name="attributes"><see cref="ProductAttributeCollection"/> to compare</param> /// <returns>True/false indicating whether or not a <see cref="IProductVariant"/> already exists with the <see cref="ProductAttributeCollection"/> passed</returns> public bool ProductVariantWithAttributesExists(IProduct product, ProductAttributeCollection attributes) { var variants = GetByProductKey(product.Key).ToArray(); var keys = attributes.Select(x => x.Key); return(variants.Any(x => x.Attributes.All(z => keys.Contains(z.Key)))); }
/// <summary> /// Compares the <see cref="ProductAttributeCollection"/> with other <see cref="IProductVariant"/>s of the <see cref="IProduct"/> pass /// to determine if the a variant already exists with the attributes passed /// </summary> /// <param name="product">The <see cref="IProduct"/> to reference</param> /// <param name="attributes"><see cref="ProductAttributeCollection"/> to compare</param> /// <returns>True/false indicating whether or not a <see cref="IProductVariant"/> already exists with the <see cref="ProductAttributeCollection"/> passed</returns> public bool ProductVariantWithAttributesExists(IProduct product, ProductAttributeCollection attributes) { var variants = GetByProductKey(product.Key).ToArray(); //// http://issues.merchello.com/youtrack/issue/M-941 var keys = attributes.Select(x => x.Key); return(variants.Any(x => x.Attributes.All(z => keys.Contains(z.Key)))); //// return variants.Any(x => x.Attributes.Equals(attributes)); }
/// <summary> /// Creates a <see cref="IProductVariant" /> of the <see cref="IProduct" /> passed defined by the collection of /// <see cref="IProductAttribute" /> /// without saving it to the database /// </summary> /// <param name="product">The <see cref="IProduct" /></param> /// <param name="variants"></param> /// <param name="name">The name of the product variant</param> /// <param name="sku">The unique SKU of the product variant</param> /// <param name="price">The price of the product variant</param> /// <param name="attributes">The <see cref="ProductAttributeCollection" /></param> /// <returns> /// Either a new <see cref="IProductVariant" /> or, if one already exists with associated attributes, the existing /// <see cref="IProductVariant" /> /// </returns> internal IProductVariant CreateProductVariant(IProduct product, List <IProductVariant> variants, string name, string sku, decimal price, ProductAttributeCollection attributes) { Mandate.ParameterNotNull(product, "product"); Mandate.ParameterNotNull(attributes, "attributes"); Mandate.ParameterCondition(attributes.Count >= product.ProductOptions.Count(x => x.Required), "An attribute must be assigned for every required option"); //// http://issues.merchello.com/youtrack/issue/M-740 // verify there is not already a variant with these attributes ////Mandate.ParameterCondition(false == ProductVariantWithAttributesExists(product, attributes), "A ProductVariant already exists for the ProductAttributeCollection"); if (ProductVariantWithAttributesExists(product, variants, attributes)) { LogHelper.Debug <ProductVariantService>( "Attempt to create a new variant that already exists. Returning existing variant."); return(GetProductVariantWithAttributes(product, variants, attributes.Select(x => x.Key).ToArray())); } var newVariant = new ProductVariant(product.Key, attributes, name, sku, price) { CostOfGoods = product.CostOfGoods, SalePrice = product.SalePrice, OnSale = product.OnSale, Weight = product.Weight, Length = product.Length, Width = product.Width, Height = product.Height, Barcode = product.Barcode, Available = product.Available, Manufacturer = product.Manufacturer, ManufacturerModelNumber = product.ManufacturerModelNumber, TrackInventory = product.TrackInventory, OutOfStockPurchase = product.OutOfStockPurchase, Taxable = product.Taxable, Shippable = product.Shippable, Download = product.Download, VersionKey = Guid.NewGuid() }; // Update existing ones in memory for checks in next loop variants.Add(newVariant); return(newVariant); }
public void Can_Retrieve_A_ProductVariant_Given_A_Product_And_A_Collection_Of_AttributeIds() { //// Arrange var attributes = new ProductAttributeCollection { _product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blk"), _product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "Lg") }; Assert.IsTrue(_product.ProductVariants.Any()); //// Act var attIds = attributes.Select(x => x.Key).ToArray(); var retrieved = _productVariantService.GetProductVariantWithAttributes(_product, attIds); //// Assert Assert.NotNull(retrieved); }
public void Can_Retrieve_A_ProductVariant_Given_A_Product_And_A_Collection_Of_AttributeIds() { //// Arrange var attributes = new ProductAttributeCollection { _product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blk"), _product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "Lg") }; Assert.IsTrue(_product.ProductVariants.Any()); //// Act var attIds = attributes.Select(x => x.Key).ToArray(); var retrieved = _productVariantService.GetProductVariantWithAttributes(_product, attIds); //// Assert Assert.NotNull(retrieved); }
/// <summary> /// Creates a <see cref="IProductVariant"/> of the <see cref="IProduct"/> passed defined by the collection of <see cref="IProductAttribute"/> /// without saving it to the database /// </summary> /// <param name="product">The <see cref="IProduct"/></param> /// <param name="name">The name of the product variant</param> /// <param name="sku">The unique SKU of the product variant</param> /// <param name="price">The price of the product variant</param> /// <param name="attributes">The <see cref="ProductAttributeCollection"/></param> /// <returns>Either a new <see cref="IProductVariant"/> or, if one already exists with associated attributes, the existing <see cref="IProductVariant"/></returns> internal IProductVariant CreateProductVariant(IProduct product, string name, string sku, decimal price, ProductAttributeCollection attributes) { Mandate.ParameterNotNull(product, "product"); Mandate.ParameterNotNull(attributes, "attributes"); Mandate.ParameterCondition(attributes.Count >= product.ProductOptions.Count(x => x.Required), "An attribute must be assigned for every required option"); //// http://issues.merchello.com/youtrack/issue/M-740 // verify there is not already a variant with these attributes ////Mandate.ParameterCondition(false == ProductVariantWithAttributesExists(product, attributes), "A ProductVariant already exists for the ProductAttributeCollection"); if (this.ProductVariantWithAttributesExists(product, attributes)) { LogHelper.Debug<ProductVariantService>("Attempt to create a new variant that already exists. Returning existing variant."); return this.GetProductVariantWithAttributes(product, attributes.Select(x => x.Key).ToArray()); } return new ProductVariant(product.Key, attributes, name, sku, price) { CostOfGoods = product.CostOfGoods, SalePrice = product.SalePrice, OnSale = product.OnSale, Weight = product.Weight, Length = product.Length, Width = product.Width, Height = product.Height, Barcode = product.Barcode, Available = product.Available, Manufacturer = product.Manufacturer, ManufacturerModelNumber = product.ManufacturerModelNumber, TrackInventory = product.TrackInventory, OutOfStockPurchase = product.OutOfStockPurchase, Taxable = product.Taxable, Shippable = product.Shippable, Download = product.Download }; }