16

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
    Commented Dec 25, 2016 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
    Commented Dec 26, 2016 at 2:18
  • Have you check any answer? need your comments. Commented Dec 26, 2016 at 9:05

2 Answers 2

20
+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
    Commented Dec 25, 2016 at 17:54
  • @MagentoOdoo.com Thanks! I updated my answer. Commented Dec 25, 2016 at 18:02
  • @Khoa TruongDinh How to add selected attributes label in product rest API. Like ['attribute_code' => 'description', 'value' => 'Test Description', 'label' => 'Description' ], Commented Sep 5, 2019 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. Commented Sep 5, 2019 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.