3

How to update qty and stock status using API for multiple products 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 a single SKU.

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

Thanks in advance!

4 Answers 4

3

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 the asynchronous 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 join data should contain entry_id and SKU for each product.

Updates:

I have tried the below and it is working fine for me, before trying 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

8
  • 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, 2020 at 7:54
  • it should be rest/async/bulk/V1/products/bySku/stockItems/byItemId and the item_id should exist in the request data. May 11, 2020 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, 2020 at 8:29
  • but i am getting "message": "Request does not match any route." error
    – Pawan
    May 11, 2020 at 8:30
  • one more thin in swagger i can only see get method for catalogInventoryStockRegistryV1.
    – Pawan
    May 11, 2020 at 8:35
1

You must use this endpoint.

/rest/default/V1/inventory/source-items

To find the proper endpoint, I tested lots of endpoints including async/bulk/V1/products... but it's a unique solution for multiple products.

If you need more, I will share the full code package which I used for a real store

0

Here's what worked for me:

PUT /V1/products/byProductSku/stockItems/byItemId

with data as follows:

[
    {
        "product_sku": "BLA_BLA",
        "stockItem": {
            "item_id": 1234,
            "is_in_stock": true,
            "is_qty_decimal": true,
            "qty": 1000
        }
    }
]

Notice that I have attributes product_sku and item_id in the payload.

-1

If you're using Magento 2.3.x version you should be able to use the MSI API's

ref: https://devdocs.magento.com/guides/v2.3/rest/modules/inventory/manage-source-items.html

Your Answer

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

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