2

According to the swagger, the REST API PUT /V1/products/{productSku}/stockItems/{itemId} is used to update the product inventory.

However, for the attribute value itemId, I found no differences no matter I entered which values. So, the following request URLs can update the same product without any errors AND return the same integer value:

http://<magento_url>/rest/V1/products/{productSku}/stockItems/1
http://<magento_url>/rest/V1/products/{productSku}/stockItems/{correctItemId}
http://<magento_url>/rest/V1/products/{productSku}/stockItems/999999

Given that the {productSku} is a simple product, my question is: What is the exact usage of itemId in the request URL?

1 Answer 1

1

If you see the method declaration in StockRegistryInterface.php

/**
 * @param string $productSku
 * @param \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem
 * @return int
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function updateStockItemBySku($productSku, \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem);

With help of this itemId magento instantiates the object of

 \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem

then passing this object as parameter in updateStockItemBySku() method.

If we provide wrong ItemId, the inventory will be updated it default website only. If we look at the method updateStockItemBySku() in Magento\CatalogInventory\Model\StockRegistry, the code

$websiteId = $stockItem->getWebsiteId() ?: null;

So, itemId make significance to define the scope of website to update inventory.

2
  • When will the itemId make difference on the result?
    – PY Yick
    Commented May 16, 2018 at 10:52
  • 1
    Only to define the scope of website of the product. Please see the updated answer. Commented May 16, 2018 at 12:08

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.