1

I'm developing an application to integrate with the Magento inventory API, and I'm confused about the fields product_id and item_id.

I'm PUT-ing to the API /V1/products/{productSku}/stockItems/{itemId}

The body of the request looks like

{
  "stockItem": {
    "item_id": 1,
    "product_id": 1,
    "stock_id": 1,
    "qty": 15
  }
}

However, I can leave off the product_id and/or the item_id, and the request still seems to work correctly, and updates the inventory.

{
  "stockItem": {
    "item_id": 1,
    "stock_id": 1,
    "qty": 20
  }
}

{
  "stockItem": {
    "stock_id": 1,
    "qty": 25
  }
}

Both of the above requests also update the product's inventory. So my questions are

  • What is the difference between product_id and item_id. I know that product_id is used as the primary key in the products table, so then what does item_id do?
  • Why are product_id and item_id both parameters in this request (and shown in Magento's documentation) if neither one of them appears to be required?
1

The body parameters come from the used interface Magento\CatalogInventory\Api\Data\StockItemInterface. That does not mean that all this parameters have any impact on every request. The method triggered by the API, updateStockItemBySku in Magento\CatalogInventory\Model\StockRegistry, uses them as follows:

  • If you update the stock for a given product sku (i.e. that product has already a corresponding stock item), the item_id is ignored.
  • The parameter product_id of stock item is always ignored, the used product_id is always the one found in the product entity associated to the given {productSku} path paramer.

Regarding the difference between item_id and product_id: The first one is the id of the stock item, the primary key of the table cataloginventory_stock_item, the product_id is the entity_id in the table catalog_product_entity of the product associated to this stock item.

But as you have correctly observed none of them really have an impact if you update stock.

I hope that makes the procedure a little clearer :-)

There is only one thing I don't understand in the process: What is the sense of the requiered {itemId} path parameter? This is not even passed to the method as far as I can see.

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.