Question

I have created a product, which has two different sources assigned to it with different quantity allocated to both sources. I want to update product's quantity for both sources separately.

enter image description here

I am using following api - /rest/default/V1/products/{productSku}/stockItems/{itemId} with request body as-

{
    "stock_item": {
            "qty": 2100, 
            "stock_id": "various for both sources", // 1 for 1st default source stock & 2 for my custom source stock
    }
}

API is updating product inventory on default source when given stock_id=1. But on my custom source created with stock_id=2 API returns 400 Bad Request as follows

{
    "message": "The stock item was unable to be saved. Please try again.",
    "trace": "#0 /var/www/php74/magento241/vendor/magento/module-catalog-inventory/Model/StockRegistry.php(181): Magento\\CatalogInventory\\Model\\Stock\\StockItemRepository->save()\n#1 /var/www/php74/magento241/vendor/magento/framework/Interception/Interceptor.php(58): Magento\\CatalogInventory\\Model\\StockRegistry->updateStockItemBySku()\n#2 /var/www/php74/magento241/vendor/magento/framework/Interception/Interceptor.php(138): Magento\\CatalogInventory\\Model\\StockRegistry\\Interceptor->___callParent()\n#3 /var/www/php74/magento241/vendor/magento/framework/Interception/Interceptor.php(153): Magento\\CatalogInventory\\Model\\StockRegistry\\Interceptor->Magento\\Framework\\Interception\\{closure}()\n#4 /var/www/php74/magento241/generated/code/Magento/CatalogInventory/Model/StockRegistry/Interceptor.php(59): Magento\\CatalogInventory\\Model\\StockRegistry\\Interceptor->___callPlugins()\n#5 [internal function]: Magento\\CatalogInventory\\Model\\StockRegistry\\Interceptor->updateStockItemBySku()\n#6 /var/www/php74/magento241/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(95): call_user_func_array()\n#7 /var/www/php74/magento241/vendor/magento/module-webapi/Controller/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process()\n#8 /var/www/php74/magento241/vendor/magento/framework/Interception/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch()\n#9 /var/www/php74/magento241/vendor/magento/framework/Interception/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent()\n#10 /var/www/php74/magento241/vendor/magento/framework/Interception/Interceptor.php(153): Magento\\Webapi\\Controller\\Rest\\Interceptor->Magento\\Framework\\Interception\\{closure}()\n#11 /var/www/php74/magento241/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(23): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callPlugins()\n#12 /var/www/php74/magento241/vendor/magento/framework/App/Http.php(116): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch()\n#13 /var/www/php74/magento241/vendor/magento/framework/App/Bootstrap.php(263): Magento\\Framework\\App\\Http->launch()\n#14 /var/www/php74/magento241/index.php(39): Magento\\Framework\\App\\Bootstrap->run()\n#15 {main}"
}

How can I update product inventory on my custom source?

Was it helpful?

Solution

You can update the stock in MSI like this:

  1. Bulk update products' inventory by using the POST /rest/default/async/bulk/V1/inventory/source-items (you will need your rabbitmq connection configured for this, or otherwise it won't work). The payload looks like this:
[
 {
  "sourceItems": [
    {
      "sku": "WSH12-29-Red",
      "source_code": "default",
      "quantity": 11,
      "status": 1
    },
     {
      "sku": "MH01-XS-Black",
      "source_code": "default",
      "quantity": 5,
      "status": 1
    }
   ]
  }
]

Note that the payload contains for each product the source_code key which you will have to populate with the source code of your stock source. In my example it's just the default source, feel free to replace it upon your needs.

  1. If you don't use the bulk update, you can still update the stock inventory by simply calling POST /rest/default/V1/inventory/source-items with a payload just like the one above. It's the same API endpoint being called, it's just the update mechanism that differs. First one is asynchronous and the last one is synchronous.

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top