14

How can I update the custom attributes such as description? The following code properly update price and product name, but not custom attributes (unchanged). The new product is created properly with custom attributes, the problem is while updating.

magento2 REST Update custom attributes

magento2 REST Update custom attributes

3
  • Hi guys, in your case,I think you need do again: php bin/magento upgrade
    – MrTo-Kane
    Dec 25 '16 at 17:56
  • Hi guys, I think you give us the code or config of attribute for custom_attributes. To we check again.
    – MrTo-Kane
    Dec 26 '16 at 2:18
  • Have you check any answer? need your comments. Dec 26 '16 at 9:05
16
+50

To create product, we can use:

  • POST: /V1/products

For updating:

  • PUT: /V1/products/{sku}

In your case, your code should be:

$productData = [
        'attribute_set_id'  => 4,
        "type_id": "simple",
        "sku": "test-SKU",
        "name": "Test",
        "price": 100,
        "status": 1,
        'custom_attributes' => [
                ['attribute_code' => 'description', 'value' => 'Test Description' ],
                ['attribute_code' => 'short_description', 'value' => 'Test Short Description' ],
            ]
    ];

The JSON body:

 {
  "product": {
    "sku": "test-SKU",
    "name": "Test",
    "attribute_set_id": 4,
    "price": 100,
    "status": 1,
    "custom_attributes": [
      {
        "attribute_code": "description",
        "value": "Test Description"
      },
      {
        "attribute_code": "short_description",
        "value": "Test Short Description"
      }
    ]
  }
}

There is an answer here: https://magento.stackexchange.com/a/135607/33057

6
  • Hi , @Khoa you need to format array to json string, because he used REST and by REST tool.
    – MrTo-Kane
    Dec 25 '16 at 17:54
  • @MagentoOdoo.com Thanks! I updated my answer. Dec 25 '16 at 18:02
  • @Khoa TruongDinh How to add selected attributes label in product rest API. Like ['attribute_code' => 'description', 'value' => 'Test Description', 'label' => 'Description' ], Sep 5 '19 at 5:50
  • @KhoaTruongDinh How to pass label in /V1/products/{sku} 'attribute_code' => 'color', 'value' => '54', 'label' => 'Blue' Let me know if you have idea. Your help would be appreciated. Sep 5 '19 at 13:33
3

Try to pass your custom attributes within array like:

'custom_attributes' => array(
     '0' => array(
          'attribute_code' => 'my_custom_attribute_code',
          'value' => 'my_custom_attribute_value'
      )
      ...
)
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.