I want to instantiate CatalogInventory model in Magento 2 by calling loadByProduct() just like I used to do in Magento 1:

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);

How can I do this or similar in Magento2?


I have tried getting the stock of a product this way:

 use Magento\CatalogInventory\Api\Data\StockItemInterface;
 use Magento\CatalogInventory\Model\ResourceModel\Stock\Item;

 $model = $this->_objectManager->create('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
 $stock=$model->loadByProductId(StockItemInterface $item, $productId, $stockId);

But I get,

Parse error: syntax error, unexpected '$item' (T_VARIABLE)


Look at vendor/magento/module-catalog-inventory/Model/ResourceModel/Stock/Item.php. As we can see, we can use an alternative method loadByProductId(). Navigate to vendor/magento/magento2-base/dev/tests/api-functional/testsuite/Magento/CatalogInventory/Api/StockItemTest.php to see the testing.

        $productId = 123456;
        $stockId = 2334;
        /** @var \Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory $stockItemFactory */
        $stockItemFactory = $this->objectManager->get('Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory');
        $stockItem = $stockItemFactory->create();
        /** @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemResource */
        $stockItemResource = $this->objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
        $stockItemResource->loadByProductId($stockItem, $productId, $stockId);
| improve this answer | |
  • Can i call something like this $stock = $this->_objectManager->create('Magento\CatalogInventory\Model\ResourceModel\Stock\Item')->loadByProductId(); – Sushivam Aug 9 '16 at 8:44
  • Can i use this way as given under UPDATED ANSWER – Sushivam Aug 9 '16 at 8:45
  • Any suggestion @Khoa on my updated answer? – Sushivam Aug 9 '16 at 9:59
  • I get exception 'Exception' with message 'Notice: Undefined variable: productId . i just assigned $stock=$stockItemResource->loadByProductId($stockItem, $productId, $stockId); – Sushivam Aug 9 '16 at 10:36
  • i tried to add $productId=$stockItemFactory->create();, then i get Undefined variable: stockId, if i try to add stockId too then i get exception 'Exception' with message 'Warning: addcslashes() expects parameter 1 to be string, object given in C:\xampp\htdocs\magento2x_3\vendor\magento\zendframework1\library\Zend\Db\Adapter\Pdo\Abstract.php on line 296' in C:\xampp\htdocs\magento2x_3\lib\internal\Magento\Framework\App\ErrorHandler.php:61 – Sushivam Aug 9 '16 at 10:46

You can simple get total stock of configurable products using objectManager.

$_product = $block->getProduct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$total_stock = 0;
if($_product->getTypeID() == 'configurable'){
    $productTypeInstance = $_product->getTypeInstance();
    $usedProducts = $productTypeInstance->getUsedProducts($_product);
    foreach ($usedProducts as $simple) {
        $total_stock += $StockState->getStockQty($simple->getId(), $simple->getStore()->getWebsiteId());
echo $total_stock;


| improve this answer | |

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.