/**
* @inheritdoc
*/
public function get($stockItemId)
{
$stockItem = $this->stockItemFactory->create();
$this->resource->load($stockItem, $stockItemId);
if (!$stockItem->getItemId()) {
throw new NoSuchEntityException(__('Stock Item with id "%1" does not exist.', $stockItemId));
}
return $stockItem;
}
/**
* @inheritdoc
*/
public function get($stockItemId)
{
$stockItem = $this->stockItemFactory->create();
$this->resource->load($stockItem, $stockItemId, 'product_id');
if (!$stockItem->getItemId()) {
throw new NoSuchEntityException(__('Stock Item with product_id "%1" does not exist.', $stockItemId));
}
return $stockItem;
}
Magento
2.1.3
Hi,
It's not a bug because, in general, the get method of a repository should work with entity id or any other unique identifier. However, by design, there is possible to have several stock items with the same product id now.
You're looking for \Magento\CatalogInventory\Api\StockRegistryInterface::getStockItem which is working exactly how you expect.
More details: https://github.com/magento/magento2/issues/7057
I am having magento 2.3.3 and had this same issue. In the default mode, the add to cart button and quantity was not appearing..
Putting the site on developer mode showed the exception Stock Item with id "51235" does not exist.
Really the stock item with that id does not exists. But the stock item for the product has a different stock item id.. so apparently magento is looking for the wrong stock item. So magento needs to find a way to load stock item when the stock item id is not the same as the product id.
Here is a screenshot of my database.


