Magento – Magento 2: REST API Add options to product

apicustom-optionsmagento2products

Issue: Cannot add options to a product via API

I cannot add an option to a product via the Magento 2 REST API.

According to the API Documentation (http://devdocs.magento.com/swagger/index_20.html#/) AND under the title "catalogProductCustomOptionRepositoryV1"

I should be able to add an option using following format:

{
"option":{
  "productSku": "SomeExistingSKU",
  "optionId": 1,
  "title": "Size",
  "type": "drop_down",
  "sortOrder": 1,
  "isRequire": true,
  "max_characters": 0,
 "image_size_x": 0,
 "image_size_y": 0,
  "values": [
    {
      "title": "5",
      "sortOrder": 0
    }
  ],
  "extensionAttributes": {}
}

When I attempt to do this in POSTMAN for example I get the following message:

"message": "No such entity." 

The product already exists in Magento with the same SKU so that is not the problem

Best Answer

Solution is to remove the element "optionId": 1, from the input json. When you pass the value for the element option, the API looks for that option id and tries to update it. Since you want to add a new option you need to remove that.

Example:

{
"option":{
  "productSku": "someexistingproduct",
  "title": "Size",
  "type": "drop_down",
  "sortOrder": 1,
  "isRequire": true,
  "max_characters": 0,
 "image_size_x": 0,
 "image_size_y": 0,
  "values": [
    {
      "title": "5",
      "sortOrder": 0
    }
  ]
}}
Related Topic