コード例 #1
0
        public ActionResult GetAvailableCombinations(VariantPickerVariantExistsViewModel viewModel)
        {
            var selectedDictionary = viewModel.VariantNameValueDictionary.Where(x => x.Value != "").ToList();

            var currentProduct = _catalogLibrary.GetProduct(viewModel.ProductSku);

            IList <ProductPropertiesViewModel>      result           = new List <ProductPropertiesViewModel>();
            IList <Ucommerce.Search.Models.Product> possibleVariants = new List <Ucommerce.Search.Models.Product>();

            foreach (var kvp in selectedDictionary)
            {
                var variants = _catalogLibrary.GetVariants(currentProduct);

                foreach (var v in variants)
                {
                    if (v.GetUserDefinedFields().Any(x =>
                                                     x.Key.Equals(kvp.Key, StringComparison.InvariantCultureIgnoreCase) &&
                                                     x.Value.ToString().Equals(kvp.Value, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        possibleVariants.Add(v);
                    }
                }

                foreach (var possibleVariant in possibleVariants)
                {
                    var properties = possibleVariant.GetUserDefinedFields()
                                     .Where(property => !property.Key.ToString().Equals(kvp.Key.ToString(), StringComparison.InvariantCultureIgnoreCase) &&
                                            !property.Value.ToString().Equals(kvp.Value.ToString(), StringComparison.InvariantCultureIgnoreCase));

                    foreach (var prop in properties)
                    {
                        ProductPropertiesViewModel property = new ProductPropertiesViewModel();
                        property.PropertyName = prop.Key;
                        property.Values.Add(prop.Value.ToString());

                        result.Add(property);
                    }
                }
            }

            return(Json(new { properties = result }));
        }
        public ActionResult GetAvailableCombinations(VariantPickerVariantExistsViewModel viewModel)
        {
            var selectedDictionary = viewModel.VariantNameValueDictionary.Where(x => x.Value != "").ToList();

            var currentProduct = _catalogLibraryInternal.GetProduct(viewModel.ProductSku);

            IList <ProductPropertiesViewModel> result = new List <ProductPropertiesViewModel>();
            IList <Product> possibleVariants          = new List <Product>();

            foreach (var kvp in selectedDictionary)
            {
                var variants = currentProduct.Variants;
                foreach (var v in variants)
                {
                    if (v.ProductProperties.Any(x => x.ProductDefinitionField.Name == kvp.Key && x.Value == kvp.Value))
                    {
                        possibleVariants.Add(v);
                    }
                }

                foreach (var possibleVariant in possibleVariants)
                {
                    var properties = ProductProperty.All()
                                     .Where(x => x.ProductDefinitionField.Name != kvp.Key && x.Value != kvp.Value && x.Product == possibleVariant).Distinct();
                    foreach (var prop in properties)
                    {
                        ProductPropertiesViewModel property = new ProductPropertiesViewModel();
                        property.PropertyName = prop.ProductDefinitionField.Name;
                        property.Values.Add(prop.Value);

                        result.Add(property);
                    }
                }
            }

            return(Json(new { properties = result }));
        }