Esempio n. 1
0
 public ProductFactory(ProductAttributeCollection productAttributes,
                       CatalogInventoryCollection catalogInventories, ProductOptionCollection productOptions, ProductVariantCollection productVariantCollection)
 {
     _productVariantFactory    = new ProductVariantFactory(productAttributes, catalogInventories);
     _productOptionCollection  = productOptions;
     _productVariantCollection = productVariantCollection;
 }
Esempio n. 2
0
        public void Can_Get_A_ProductVariant_From_GetVariantForPurchase_Method_Given_A_List_Of_Attributes()
        {
            //// Arrange
            var att = new ProductAttribute("Choice1", "choice")
            {
                Key = Guid.NewGuid()
            };
            var attCollection = new ProductAttributeCollection();

            attCollection.Add(att);
            var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false, false,
                                              "Variant", "variant", 5M);

            _product.ProductOptions.Add(new ProductOption("Option1")
            {
                Key = Guid.NewGuid()
            });
            _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att);
            _product.ProductVariants.Add(expected);

            //// Act
            var variant = _product.GetProductVariantForPurchase(attCollection);

            //// Assert
            Assert.NotNull(variant);
            Assert.AreEqual(expected.Key, variant.Key);
        }
        void BindGrid()
        {
            ProductAttributeCollection productAttributes = ProductAttributeManager.GetAllProductAttributes();

            gvProductAttributes.DataSource = productAttributes;
            gvProductAttributes.DataBind();
        }
Esempio n. 4
0
        public void Can_Serialize_A_Product_To_Xml()
        {
            //// Arrange
            var att = new ProductAttribute("Choice1", "choice")
            {
                Key = Guid.NewGuid()
            };
            var attCollection = new ProductAttributeCollection();

            attCollection.Add(att);
            var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false, false,
                                              "Variant", "variant", 5M);

            _product.ProductOptions.Add(new ProductOption("Option1")
            {
                Key = Guid.NewGuid()
            });
            _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att);
            _product.ProductVariants.Add(expected);

            //// Act
            var xml = _product.SerializeToXml();

            Console.Write(xml.ToString());

            //// Assert
            Assert.NotNull(xml);
        }
Esempio n. 5
0
        public void Can_Verify_ProductAttributeCollection_Equals_Returns_False_For_Different_Attributes_DifferentCount()
        {
            //// Arrange
            var collection = new ProductAttributeCollection()
            {
                new ProductAttribute("Att1", "Sku1")
                {
                    Key = Guid.NewGuid()
                },
                new ProductAttribute("Att2", "Sku2")
                {
                    Key = Guid.NewGuid()
                },
                new ProductAttribute("Att3", "Sku3")
                {
                    Key = Guid.NewGuid()
                },
                new ProductAttribute("Att4", "Sku4")
                {
                    Key = Guid.NewGuid()
                },
            };

            //// Act / Assert
            Console.Write("Collections are equal: " + _productVariant.Attributes.Equals(collection));
            Assert.IsFalse(_productVariant.Attributes.Equals(collection));
        }
Esempio n. 6
0
        /// <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">
        ///     Existing variants to check against
        /// </param>
        /// <param name="attributes">The <see cref="IProductVariant" /></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,
                                                      ProductAttributeCollection attributes)
        {
            var skuSeparator = MerchelloConfiguration.Current.DefaultSkuSeparator;

            // verify the order of the attributes so that a sku can be constructed in the same order as the UI
            var optionIds = product.ProductOptionsForAttributes(attributes).OrderBy(x => x.SortOrder).Select(x => x.Key)
                            .Distinct();

            // the base sku
            var sku  = product.Sku;
            var name = string.Format("{0} - ", product.Name);

            foreach (var att in optionIds.Select(key => attributes.FirstOrDefault(x => x.OptionKey == key))
                     .Where(att => att != null))
            {
                name += att.Name + " ";

                sku += skuSeparator + (string.IsNullOrEmpty(att.Sku)
                           ? Regex.Replace(att.Name, "[^0-9a-zA-Z]+", string.Empty)
                           : att.Sku);
            }

            return(CreateProductVariant(product, variants, name, sku, product.Price, attributes));
        }
        public void Can_Retrieve_All_Variants_For_A_Product()
        {
            //// Arrange
            var attributes1 = 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")
            };
            var attributes2 = 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 == "XL")
            };

            _productVariantService.CreateProductVariantWithKey(_product, attributes1);
            _productVariantService.CreateProductVariantWithKey(_product, attributes2);

            Assert.IsTrue(_product.ProductVariants.Count == 2);

            //// Act
            var variants = _productVariantService.GetByProductKey(_product.Key);

            //// Assert
            Assert.IsTrue(variants.Any());
            Assert.IsTrue(2 == variants.Count());
        }
