Exemplo n.º 1
0
        private void SaveProduct(coreModel.Catalog catalog, FulfillmentCenter defaultFulfillmentCenter, CsvProduct csvProduct)
        {
            var defaultLanguge = catalog.DefaultLanguage != null ? catalog.DefaultLanguage.LanguageCode : "EN-US";

            coreModel.CatalogProduct alreadyExistProduct = null;
            //For new product try to find them by code
            if (csvProduct.IsTransient() && !String.IsNullOrEmpty(csvProduct.Code))
            {
                var criteria = new SearchCriteria
                {
                    CatalogId     = catalog.Id,
                    CategoryId    = csvProduct.CategoryId,
                    Code          = csvProduct.Code,
                    ResponseGroup = SearchResponseGroup.WithProducts | SearchResponseGroup.WithVariations
                };
                var result = _searchService.Search(criteria);
                alreadyExistProduct = result.Products.FirstOrDefault();
                csvProduct.Id       = alreadyExistProduct != null ? alreadyExistProduct.Id : csvProduct.Id;
            }
            else if (!csvProduct.IsTransient())
            {
                //If id specified need check that product really exist
                alreadyExistProduct = _productService.GetById(csvProduct.Id, ItemResponseGroup.ItemInfo);
            }
            var isNewProduct = alreadyExistProduct == null;

            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 = !String.IsNullOrEmpty(csvProduct.CategoryId) ? _categoryService.GetById(csvProduct.CategoryId, CategoryResponseGroup.WithProperties).Properties : _catalogService.GetById(csvProduct.CatalogId).Properties;

            if (csvProduct.PropertyValues != null)
            {
                //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)
                            {
                                property = _propertyService.GetById(property.Id);
                                var dicValue = property.DictionaryValues.FirstOrDefault(x => String.Equals(x.Value, propertyValue.Value));
                                propertyValue.ValueId = dicValue != null ? dicValue.Id : null;
                            }
                        }
                    }
                }
            }

            if (!isNewProduct)
            {
                _productService.Update(new coreModel.CatalogProduct[] { csvProduct });
            }
            else
            {
                var newProduct = _productService.Create(csvProduct);
                csvProduct.Id = newProduct.Id;
            }

            //Create price in default price list

            if (csvProduct.Price.EffectiveValue > 0)
            {
                csvProduct.Price.ProductId = csvProduct.Id;

                if (csvProduct.Price.IsTransient() || _pricingService.GetPriceById(csvProduct.Price.Id) == null)
                {
                    _pricingService.CreatePrice(csvProduct.Price);
                }
                else
                {
                    _pricingService.UpdatePrices(new Price[] { csvProduct.Price });
                }
            }

            //Create inventory
            csvProduct.Inventory.ProductId           = csvProduct.Id;
            csvProduct.Inventory.FulfillmentCenterId = csvProduct.Inventory.FulfillmentCenterId ?? defaultFulfillmentCenter.Id;
            _inventoryService.UpsertInventory(csvProduct.Inventory);
        }