示例#1
0
        /// <summary>
        /// This function provides an example of how to create a product in EA given a Magento SKU
        /// This function specifically creates a Master Product from a *SIMPLE* Magento Product
        ///
        /// NOTE:
        ///		This function requires you prepare the product by ensuring it has:
        ///		- The first category must also be one defined in EA
        ///     - If manufacturer exists, it will be mapped to a manufacturer in EA
        ///		- If there are multiple Categories or Manufacturers, the first one will be used
        ///		- When the product is sucessfully created, an attribute (MappingCode) will be added to each Magento product
        ///		- If MULTIPLE images are provided, the BASE Magento image will be used as the hero shot
        /// </summary>
        /// <param name="magentoProduct">Magento product object</param>
        /// <returns>ProductDocumentId Identifier of created Product Document</returns>
        private static int UpsertSimpleMagentoProductToEa(ProductResource magentoProduct)
        {
            //Get fields and assets
            var fields = _fieldMapper.ParseFieldsFromProduct(magentoProduct);
            var assets = _assetMapper.ParseAssetsFromProduct(magentoProduct, ConfigReader.MagentoStorageConfiguration);

            var eaCategoryId     = _fieldMapper.GetMatchingCategory(magentoProduct.custom_attributes);
            var eaManufacturerId = _fieldMapper.GetMatchingManufacturer(magentoProduct.custom_attributes);

            var productMapping = _fieldMapper.GetProductMapping(magentoProduct);

            //Upsert product
            var productDocumentId = _productMapper.UpsertMasterProduct(productMapping, eaCategoryId, ConfigReader.EaClassificationTreeId,
                                                                       eaManufacturerId, fields, assets);

            var slug = _productMapper.CalculateSlug(productDocumentId, null);

            if (productMapping == null)
            {
                //Add to your library
                productMapping = _productMapper.AddProductToEndlessAisle(slug);

                //Set the mapping on the product so we know it is mapped
                _fieldMapper.CreateMappingForProduct(magentoProduct, productMapping);
            }

            _pricingMapper.UpsertPricingForCatalogItem(productMapping, magentoProduct.price);

            //Create availability for product at company level
            _availabilityMapper.UpsertAvailabilityForCatalogItem(productMapping, _productMapper.GetQuantityBySku(magentoProduct.sku));

            //If we have colors defined, add it as a color definition (if its not already added)
            var colorId = _colorMapper.GetMagentoColorAttribute(magentoProduct);

            if (colorId != null)
            {
                _colorMapper.UpsertColorDefinitions(productDocumentId, int.Parse(colorId));
            }

            return(productDocumentId);
        }