Esempio n. 8
0
        /// <summary>
        ///     Creates a <see cref="IProductVariant" /> of the <see cref="IProduct" /> passed defined by the collection of
        ///     <see cref="IProductAttribute" />
        /// </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="IProductVariant" /></param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        /// <returns>
        ///     Either a new <see cref="IProductVariant" /> or, if one already exists with associated attributes, the existing
        ///     <see cref="IProductVariant" />
        /// </returns>
        public IProductVariant CreateProductVariantWithKey(IProduct product, string name, string sku, decimal price,
                                                           ProductAttributeCollection attributes, bool raiseEvents = true)
        {
            var productVariant = CreateProductVariant(product, GetByProductKey(product.Key).ToList(), name, sku, price,
                                                      attributes);

            if (raiseEvents)
            {
                if (Creating.IsRaisedEventCancelled(new Events.NewEventArgs <IProductVariant>(productVariant), this))
                {
                    ((ProductVariant)productVariant).WasCancelled = true;
                    return(productVariant);
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = UowProvider.GetUnitOfWork();
                using (var repository = RepositoryFactory.CreateProductVariantRepository(uow))
                {
                    repository.AddOrUpdate(productVariant);
                    uow.Commit();
                }
            }

            if (raiseEvents)
            {
                Created.RaiseEvent(new Events.NewEventArgs <IProductVariant>(productVariant), this);
            }

            product.ProductVariants.Add(productVariant);

            return(productVariant);
        }
Esempio n. 9
0
        /// <summary>
        /// Creates a collection of <see cref="IProductVariant"/> that can be created based on unmapped product options.
        /// </summary>
        /// <param name="product">The <see cref="IProduct"/></param>
        /// <returns>A collection of <see cref="IProductVariant"/></returns>
        /// <remarks>
        ///
        /// This is an expensive method due to the potential number of database calls and should probably
        /// be refactored
        ///
        /// </remarks>
        public IEnumerable <IProductVariant> GetProductVariantsThatCanBeCreated(IProduct product)
        {
            var variants = new List <IProductVariant>();

            if (!product.ProductOptions.Any())
            {
                return(variants);
            }

            var possibleCombinations = GetPossibleProductAttributeCombinations(product);

            foreach (var combo in possibleCombinations)
            {
                var attributes = new ProductAttributeCollection();

                foreach (var att in combo)
                {
                    attributes.Add(att);
                }

                if (!ProductVariantWithAttributesExists(product, attributes))
                {
                    variants.Add(CreateProductVariant(product, attributes));
                }
            }

            return(variants);
        }
Esempio n. 10
0
 /// <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)
 {
     using (var repository = _repositoryFactory.CreateProductVariantRepository(_uowProvider.GetUnitOfWork()))
     {
         return(repository.ProductVariantWithAttributesExists(product, attributes));
     }
 }
Esempio n. 11
0
 /// <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="variants">
 ///     Variants to check against
 /// </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>
 private bool ProductVariantWithAttributesExists(IProduct product, List <IProductVariant> variants,
                                                 ProductAttributeCollection attributes)
 {
     using (var repository = RepositoryFactory.CreateProductVariantRepository(UowProvider.GetUnitOfWork()))
     {
         return(repository.ProductVariantWithAttributesExists(product, variants, attributes));
     }
 }
Esempio n. 12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProductVariantFactory"/> class.
 /// </summary>
 /// <param name="productAttributes">
 /// The product attributes.
 /// </param>
 /// <param name="catalogInventories">
 /// The catalog inventories.
 /// </param>
 /// <param name="detachedContentCollection">
 /// The <see cref="DetachedContentCollection{IProductVariantDetachedContent}"/>
 /// </param>
 public ProductVariantFactory(ProductAttributeCollection productAttributes,
                              CatalogInventoryCollection catalogInventories,
                              DetachedContentCollection <IProductVariantDetachedContent> detachedContentCollection)
 {
     _productAttributeCollection = productAttributes;
     _catalogInventories         = catalogInventories;
     _detachedContentCollection  = detachedContentCollection;
 }
        /// <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))));
        }
Esempio n. 14
0
        public void Can_Add_A_New_Product_To_The_Index()
        {
            PreTestDataWorker.DeleteAllProducts();

            //// Arrange
            var provider = (ProductIndexer)ExamineManager.Instance.IndexProviderCollection["MerchelloProductIndexer"];

            provider.RebuildIndex();

            var searcher = ExamineManager.Instance.SearchProviderCollection["MerchelloProductSearcher"];

            var productVariantService = PreTestDataWorker.ProductVariantService;

            //// Act
            var product = MockProductDataMaker.MockProductCollectionForInserting(1).First();

            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Med"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
            product.Height      = 11M;
            product.Width       = 11M;
            product.Length      = 11M;
            product.CostOfGoods = 15M;
            product.OnSale      = true;
            product.SalePrice   = 18M;
            _productService.Save(product);


            var attributes = new ProductAttributeCollection()
            {
                product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blue"),
                product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "XL")
            };

            productVariantService.CreateProductVariantWithKey(product, attributes);

            provider.AddProductToIndex(product);

            //// Assert
            var criteria = searcher.CreateSearchCriteria("productvariant", BooleanOperation.And);

            criteria.Field("productKey", product.Key.ToString()).And().Field("master", "true");

            ISearchResults results = searcher.Search(criteria);

            Assert.IsTrue(results.Count() == 1);
            var result = results.First();

            Assert.NotNull(result.Fields["productOptions"]);

            provider.RebuildIndex();
        }