For the first part of the stock_item table, product ids were the same as stock item ids.. but later that was not the case again...
And for new products created now stock item id is not the same as product id. Hence the stock item is not being loaded...
@igrybkov You said "the get method of a repository should work with entity id or any other unique identifier." Yes. But in this case the stock item entity_id is not the same as the product id.. So cases like this too should be considered
@Myced, from what I see in the issue now, the question was that StockItemRepository returns stock item by stock item id and the author of the issue (@djbokkeman) was expecting it to return stock item by product id.
My answer was that the behavior of the repository is correct. This is because the stock item has a primary key (stock item id) and the repository operates with the primary key, which is the usual practice. The primary key is not expected to match product identifier and exists on its own.
And there's another interface that allows to get a stock item by product id without knowing stock item id: \Magento\CatalogInventory\Api\StockRegistryInterface::getStockItem.
I don't know what you want to disagree with.
If it's your custom code that gets the wrong or missing stock item by product id — you should not use StockItemRepository and use \Magento\CatalogInventory\Api\StockRegistryInterface::getStockItem to get stock item by product id.
If it's a bug in Magento where in some case it tries to get stock item by product id using StockItemRepository — the place that uses StockItemRepository should use StockRegistryInterface (or whatever is recommended in MSI now) instead. Please create a new GitHub issue describing how to reproduce this specific behavior on the pure Magento and/or a pull request to fix it.
If it's a bug in 3rd party extension incorrectly using the repository instead of the registry — you could patch it on your own and send a bug report to the extension developer.
I see.. But in my case its not a custom module I am writing.. And from the stack trace I see no custom module trying to get the stock item. So I think its a magento bug.
`1 exception(s):
Exception #0 (Magento\Framework\Exception\NoSuchEntityException): The stock item with the "20751" ID wasn't found. Verify the ID and try again.
Exception #0 (Magento\Framework\Exception\NoSuchEntityException): The stock item with the "20751" ID wasn't found. Verify the ID and try again.
#1 include() called at [vendor/magento/framework/View/TemplateEngine/Php.php:59]`2 Magento\FrameworkView\TemplateEngine\Php->render() called at [vendor/magento/framework/View/Element/Template.php:271]
3 Magento\FrameworkView\Element\Template->fetchView() called at [generated/code/Magento/Catalog/Block/Product/View/Type/Simple/Interceptor.php:414]
4 Magento\Catalog\Block\ProductView\Type\Simple\Interceptor->fetchView() called at [vendor/magento/framework/View/Element/Template.php:301]
5 Magento\FrameworkView\Element\Template->_toHtml() called at [vendor/magento/framework/View/Element/AbstractBlock.php:1097]
6 Magento\FrameworkView\Element\AbstractBlock->Magento\FrameworkView\Element{closure}() called at [vendor/magento/framework/View/Element/AbstractBlock.php:1101]
7 Magento\FrameworkView\Element\AbstractBlock->_loadCache() called at [vendor/magento/framework/View/Element/AbstractBlock.php:671]
8 Magento\FrameworkView\Element\AbstractBlock->toHtml() called at [generated/code/Magento/Catalog/Block/Product/View/Type/Simple/Interceptor.php:739]
9 Magento\Catalog\Block\ProductView\Type\Simple\Interceptor->toHtml() called at [vendor/magento/framework/View/Layout.php:557]
10 Magento\FrameworkView\Layout->_renderBlock() called at [vendor/magento/framework/View/Layout.php:533]
11 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
12 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
13 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
14 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
15 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
16 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
17 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
18 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
19 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
20 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
21 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
22 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
23 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
24 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
25 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
26 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
27 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
28 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
29 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
30 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
31 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
32 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
33 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
34 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
35 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
36 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
37 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
38 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
39 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
40 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
41 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
42 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
43 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
44 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
45 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
46 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
47 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
48 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
49 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
50 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
51 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
52 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
53 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
54 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
55 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
56 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
57 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
58 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
59 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
60 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
61 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
62 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
63 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
64 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
65 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
66 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
67 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
68 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
69 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
70 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
71 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
72 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
73 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
74 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:585]
75 Magento\FrameworkView\Layout->_renderContainer() called at [vendor/magento/framework/View/Layout.php:535]
76 Magento\FrameworkView\Layout->renderNonCachedElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:206]
77 Magento\FrameworkView\Layout\Interceptor->renderNonCachedElement() called at [vendor/magento/framework/View/Layout.php:488]
78 Magento\FrameworkView\Layout->renderElement() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:193]
79 Magento\FrameworkView\Layout\Interceptor->renderElement() called at [vendor/magento/framework/View/Layout.php:954]
80 Magento\FrameworkView\Layout->getOutput() called at [vendor/magento/framework/Interception/Interceptor.php:58]
81 Magento\FrameworkView\Layout\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
82 Magento\FrameworkView\Layout\Interceptor->Magento\Framework\Interception{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
83 Magento\FrameworkView\Layout\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/View/Layout/Interceptor.php:494]
84 Magento\FrameworkView\Layout\Interceptor->getOutput() called at [vendor/magento/framework/View/Result/Page.php:258]
85 Magento\FrameworkView\Result\Page->render() called at [vendor/magento/framework/View/Result/Layout.php:171]
86 Magento\FrameworkView\Result\Layout->renderResult() called at [vendor/magento/framework/Interception/Interceptor.php:58]
87 Magento\FrameworkView\Result\Page\Interceptor->___callParent() called at [vendor/magento/framework/Interception/Interceptor.php:138]
88 Magento\FrameworkView\Result\Page\Interceptor->Magento\Framework\Interception{closure}() called at [app/code/Potato/Compressor/Plugin/ControllerRenderResultAfter.php:65]
89 Potato\Compressor\Plugin\ControllerRenderResultAfter->aroundRenderResult() called at [vendor/magento/framework/Interception/Interceptor.php:135]
90 Magento\FrameworkView\Result\Page\Interceptor->Magento\Framework\Interception{closure}() called at [vendor/magento/framework/Interception/Interceptor.php:153]
91 Magento\FrameworkView\Result\Page\Interceptor->___callPlugins() called at [generated/code/Magento/Framework/View/Result/Page/Interceptor.php:130]
92 Magento\FrameworkView\Result\Page\Interceptor->renderResult() called at [vendor/magento/framework/App/Http.php:141]
93 Magento\Framework\App\Http->launch() called at [generated/code/Magento/Framework/App/Http/Interceptor.php:24]
94 Magento\Framework\App\Http\Interceptor->launch() called at [vendor/magento/framework/App/Bootstrap.php:261]
95 Magento\Framework\App\Bootstrap->run() called at [index.php:39]
@Myced, the only thing I see in your stack trace is that it happens somewhere in the view layer.
This line:
#1 include() called at [vendor/magento/framework/View/TemplateEngine/Php.php:59]
specifically says that there was a template included that caused the issue. Check your templates first and also check that line with the debugger to see which template causes the issue for you.
So the only thing I know for sure now — it's somephtml template where the exception happens.
And again, if you'd be able to prove that the error is caused by the code in Magento and not in a custom extension — please create a new issue because this one will remain closed.
I'm locking this conversation now to move unrelated discussions out of this issue.
Most helpful comment
Hi,
It's not a bug because, in general, the
getmethod of a repository should work with entity id or any other unique identifier. However, by design, there is possible to have several stock items with the same product id now.You're looking for
\Magento\CatalogInventory\Api\StockRegistryInterface::getStockItemwhich is working exactly how you expect.