1

How to update qty and stock status using API for multiple product in Magento 2 ?

We are doing ERP integration with magento 2 and we need to update qty and stock status.

in magento 1 we had catalogInventoryStockItemUpdate Soap API for update.

I have search in magento 2 API and found below:

catalogInventoryStockRegistryV1
PUT /V1/products/{productSku}/stockItems/{itemId}


catalogProductRepositoryV1
PUT /V1/products/{sku} 

But they only work for single SKU.

What is the equivalent of catalogInventoryStockItemUpdate in magento 2 API, Where we can pass array of SKU for update qty and stock

Thanks in advance!

1

Try this bulk endpoints

Bulk API endpoints differ from other REST endpoints in that they combine multiple calls of the same type into an array and execute them as a single request. The endpoint handler splits the array into individual entities and writes them as separate messages to the message queue.

To call a bulk endpoint, add the prefix /async/bulk before the /V1 of a synchronous endpoint route. For example:

POST /async/bulk/V1/products
POST /async/bulk/V1/customers

Endpoint routes that contain input parameters require additional changes. For example, PUT /V1/products/:sku/media/:entryId contains the :sku and :entryId input parameters. The route of a bulk request cannot contain input parameters, so you must change the route so that it does not contain any. To do this, replace the colon (:) with by and change the first letter of the parameter to uppercase.

PUT /V1/products/:sku/media/:entryId -> PUT async/bulk/V1/products/bySku/media/byEntryId

it means your request josn data should contain entry_id and sku for each product.

Updates:

I have tried the below and it is working fine for me, before try the below you should install rabbitmq and add queue configuration in app/etc/env.php

e.g) env.php

'queue' => [
'amqp' => [
  'host' => '127.0.0.1',
  'port' => '5672',
  'user' => 'bilal',
  'password' => 'password',
  'virtualhost' => '/'
 ],
]

PUT http://127.0.0.1/m234/rest/all/async/bulk/V1/products/bySku

Request data:

[
{
  "product": {
    "sku": "sku1",
    "name": "Product1",
    "attribute_set_id": 4,
    "status": 1,
    "visibility": 4,
    "price":10,
    "type_id": "simple",
    "extension_attributes": {
      "stock_item": {
      "item_id": 3072,
      "product_id": 3073,
      "stock_id": 1,
      "qty": 20,
      "is_in_stock": true
      }
    },
    "custom_attributes": [
      {
        "attribute_code": "part_number",
        "value": "1234"
      }
    ]    
  }
},
{
  "product": {
    "sku": "3483",
    "name": "Product2",
    "attribute_set_id": 4,
    "status": 1,
    "visibility": 4,
    "price":20,
    "type_id": "simple",
    "extension_attributes": {
      "stock_item": {
        "item_id": 3071,
        "product_id": 3072,
        "stock_id": 1,
        "qty": 10,
        "is_in_stock": true
      }
    },
    "custom_attributes": [
      {
        "attribute_code": "part_number",
        "value": "1234"
      }
    ]
  }
}
]

Response

{
    "bulk_uuid": "eda7899f-d00b-4d0a-b6f9-77fea3296f4d",
    "request_items": [
        {
            "id": 0,
            "data_hash": "986765adeb0b00b03997159b84b627f5c5eda5e281b52dc34d61446cc496ebf3",
            "status": "accepted"
        },
        {
            "id": 1,
            "data_hash": "3ae299b09bd30686772ee24214b482d88776f49f68ce804b1e78dfc4c1d19802",
            "status": "accepted"
        }
    ],
    "errors": false
}

If cron configured, it will automatically trigger otherwise you need to run the below command manually to trigger the queue

php bin/magento queue:consumers:start async.operations.all

You can check this queue status in Magento admin -> System -> Bulk Action Log with the above response bulk_uuid (eda7899f-d00b-4d0a-b6f9-77fea3296f4d) Easy for your reference: https://nimb.ws/qnA6oY

| improve this answer | |
  • Thank you for your answer. that means rest/V1/products/001-main/stockItems/1 will be rest/async/bulk/V1/products/bySku/stockItems/byEntryId for bulk update ? – Pawan May 11 at 7:54
  • it should be rest/async/bulk/V1/products/bySku/stockItems/byItemId and the item_id should exist in the request data. – Bilal Usean May 11 at 8:09
  • I change as per your comment and pass parameter as `[ {"stockItem":{ "sku":"001-main", "qty":500, "item_id":1 } }, {"stockItem":{ "sku":"BARLIQUORTESTSHOT", "qty":500, "item_id":1 } } ]' – Pawan May 11 at 8:29
  • but i am getting "message": "Request does not match any route." error – Pawan May 11 at 8:30
  • one more thin in swagger i can only see get method for catalogInventoryStockRegistryV1. – Pawan May 11 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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