Esempio n. 15
0
 internal ProductVariant(Guid productKey, ProductAttributeCollection attributes, InventoryCollection inventory, bool template, string name, string sku, decimal price)
     : base(name, sku, price)
 {
     Mandate.ParameterNotNull(attributes, "attributes");
     Mandate.ParameterNotNull(inventory, "inventory");
     _productKey = productKey;
     _attibutes = attributes;
     _template = template;
 }
Esempio n. 16
0
 internal ProductVariant(Guid productKey, ProductAttributeCollection attributes, CatalogInventoryCollection catalogInventoryCollection, bool master, string name, string sku, decimal price)
     : base(name, sku, price, catalogInventoryCollection)
 {
     Mandate.ParameterNotNull(attributes, "attributes");
     Mandate.ParameterNotNull(catalogInventoryCollection, "warehouseInventory");
     _productKey = productKey;
     _attibutes = attributes;
     _master = master;
 }
        protected void gvProductVariantAttributes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ProductVariantAttribute productVariantAttribute = (ProductVariantAttribute)e.Row.DataItem;

                Button btnUpdate = e.Row.FindControl("btnUpdate") as Button;
                if (btnUpdate != null)
                {
                    btnUpdate.CommandArgument = e.Row.RowIndex.ToString();
                }

                DropDownList ddlProductAttribute = e.Row.FindControl("ddlProductAttribute") as DropDownList;
                if (ddlProductAttribute != null)
                {
                    ddlProductAttribute.Items.Clear();
                    ProductAttributeCollection productAttributes = ProductAttributeManager.GetAllProductAttributes();
                    foreach (ProductAttribute productAttribute in productAttributes)
                    {
                        ListItem item = new ListItem(productAttribute.Name,
                                                     productAttribute.ProductAttributeId.ToString());
                        ddlProductAttribute.Items.Add(item);
                        if (productAttribute.ProductAttributeId == productVariantAttribute.ProductAttributeId)
                        {
                            item.Selected = true;
                        }
                    }
                }

                DropDownList ddlAttributeControlType = e.Row.FindControl("ddlAttributeControlType") as DropDownList;
                {
                    if (ddlAttributeControlType != null)
                    {
                        CommonHelper.FillDropDownWithEnum(ddlAttributeControlType, typeof(AttributeControlTypeEnum));
                    }
                    CommonHelper.SelectListItem(ddlAttributeControlType, productVariantAttribute.AttributeControlTypeId);
                }

                HyperLink hlAttributeValues = e.Row.FindControl("hlAttributeValues") as HyperLink;
                if (hlAttributeValues != null)
                {
                    if (productVariantAttribute.ShouldHaveValues)
                    {
                        hlAttributeValues.Visible     = true;
                        hlAttributeValues.NavigateUrl = string.Format("{0}ProductVariantAttributeValues.aspx?ProductVariantAttributeID={1}",
                                                                      CommonHelper.GetStoreAdminLocation(),
                                                                      productVariantAttribute.ProductVariantAttributeId);
                        hlAttributeValues.Text = string.Format(GetLocaleResourceString("Admin.ProductVariantAttributes.Values.Count"), productVariantAttribute.ProductVariantAttributeValues.Count);
                    }
                    else
                    {
                        hlAttributeValues.Visible = false;
                    }
                }
            }
        }
Esempio n. 18
0
        public void Init()
        {
            _attributes = new ProductAttributeCollection()
                {
                    new ProductAttribute("Att1", "Sku1") { Key = Guid.NewGuid() },
                    new ProductAttribute("Att2", "Sku2") { Key = Guid.NewGuid() },
                    new ProductAttribute("Att3", "Sku3") { Key = Guid.NewGuid() }
                };

            _productVariant = new ProductVariant(Guid.NewGuid(), _attributes, new CatalogInventoryCollection(), false, "Product1", "P1", 11M);
        }
        /// <summary>
        /// Converts an enumeration of ProductAttributes to a ProductAttributecollection
        /// </summary>
        /// <param name="attributes">
        /// The attributes.
        /// </param>
        /// <returns>
        /// The <see cref="ProductAttributeCollection"/>.
        /// </returns>
        internal static ProductAttributeCollection ToProductAttributeCollection(this IEnumerable <IProductAttribute> attributes)
        {
            var collection = new ProductAttributeCollection();

            foreach (var att in attributes)
            {
                collection.Add(att);
            }

            return(collection);
        }
Esempio n. 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProductFactory"/> class.
 /// </summary>
 /// <param name="getProductAttributes">
 /// The product attributes.
 /// </param>
 /// <param name="getCatalogInventories">
 /// The catalog inventories.
 /// </param>
 /// <param name="getProductOptions">
 /// The product options.
 /// </param>
 /// <param name="getProductVariantCollection">
 /// The product variant collection.
 /// </param>
 /// <param name="getDetachedContentCollection">
 /// Gets the detached content collection
 /// </param>
 public ProductFactory(
     ProductAttributeCollection getProductAttributes,
     CatalogInventoryCollection getCatalogInventories,
     Func <Guid, ProductOptionCollection> getProductOptions,
     Func <Guid, ProductVariantCollection> getProductVariantCollection,
     DetachedContentCollection <IProductVariantDetachedContent> getDetachedContentCollection)
 {
     _productVariantFactory            = new ProductVariantFactory(getProductAttributes, getCatalogInventories, getDetachedContentCollection);
     this._getProductOptionCollection  = getProductOptions;
     this._getProductVariantCollection = getProductVariantCollection;
 }
