public void AddNewPropertiesProgrammably()
        {
            var propertyService = GetPropertyService();

            var newProperty = new Property
            {
                //Electronics
                CatalogId  = "4974648a41df4e6ea67ef2ad76d7bbd4",
                Dictionary = true,
                Name       = "color2",
                Type       = PropertyType.Product,
                ValueType  = PropertyValueType.ShortText
            };

            newProperty.DictionaryValues = new List <PropertyDictionaryValue>();
            var colorDictValue = new PropertyDictionaryValue
            {
                Alias = "green2",
                Value = "green2"
            };

            newProperty.DictionaryValues.Add(colorDictValue);
            newProperty = propertyService.Create(newProperty);

            var productService = GetItemService();
            var product        = productService.GetById("93beb4e92dba4a08a173aa0a0cf0cffb", ItemResponseGroup.ItemInfo);

            //Set value
            product.PropertyValues.Add(new PropertyValue {
                PropertyId = newProperty.Id, PropertyName = newProperty.Name, ValueId = colorDictValue.Id, Value = colorDictValue.Value, ValueType = newProperty.ValueType
            });
            productService.Update(new[] { product });
        }
Ejemplo n.º 2
0
        private static AttributeFilterValue ConvertToAttributeFilterValue(PropertyDictionaryValue dictionaryValue)
        {
            var result = new AttributeFilterValue
            {
                Id       = dictionaryValue.Alias,
                Value    = dictionaryValue.Value,
                Language = dictionaryValue.LanguageCode,
            };

            return(result);
        }
