/** * This function takes a product resource and either creates a variation or updates one, if there is a mapping (UPSERT) * * @param magentoProduct Magento product to be updated * @param productDocumentId Identifier of parent * @param colorDefinitionId Identifier for a color definition, a color for this product * @param fields Fields representing changes to product details * @param assets Assets for product * * @return int Identifier of a variation in EA */ public int UpsertVariation(ProductResource magentoProduct, int productDocumentId, string colorDefinitionId, List <FieldResource> fields, List <AssetResource> assets) { //Update the variation instead of creating it, if applicable var variationId = -1; if (ProductHasMapping(magentoProduct)) { //Get identifier of variation we are updating var catalogItemId = GetAttributeByCode(magentoProduct.custom_attributes, ConfigReader.MappingCode).ToString(); var slug = _eaCatalogController.GetCatalogItem(catalogItemId).Slug; variationId = GetVariationIdFromSlug(slug); Guid?colorDefGuid; if (colorDefinitionId != null) { colorDefGuid = null; } else { colorDefGuid = new Guid(colorDefinitionId); } var variationUpdate = new RevisionEditResource { FieldValues = fields, Assets = assets, IdentifierGroups = null, ColorDefinitionId = colorDefGuid }; variationId = _eaProductController.UpdateVariation(productDocumentId, variationId, variationUpdate); } else { var variation = new VariationResource() { FieldValues = fields, Assets = assets, ColorDefinitionId = new Guid(colorDefinitionId) }; variationId = _eaProductController.CreateVariation(productDocumentId, variation).VariationId; } return(variationId); }
/** * Updates a Master Product * * @param productDocumentId Identifier for a product document * @param product Object representing Master product to be updated * * @return int Identifier of product document that was updated */ public int UpdateMasterProduct(int productDocumentId, RevisionEditResource product) { var endpoint = UrlFormatter.EndlessAisleGetProductUrl(productDocumentId); var client = new RestClient(endpoint); var request = new RestRequest(Method.PUT); request.AddHeader("Authorization", string.Format("Bearer {0}", AuthToken)); request.AddHeader("Accept", "application/json"); request.AddHeader("Content-Type", "application/json"); request.AddJsonBody(product); var response = client.Execute(request); //Ensure we get the right code CheckStatusCode(response.StatusCode, HttpStatusCode.NoContent); return(productDocumentId); }