Esempio n. 21
0
        /// <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));
        }
Esempio n. 22
0
        public void Can_Add_A_New_Product_To_The_Index()
        {
            PreTestDataWorker.DeleteAllProducts();

            //// Arrange
            var provider = (ProductIndexer) ExamineManager.Instance.IndexProviderCollection["MerchelloProductIndexer"];
            provider.RebuildIndex();

            var searcher = ExamineManager.Instance.SearchProviderCollection["MerchelloProductSearcher"];

            var productVariantService = PreTestDataWorker.ProductVariantService;

            //// Act
            var product = MockProductDataMaker.MockProductCollectionForInserting(1).First();
            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Med"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
            product.Height = 11M;
            product.Width = 11M;
            product.Length = 11M;
            product.CostOfGoods = 15M;
            product.OnSale = true;
            product.SalePrice = 18M;
            _productService.Save(product);

            var attributes = new ProductAttributeCollection()
            {
                product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blue" ),
                product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "XL")
            };

            productVariantService.CreateProductVariantWithKey(product, attributes);

            provider.AddProductToIndex(product);

            //// Assert
            var criteria = searcher.CreateSearchCriteria("productvariant", BooleanOperation.And);
            criteria.Field("productKey", product.Key.ToString()).And().Field("master", "true");

            ISearchResults results = searcher.Search(criteria);

            Assert.IsTrue(results.Count() == 1);
            var result = results.First();
            Assert.NotNull(result.Fields["productOptions"]);

            provider.RebuildIndex();
        }
Esempio n. 23
0
        internal ProductOption(string name, bool required, ProductAttributeCollection choices)
        {
            
            // This is required so that we can create attributes from the WebApi without a lot of             
            // round trip traffic to the db to generate the Key(s).  Key is virtual so also forces
            // this class to be sealed
            Key = Guid.NewGuid();
            HasIdentity = false;

            _name = name;
            _required = required;
            _choices = choices;
        }
        private void FillDropDowns()
        {
            this.ddlNewProductAttributes.Items.Clear();
            ProductAttributeCollection productAttributes = ProductAttributeManager.GetAllProductAttributes();

            foreach (ProductAttribute pa in productAttributes)
            {
                ListItem item2 = new ListItem(pa.Name, pa.ProductAttributeId.ToString());
                this.ddlNewProductAttributes.Items.Add(item2);
            }

            CommonHelper.FillDropDownWithEnum(this.ddlAttributeControlType, typeof(AttributeControlTypeEnum));
        }
Esempio n. 25
0
        public void Init()
        {
            var warehouseService = PreTestDataWorker.WarehouseService;

            _warehouse = warehouseService.GetDefaultWarehouse();

            var productVariantService = PreTestDataWorker.ProductVariantService;
            var productService        = PreTestDataWorker.ProductService;

            var product = MockProductDataMaker.MockProductCollectionForInserting(1).First();

            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Med"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
            product.Height                  = 11M;
            product.Width                   = 11M;
            product.Length                  = 11M;
            product.CostOfGoods             = 15M;
            product.OnSale                  = true;
            product.SalePrice               = 18M;
            product.Manufacturer            = "Nike";
            product.ManufacturerModelNumber = "N01-012021-A";
            product.TrackInventory          = true;
            productService.Save(product);

            _productKey = product.Key;

            var attributes = new ProductAttributeCollection()
            {
                product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blue"),
                product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "XL")
            };

            var variant = productVariantService.CreateProductVariantWithKey(product, attributes);

            variant.AddToCatalogInventory(_warehouse.DefaultCatalog());
            productVariantService.Save(variant);
            _productVariantKey = variant.Key;
            _examineId         = ((ProductVariant)variant).ExamineId;

            var provider = (ProductIndexer)ExamineManager.Instance.IndexProviderCollection["MerchelloProductIndexer"];

            provider.AddProductToIndex(product);
        }
Esempio n. 26
0
        public void Can_Verify_ProductAttributeCollection_Equals_Returns_False_For_Different_Attributes_SameCount()
        {
            //// Arrange
            var collection = new ProductAttributeCollection()
                {
                    new ProductAttribute("Att1", "Sku1") { Key = Guid.NewGuid() },
                    new ProductAttribute("Att6", "Sku6") { Key = Guid.NewGuid() },
                    new ProductAttribute("Att3", "Sku3") { Key = Guid.NewGuid() }
                };

            //// Act / Assert
            Console.Write("Collections are equal: " + _productVariant.Attributes.Equals(collection));
            Assert.IsFalse(_productVariant.Attributes.Equals(collection));
        }
Esempio n. 27
0
        public void Can_Not_Create_A_Duplicate_ProductVariant()
        {
            //// 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")
            };

            //// Act


            //// Assert
            Assert.Throws <ArgumentException>(() => _productVariantService.CreateProductVariantWithKey(_product, attributes));
        }
Esempio n. 28
0
        public void Can_Create_A_ProductVariant()
        {
            //// 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")
            };

            //// Act
            var variant = _productVariantService.CreateProductVariantWithKey(_product, attributes);

            //// Assert
            Assert.IsTrue(variant.HasIdentity);
        }
