0

I was using the postman to send post request. I have a single json payload send to API endpoint, which is success

rest/default/V1/products/

{
  "product": {
    "sku": "203-197-1022",
    "custom_attributes": [
      {
        "attribute_code": "description2",
        "value": "CHECK CHECK 555"
      }
    ]
  }
}

But if I try to add multiple items in the payload, it doesn't work. It just only update the last SKU. Can anyone help to check what's wrong with my json?

    {
  "product": {
    "sku": "203-197-1022",
    "custom_attributes": [
      {
        "attribute_code": "description2",
        "value": "CHECK CHECK 555"
      }
    ]
  },
  "product": {
    "sku": "728-207-1022",
    "custom_attributes": [
      {
        "attribute_code": "description2",
        "value": "CHECK CHECK 555"
      }
    ]
  } 
}

1 Answer 1

1

The issue you're encountering is due to the JSON structure you're using to update multiple products. In the current format, you have multiple objects with the same key ("product"), which means the last one will overwrite the previous ones.

To update multiple products in a single request, you should use an array of products instead. Here's an example of how your JSON payload should look:

{
  "products": [
    {
      "product": {
        "sku": "203-197-1022",
        "custom_attributes": [
          {
            "attribute_code": "description2",
            "value": "CHECK CHECK 555"
          }
        ]
      }
    },
    {
      "product": {
        "sku": "728-207-1022",
        "custom_attributes": [
          {
            "attribute_code": "description2",
            "value": "CHECK CHECK 555"
          }
        ]
      }
    }
  ]
}

However, the default Magento 2 API doesn't support bulk updates for products out-of-the-box. You can either create a custom API endpoint to handle this JSON structure or make individual API requests for each product update.

Creating a custom API endpoint would involve:

Creating a custom Magento 2 module. Defining a new API endpoint in the webapi.xml file. Implementing the logic to process the bulk update in your custom module's class. If you prefer to make individual API requests for each product update, you can continue using the same JSON structure you have used for single products, but you'll need to send separate requests for each product.

EXMAPLE OF A BULK UPDATE

you can use the Magento 2 REST API for a bulk update of products, including custom attributes, by using the async/bulk/V1/products endpoint. This endpoint allows you to update multiple products in a single API call. To use this endpoint, you need to send a PUT request with the appropriate JSON payload containing the product data you want to update.

Here's an example of the JSON format to update custom attributes for multiple products:

[
  {
    "sku": "product1-sku",
    "product": {
      "custom_attributes": [
        {
          "attribute_code": "custom_attribute1",
          "value": "updated value for custom_attribute1"
        },
        {
          "attribute_code": "custom_attribute2",
          "value": "updated value for custom_attribute2"
        }
      ]
    }
  },
  {
    "sku": "product2-sku",
    "product": {
      "custom_attributes": [
        {
          "attribute_code": "custom_attribute1",
          "value": "updated value for custom_attribute1"
        },
        {
          "attribute_code": "custom_attribute2",
          "value": "updated value for custom_attribute2"
        }
      ]
    }
  }
]

Replace product1-sku and product2-sku with the actual SKU of the products you want to update, and use the appropriate attribute_code and value for the custom attributes you want to update.

To make a PUT request to the bulk endpoint, you can use a tool like cURL or Postman. If you're using cURL, the command would look like this:

curl -X PUT "https://your-magento-site.com/rest/async/bulk/V1/products" \
  -H "Authorization: Bearer your-access-token" \
  -H "Content-Type: application/json" \
  -d 'your-json-payload'

Replace your-magento-site.com with your actual Magento site URL, your-access-token with your Magento access token, and your-json-payload with the JSON data you want to send.

Keep in mind that this is an asynchronous request, meaning that the response will include a bulk UUID that you can use to track the status of your request. You'll need to poll the /async/bulk/:bulkUuid endpoint to get the status and results of your bulk update request.

6
  • Thanks for your details explanation. I don't know why bulk update is not available, I thought this is something very common and widely use.
    – Paul Fan
    Commented Apr 25, 2023 at 22:40
  • Hello can I use this endpoint to do the bulk update? async/bulk/V1/products/
    – Paul Fan
    Commented Apr 26, 2023 at 22:09
  • @PaulFan I added on to my answer with the bulk update Commented Apr 27, 2023 at 17:24
  • Thankyou so much!
    – Paul Fan
    Commented Apr 28, 2023 at 23:05
  • 1
    @PaulFan I am only speculating but I would think it is related to the amount of resources you have to process. Commented Apr 30, 2023 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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