Ejemplo n.º 3
0
        private void LoadProductDependencies(IEnumerable <CsvProduct> csvProducts, Catalog catalog, out ICollection <Property> modifiedProperties)
        {
            modifiedProperties = new List <Property>();
            var allCategoriesIds = csvProducts.Select(x => x.CategoryId).Distinct().ToArray();
            var categoriesMap    = _categoryService.GetByIds(allCategoriesIds, CategoryResponseGroup.Full).ToDictionary(x => x.Id);
            var defaultLanguge   = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "en-US";

            foreach (var csvProduct in csvProducts)
            {
                csvProduct.Catalog   = catalog;
                csvProduct.CatalogId = catalog.Id;
                if (csvProduct.CategoryId != null)
                {
                    csvProduct.Category = categoriesMap[csvProduct.CategoryId];
                }

                //Try to set parent relations
                //By id or code reference
                var parentProduct = csvProducts.FirstOrDefault(x => csvProduct.MainProductId != null && (x.Id == csvProduct.MainProductId || x.Code == csvProduct.MainProductId));
                csvProduct.MainProduct   = parentProduct;
                csvProduct.MainProductId = parentProduct != null ? parentProduct.Id : null;

                if (string.IsNullOrEmpty(csvProduct.Code))
                {
                    csvProduct.Code = _skuGenerator.GenerateSku(csvProduct);
                }
                csvProduct.EditorialReview.LanguageCode = defaultLanguge;
                csvProduct.SeoInfo.LanguageCode         = defaultLanguge;
                csvProduct.SeoInfo.SemanticUrl          = string.IsNullOrEmpty(csvProduct.SeoInfo.SemanticUrl) ? csvProduct.Code : csvProduct.SeoInfo.SemanticUrl;

                //Properties inheritance
                csvProduct.Properties = (csvProduct.Category != null ? csvProduct.Category.Properties : csvProduct.Catalog.Properties).OrderBy(x => x.Name).ToList();
                foreach (var propertyValue in csvProduct.PropertyValues.ToArray())
                {
                    //Try to find property meta information
                    propertyValue.Property = csvProduct.Properties.FirstOrDefault(x => x.Name.EqualsInvariant(propertyValue.PropertyName));
                    if (propertyValue.Property != null)
                    {
                        propertyValue.ValueType = propertyValue.Property.ValueType;
                        if (propertyValue.Property.Dictionary)
                        {
                            var dicValue = propertyValue.Property.DictionaryValues.FirstOrDefault(x => Equals(x.Value, propertyValue.Value));
                            if (dicValue == null)
                            {
                                dicValue = new PropertyDictionaryValue
                                {
                                    Alias = propertyValue.Value.ToString(),
                                    Value = propertyValue.Value.ToString(),
                                    Id    = Guid.NewGuid().ToString()
                                };
                                //need to register modified property for future update
                                if (!modifiedProperties.Contains(propertyValue.Property))
                                {
                                    modifiedProperties.Add(propertyValue.Property);
                                }
                            }
                            propertyValue.ValueId = dicValue.Id;
                        }
                    }
                }
            }
        }
        private void SaveProducts(Catalog catalog, List <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback)
        {
            progressInfo.ProcessedCount = 0;
            progressInfo.TotalCount     = csvProducts.Count;

            var defaultFulfilmentCenter = _commerceService.GetAllFulfillmentCenters().FirstOrDefault();

            DetectParents(csvProducts);

            //Detect already exist product by Code
            using (var repository = _catalogRepositoryFactory())
            {
                var codes         = csvProducts.Where(x => x.IsTransient()).Select(x => x.Code).Where(x => x != null).Distinct().ToArray();
                var existProducts = repository.Items.Where(x => x.CatalogId == catalog.Id && codes.Contains(x.Code)).Select(x => new { Id = x.Id, Code = x.Code }).ToArray();
                foreach (var existProduct in existProducts)
                {
                    var product = csvProducts.FirstOrDefault(x => x.Code == existProduct.Code);
                    if (product != null)
                    {
                        product.Id = product.Id;
                    }
                }
            }

            var categoriesIds = csvProducts.Where(x => x.CategoryId != null).Select(x => x.CategoryId).Distinct().ToArray();
            var categpories   = _categoryService.GetByIds(categoriesIds, CategoryResponseGroup.WithProperties);

            var defaultLanguge    = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "EN-US";
            var changedProperties = new List <Property>();

            foreach (var csvProduct in csvProducts)
            {
                csvProduct.CatalogId = catalog.Id;
                if (string.IsNullOrEmpty(csvProduct.Code))
                {
                    csvProduct.Code = _skuGenerator.GenerateSku(csvProduct);
                }
                //Set a parent relations
                if (csvProduct.MainProductId == null && csvProduct.MainProduct != null)
                {
                    csvProduct.MainProductId = csvProduct.MainProduct.Id;
                }
                csvProduct.EditorialReview.LanguageCode = defaultLanguge;
                csvProduct.SeoInfo.LanguageCode         = defaultLanguge;
                csvProduct.SeoInfo.SemanticUrl          = string.IsNullOrEmpty(csvProduct.SeoInfo.SemanticUrl) ? csvProduct.Code : csvProduct.SeoInfo.SemanticUrl;

                var properties = catalog.Properties;
                if (csvProduct.CategoryId != null)
                {
                    var category = categpories.FirstOrDefault(x => x.Id == csvProduct.CategoryId);
                    if (category != null)
                    {
                        properties = category.Properties;
                    }
                }

                //Try to fill properties meta information for values
                foreach (var propertyValue in csvProduct.PropertyValues)
                {
                    if (propertyValue.Value != null)
                    {
                        var property = properties.FirstOrDefault(x => string.Equals(x.Name, propertyValue.PropertyName));
                        if (property != null)
                        {
                            propertyValue.ValueType = property.ValueType;
                            if (property.Dictionary)
                            {
                                var dicValue = property.DictionaryValues.FirstOrDefault(x => Equals(x.Value, propertyValue.Value));
                                if (dicValue == null)
                                {
                                    dicValue = new PropertyDictionaryValue
                                    {
                                        Alias = propertyValue.Value.ToString(),
                                        Value = propertyValue.Value.ToString(),
                                        Id    = Guid.NewGuid().ToString()
                                    };
                                    property.DictionaryValues.Add(dicValue);
                                    if (!changedProperties.Contains(property))
                                    {
                                        changedProperties.Add(property);
                                    }
                                }
                                propertyValue.ValueId = dicValue.Id;
                            }
                        }
                    }
                }
            }

            progressInfo.Description = string.Format("Saving property dictionary values...");
            progressCallback(progressInfo);
            _propertyService.Update(changedProperties.ToArray());

            var options = new ParallelOptions
            {
                MaxDegreeOfParallelism = 10
            };

            foreach (var group in new[] { csvProducts.Where(x => x.MainProduct == null), csvProducts.Where(x => x.MainProduct != null) })
            {
                var partSize   = 25;
                var partsCount = Math.Max(1, group.Count() / partSize);
                var parts      = group.Select((x, i) => new { Index = i, Value = x })
                                 .GroupBy(x => x.Index % partsCount)
                                 .Select(x => x.Select(v => v.Value));

                Parallel.ForEach(parts, options, products =>
                {
                    try
                    {
                        //Save main products first and then variations
                        var toUpdateProducts = products.Where(x => !x.IsTransient());
                        //Need to additional  check that  product with id exist
                        using (var repository = _catalogRepositoryFactory())
                        {
                            var updateProductIds = toUpdateProducts.Select(x => x.Id).ToArray();
                            var existProductIds  = repository.Items.Where(x => updateProductIds.Contains(x.Id)).Select(x => x.Id).ToArray();
                            toUpdateProducts     = toUpdateProducts.Where(x => existProductIds.Contains(x.Id)).ToList();
                        }
                        var toCreateProducts = products.Except(toUpdateProducts);
                        if (!toCreateProducts.IsNullOrEmpty())
                        {
                            _productService.Create(toCreateProducts.ToArray());
                        }
                        if (!toUpdateProducts.IsNullOrEmpty())
                        {
                            _productService.Update(toUpdateProducts.ToArray());
                        }

                        //Set productId for dependent objects
                        foreach (var product in products)
                        {
                            product.Inventory.ProductId           = product.Id;
                            product.Inventory.FulfillmentCenterId = product.Inventory.FulfillmentCenterId ?? defaultFulfilmentCenter.Id;
                            product.Price.ProductId = product.Id;
                        }

                        var inventories = products.Where(x => x.Inventory != null).Select(x => x.Inventory).ToArray();
                        _inventoryService.UpsertInventories(inventories);

                        var prices = products.Where(x => x.Price != null && x.Price.EffectiveValue > 0).Select(x => x.Price).ToArray();
                        _pricingService.SavePrices(prices);
                    }
                    catch (Exception ex)
                    {
                        lock (_lockObject)
                        {
                            progressInfo.Errors.Add(ex.ToString());
                            progressCallback(progressInfo);
                        }
                    }
                    finally
                    {
                        lock (_lockObject)
                        {
                            //Raise notification
                            progressInfo.ProcessedCount += products.Count();
                            progressInfo.Description     = string.Format("Saving products: {0} of {1} created", progressInfo.ProcessedCount, progressInfo.TotalCount);
                            progressCallback(progressInfo);
                        }
                    }
                });
            }
        }