Esempio n. 29
0
        public void Can_RetrieveProductOptions_From_ProductInIndex()
        {
            //// Arrange

            var merchello = new MerchelloHelper(MerchelloContext.Services);

            var productVariantService = PreTestDataWorker.ProductVariantService;
            var productService        = PreTestDataWorker.ProductService;

            var product = MockProductDataMaker.MockProductCollectionForInserting(1).First();

            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Med"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
            product.Height      = 11M;
            product.Width       = 11M;
            product.Length      = 11M;
            product.CostOfGoods = 15M;
            product.OnSale      = true;
            product.SalePrice   = 18M;
            productService.Save(product);


            var attributes = new ProductAttributeCollection()
            {
                product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blue"),
                product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "XL")
            };

            productVariantService.CreateProductVariantWithKey(product, attributes);

            _provider.AddProductToIndex(product);

            //// Act
            var productDisplay = merchello.Query.Product.GetByKey(product.Key);

            //// Assert
            Assert.NotNull(productDisplay);
            Assert.IsTrue(productDisplay.ProductOptions.Any());
        }
        private static ProductAttributeCollection DBMapping(DBProductAttributeCollection dbCollection)
        {
            if (dbCollection == null)
            {
                return(null);
            }

            ProductAttributeCollection collection = new ProductAttributeCollection();

            foreach (DBProductAttribute dbItem in dbCollection)
            {
                ProductAttribute item = DBMapping(dbItem);
                collection.Add(item);
            }

            return(collection);
        }
Esempio n. 31
0
        public ProductVariantDisplay NewProductVariant(ProductVariantDisplay productVariant)
        {
            IProductVariant newProductVariant;

            try
            {
                var product = _productService.GetByKey(productVariant.ProductKey) as Product;

                if (product != null)
                {
                    var productAttributes = new ProductAttributeCollection();
                    foreach (var attribute in productVariant.Attributes)
                    {
                        // TODO: This should be refactored into an extension method
                        var productOption = product.ProductOptions.FirstOrDefault(x => x.Key == attribute.OptionKey) as ProductOption;

                        if (productOption != null)
                        {
                            productAttributes.Add(productOption.Choices[attribute.Key]);
                        }
                    }

                    newProductVariant = _productVariantService.CreateProductVariantWithKey(product, productAttributes);

                    if (productVariant.TrackInventory)
                    {
                        newProductVariant.AddToCatalogInventory(_warehouseService.GetDefaultWarehouse().DefaultCatalog());
                    }

                    newProductVariant = productVariant.ToProductVariant(newProductVariant);

                    _productVariantService.Save(newProductVariant);
                }
                else
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError));
                }
            }
            catch (Exception e)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }

            return(newProductVariant.ToProductVariantDisplay());
        }
Esempio n. 32
0
        /// <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);
        }
Esempio n. 33
0
        public void Can_RetrieveProductOptions_From_ProductInIndex()
        {
            //// Arrange

            var merchello = new MerchelloHelper();

            var productVariantService = PreTestDataWorker.ProductVariantService;
            var productService = PreTestDataWorker.ProductService;

            var product = MockProductDataMaker.MockProductCollectionForInserting(1).First();
            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Med"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
            product.Height = 11M;
            product.Width = 11M;
            product.Length = 11M;
            product.CostOfGoods = 15M;
            product.OnSale = true;
            product.SalePrice = 18M;
            productService.Save(product);

            var attributes = new ProductAttributeCollection()
            {
                product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blue" ),
                product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "XL")
            };

            productVariantService.CreateProductVariantWithKey(product, attributes);

            _provider.AddProductToIndex(product);

            //// Act
            var productDisplay = merchello.Product(product.Key);

            //// Assert
            Assert.NotNull(productDisplay);
            Assert.IsTrue(productDisplay.ProductOptions.Any());
        }
Esempio n. 34
0
        public void Can_Map_ProductOption_To_ProductOptionDisplay()
        {
            //// Arrange
            var attributes = new ProductAttributeCollection
                {
                    new ProductAttribute("One", "One"),
                    new ProductAttribute("Two", "Two"),
                    new ProductAttribute("Three", "Three"),
                    new ProductAttribute("Four", "Four")
                };

            var productOption = new ProductOption("Numbers", true, attributes);

            //// Act
            var productOptionDisplay = AutoMapper.Mapper.Map<ProductOptionDisplay>(productOption);

            //// Assert
            Assert.NotNull(productOptionDisplay);
        }
Esempio n. 35
0
        public void Can_Get_A_ProductVariant_From_GetVariantForPurchase_Method_Given_A_List_Of_Attributes()
        {
            //// Arrange
            var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() };
            var attCollection = new ProductAttributeCollection();
            attCollection.Add(att);
            var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false,
                "Variant", "variant", 5M);
            _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() });
            _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att);
            _product.ProductVariants.Add(expected);

            //// Act
            var variant = _product.GetProductVariantForPurchase(attCollection);

            //// Assert
            Assert.NotNull(variant);
            Assert.AreEqual(expected.Key, variant.Key);
        }
