Magento – Magento 2 : How to update qty and stock status using API for multiple products

apimagento2magento2.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!

Best Answer

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

Related Topic