Esempio n. 36
0
        public void Can_Map_ProductOption_To_ProductOptionDisplay()
        {
            //// Arrange
            var attributes = new ProductAttributeCollection
            {
                new ProductAttribute("One", "One"),
                new ProductAttribute("Two", "Two"),
                new ProductAttribute("Three", "Three"),
                new ProductAttribute("Four", "Four")
            };

            var productOption = new ProductOption("Numbers", true, attributes);

            //// Act
            var productOptionDisplay = AutoMapper.Mapper.Map <ProductOptionDisplay>(productOption);

            //// Assert
            Assert.NotNull(productOptionDisplay);
        }
Esempio n. 37
0
        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);
        }
Esempio n. 38
0
        /// <summary>
        /// Creates a <see cref="IProductVariant"/> of the <see cref="IProduct"/> passed defined by the collection of <see cref="IProductAttribute"/>
        /// </summary>
        /// <param name="product">The <see cref="IProduct"/></param>
        /// <param name="attributes">The <see cref="IProductVariant"/></param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        /// <returns>Either a new <see cref="IProductVariant"/> or, if one already exists with associated attributes, the existing <see cref="IProductVariant"/></returns>
        public IProductVariant CreateProductVariantWithKey(IProduct product, ProductAttributeCollection attributes, bool raiseEvents = true)
        {
            var skuSeparator = MerchelloConfiguration.Current.DefaultSkuSeparator;

            // verify the order of the attributes so that a sku can be constructed in the same order as the UI
            var optionIds = product.ProductOptionsForAttributes(attributes).OrderBy(x => x.SortOrder).Select(x => x.Key).Distinct();

            // the base sku
            var sku = product.Sku;
            var name = string.Format("{0} - ", product.Name);

            foreach (var att in optionIds.Select(key => attributes.FirstOrDefault(x => x.OptionKey == key)).Where(att => att != null))
            {
                name += att.Name + " ";

                sku += skuSeparator + (string.IsNullOrEmpty(att.Sku) ? Regex.Replace(att.Name, "[^0-9a-zA-Z]+", string.Empty) : att.Sku);
            }

            return CreateProductVariantWithKey(product, name.Trim(), sku, product.Price, attributes, raiseEvents);
        }
Esempio n. 39
0
        private ProductAttributeCollection GetProductAttributeCollection(Guid optionKey)
        {
            var sql = new Sql();

            sql.Select("*")
            .From <ProductAttributeDto>()
            .Where <ProductAttributeDto>(x => x.OptionKey == optionKey);

            var dtos = Database.Fetch <ProductAttributeDto>(sql);

            var attributes = new ProductAttributeCollection();
            var factory    = new ProductAttributeFactory();

            foreach (var dto in dtos)
            {
                var attribute = factory.BuildEntity(dto);
                attributes.Add(attribute);
            }
            return(attributes);
        }
Esempio n. 40
0
        public void Init()
        {
            _attributes = new ProductAttributeCollection()
            {
                new ProductAttribute("Att1", "Sku1")
                {
                    Key = Guid.NewGuid()
                },
                new ProductAttribute("Att2", "Sku2")
                {
                    Key = Guid.NewGuid()
                },
                new ProductAttribute("Att3", "Sku3")
                {
                    Key = Guid.NewGuid()
                }
            };

            _productVariant = new ProductVariant(Guid.NewGuid(), _attributes, new CatalogInventoryCollection(), false, false, "Product1", "P1", 11M);
        }
Esempio n. 41
0
        /// <summary>
        /// Creates a <see cref="IProductVariant"/> of the <see cref="IProduct"/> passed defined by the collection of <see cref="IProductAttribute"/>
        /// </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="IProductVariant"/></param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        /// <returns>Either a new <see cref="IProductVariant"/> or, if one already exists with associated attributes, the existing <see cref="IProductVariant"/></returns>
        public IProductVariant CreateProductVariantWithKey(IProduct product, string name, string sku, decimal price, ProductAttributeCollection attributes, bool raiseEvents = true)
        {
            var productVariant = CreateProductVariant(product, name, sku, price, attributes);

            if(raiseEvents)
            if (Creating.IsRaisedEventCancelled(new Events.NewEventArgs<IProductVariant>(productVariant), this))
            {
                ((ProductVariant)productVariant).WasCancelled = true;
                return productVariant;
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateProductVariantRepository(uow))
                {
                    repository.AddOrUpdate(productVariant);
                    uow.Commit();
                }
            }

            if (raiseEvents)
            Created.RaiseEvent(new Events.NewEventArgs<IProductVariant>(productVariant), this);

            product.ProductVariants.Add(productVariant);

            return productVariant;
        }
        public ProductVariantDisplay NewProductVariant(ProductVariantDisplay productVariant)
        {
            IProductVariant newProductVariant;

            try
            {
                var product = _productService.GetByKey(productVariant.ProductKey) as Product;

                if (product != null)
                {
                    var productAttributes = new ProductAttributeCollection();
                    foreach (var attribute in productVariant.Attributes)
                    {
                        // TODO: This should be refactored into an extension method
                        var productOption = product.ProductOptions.FirstOrDefault(x => x.Key == attribute.OptionKey) as ProductOption;

                        if (productOption != null) productAttributes.Add(productOption.Choices[attribute.Key]);
                    }

                    newProductVariant = _productVariantService.CreateProductVariantWithKey(product, productAttributes);

                    if (productVariant.TrackInventory)
                    {
                        newProductVariant.AddToCatalogInventory(_warehouseService.GetDefaultWarehouse().DefaultCatalog());
                    }

                    newProductVariant = productVariant.ToProductVariant(newProductVariant);

                    _productVariantService.Save(newProductVariant);
                }
                else
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError));
                }
            }
            catch (Exception e)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }

            return newProductVariant.ToProductVariantDisplay();
        }
Esempio n. 43
0
        public void Init()
        {
            var warehouseService = PreTestDataWorker.WarehouseService;
            _warehouse = warehouseService.GetDefaultWarehouse();

            var productVariantService = PreTestDataWorker.ProductVariantService;
            var productService = PreTestDataWorker.ProductService;

            var product = MockProductDataMaker.MockProductCollectionForInserting(1).First();
            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Red", "Red"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Med"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("X-Large", "XL"));
            product.Height = 11M;
            product.Width = 11M;
            product.Length = 11M;
            product.CostOfGoods = 15M;
            product.OnSale = true;
            product.SalePrice = 18M;
            product.Manufacturer = "Nike";
            product.ManufacturerModelNumber = "N01-012021-A";
            product.TrackInventory = true;
            productService.Save(product);

            _productKey = product.Key;

            var attributes = new ProductAttributeCollection()
            {
                product.ProductOptions.First(x => x.Name == "Color").Choices.First(x => x.Sku == "Blue"),
                product.ProductOptions.First(x => x.Name == "Size").Choices.First(x => x.Sku == "XL" )
            };

            var variant = productVariantService.CreateProductVariantWithKey(product, attributes);
            variant.AddToCatalogInventory(_warehouse.DefaultCatalog());
            productVariantService.Save(variant);
            _productVariantKey = variant.Key;
            _examineId = ((ProductVariant) variant).ExamineId;

            var provider = (ProductIndexer)ExamineManager.Instance.IndexProviderCollection["MerchelloProductIndexer"];
            provider.AddProductToIndex(product);
        }
Esempio n. 44
0
        public void Can_Delete_A_Variant()
        {
            //// 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" )
            };
            var variant = _productVariantService.CreateProductVariantWithKey(_product, attributes);
            var key = variant.Key;
            Assert.IsTrue(_product.ProductVariants.Any());

            //// Act
            _productVariantService.Delete(variant);

            //// Assert
            var retrieved = _productVariantService.GetByKey(key);

            Assert.IsNull(retrieved);
        }
Esempio n. 45
0
        public void Can_Retrieve_All_Variants_For_A_Product()
        {
            //// Arrange
            var attributes1 = 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" )
            };
            var attributes2 = 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 == "XL" )
            };
            _productVariantService.CreateProductVariantWithKey(_product, attributes1);
            _productVariantService.CreateProductVariantWithKey(_product, attributes2);

            Assert.IsTrue(_product.ProductVariants.Count == 2);

            //// Act
            var variants = _productVariantService.GetByProductKey(_product.Key);

            //// Assert
               Assert.IsTrue(variants.Any());
               Assert.IsTrue(2 == variants.Count());
        }
Esempio n. 46
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProductVariant"/> class.
 /// </summary>
 /// <param name="productKey">
 /// The product key.
 /// </param>
 /// <param name="attributes">
 /// The attributes.
 /// </param>
 /// <param name="name">
 /// The name.
 /// </param>
 /// <param name="sku">
 /// The SKU.
 /// </param>
 /// <param name="price">
 /// The price.
 /// </param>
 internal ProductVariant(
     Guid productKey,
     ProductAttributeCollection attributes,
     string name,
     string sku,
     decimal price)
     : this(productKey, attributes, new CatalogInventoryCollection(), false, name, sku, price)
 {
 }
Esempio n. 47
0
        /// <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
            };
        }
Esempio n. 48
0
        /// <summary>
        /// Converts an enumeration of ProductAttributes to a ProductAttributecollection
        /// </summary>
        /// <param name="attributes">
        /// The attributes.
        /// </param>
        /// <returns>
        /// The <see cref="ProductAttributeCollection"/>.
        /// </returns>
        internal static ProductAttributeCollection ToProductAttributeCollection(this IEnumerable<IProductAttribute> attributes)
        {
            var collection = new ProductAttributeCollection();
            foreach (var att in attributes)
            {
                collection.Add(att);
            }

            return collection;
        }
Esempio n. 49
0
        public void Can_Verify_That_ProductVariant_Is_Deleted_When_An_Option_Is_Deleted()
        {
            //// 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" )
            };
            _productVariantService.CreateProductVariantWithKey(_product, attributes);

            Assert.IsTrue(_product.ProductVariants.Any());

            //// Act
            _product.ProductOptions.Remove(_product.ProductOptions.First(x => x.Name == "Size"));
            _productService.Save(_product);

            //// Assert
            Assert.IsFalse(_product.ProductVariants.Any());
        }
Esempio n. 50
0
        public void Can_Retrieve_A_ProductVariant_by_Its_Key()
        {
            //// 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" )
            };
            var expected = _productVariantService.CreateProductVariantWithKey(_product, attributes);
            var id = expected.Key;
            Assert.AreNotEqual(id, Guid.Empty);

            //// Act
            var retrieved = _productVariantService.GetByKey(id);

            //// Assert
            Assert.NotNull(retrieved);
            Assert.IsTrue(id == retrieved.Key);
        }
Esempio n. 51
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProductVariant"/> class.
        /// </summary>
        /// <param name="productKey">
        /// The product key.
        /// </param>
        /// <param name="attributes">
        /// The attributes.
        /// </param>
        /// <param name="catalogInventoryCollection">
        /// The catalog inventory collection.
        /// </param>
        /// <param name="detachedContents">
        /// The detached contents.
        /// </param>
        /// <param name="master">
        /// The master.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="sku">
        /// The SKU.
        /// </param>
        /// <param name="price">
        /// The price.
        /// </param>
        internal ProductVariant(Guid productKey, ProductAttributeCollection attributes, CatalogInventoryCollection catalogInventoryCollection, DetachedContentCollection<IProductVariantDetachedContent> detachedContents,  bool master, string name, string sku, decimal price)
            : base(name, sku, price, catalogInventoryCollection, detachedContents)
        {
            Ensure.ParameterNotNull(attributes, "attributes");
            Ensure.ParameterNotNull(catalogInventoryCollection, "warehouseInventory");

            _productKey = productKey;
            _attibutes = attributes;
            _master = master;
        }
Esempio n. 52
0
        public void Can_Create_A_ProductVariant()
        {
            //// 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" )
            };

            //// Act
            var variant = _productVariantService.CreateProductVariantWithKey(_product, attributes);

            //// Assert
            Assert.IsTrue(variant.HasIdentity);
        }
Esempio n. 53
0
        public void Can_Serialize_A_Product_To_Xml()
        {
            //// Arrange
            var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() };
            var attCollection = new ProductAttributeCollection();
            attCollection.Add(att);
            var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false,
                "Variant", "variant", 5M);
            _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() });
            _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att);
            _product.ProductVariants.Add(expected);

            //// Act
            var xml = _product.SerializeToXml();
            Console.Write(xml.ToString());

            //// Assert
            Assert.NotNull(xml);
        }
Esempio n. 54
0
 /// <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)
 {
     using (var repository = _repositoryFactory.CreateProductVariantRepository(_uowProvider.GetUnitOfWork()))
     {
         return repository.ProductVariantWithAttributesExists(product, attributes);
     }
 }
Esempio n. 55
0
        public void Can_Not_Create_A_Duplicate_ProductVariant()
        {
            //// 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" )
            };

            //// Act

            //// Assert
            Assert.Throws<ArgumentException>(() => _productVariantService.CreateProductVariantWithKey(_product, attributes));
        }
Esempio n. 56
0
        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);
        }
Esempio n. 57
0
        /// <summary>
        /// Creates a collection of <see cref="IProductVariant"/> that can be created based on unmapped product options.
        /// </summary>
        /// <param name="product">The <see cref="IProduct"/></param>
        /// <returns>A collection of <see cref="IProductVariant"/></returns>
        /// <remarks>
        /// 
        /// This is an expensive method due to the potential number of database calls and should probably 
        /// be refactored
        /// 
        /// </remarks>
        public IEnumerable<IProductVariant> GetProductVariantsThatCanBeCreated(IProduct product)
        {
            var variants = new List<IProductVariant>();

            if (!product.ProductOptions.Any()) return variants;

            var possibleCombinations = GetPossibleProductAttributeCombinations(product);

            foreach (var combo in possibleCombinations)
            {
                var attributes = new ProductAttributeCollection();

                foreach(var att in combo) attributes.Add(att);

                if (!ProductVariantWithAttributesExists(product, attributes)) variants.Add(CreateProductVariant(product, attributes));
            }

            return variants;
        }
Esempio n. 58
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ProductVariant"/> class.
 /// </summary>
 /// <param name="productKey">
 /// The product key.
 /// </param>
 /// <param name="attributes">
 /// The attributes.
 /// </param>
 /// <param name="catalogInventoryCollection">
 /// The catalog inventory collection.
 /// </param>
 /// <param name="master">
 /// The master.
 /// </param>
 /// <param name="name">
 /// The name.
 /// </param>
 /// <param name="sku">
 /// The SKU.
 /// </param>
 /// <param name="price">
 /// The price.
 /// </param>
 internal ProductVariant(Guid productKey, ProductAttributeCollection attributes, CatalogInventoryCollection catalogInventoryCollection, bool master, string name, string sku, decimal price)
     : this(productKey, attributes, catalogInventoryCollection, new DetachedContentCollection<IProductVariantDetachedContent>(), false, name, sku, price)
 {
 }
Esempio n. 59
0
        /// <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"><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"><see cref="IProductVariant"/></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");
            // verify there is not already a variant with these attributes
            Mandate.ParameterCondition(false == ProductVariantWithAttributesExists(product, attributes), "A ProductVariant already exists for the ProductAttributeCollection");

            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,
                TrackInventory = product.TrackInventory,
                OutOfStockPurchase = product.OutOfStockPurchase,
                Taxable = product.Taxable,
                Shippable = product.Shippable,
                Download = product.Download
            };
        }
Esempio n. 60
0
 public ProductVariant(Guid productKey, ProductAttributeCollection attributes, InventoryCollection inventory, string name, string sku, decimal price)
     : this(productKey, attributes, inventory, false, name, sku, price)
 {
 }