I working to Magento 2.0.2 and i trying to call the table stock_item of the product. When i worked in Magento 1.9 i used this code:


But now the class Mage is not compatible with the new version of Magento. So how i do for call this table in my .pthml file?

Thank you very much for any help.

P.S. On other question, what is the .phtml file where is called the name of the product?

2 Answers 2


If you need to show quantity on Store Front then you should use \Magento\CatalogInventory\Api\StockRegistryInterface::getStockStatus method like:

  • Thank you man but this code is not good for my magento. This code not call the table of stock quantity of my quantity. The problem is this, i created four type of quantity for my product in my table of database and i don't know as recall any single of this data for any product and showing in front end...
    – Gabriel91
    Commented Feb 8, 2016 at 8:29
  • In magento 1.9 existed the Class Mage for this but now? Thanks again...
    – Gabriel91
    Commented Feb 8, 2016 at 9:00

Perform the below steps to get the stock item in your phtml file.

  1. Instantiate \Magento\CatalogInventory\Model\Stock\StockItemRepository class
  2. Call get method with product id.
  3. Call the qty functions as per the requirement in your phtml file.
public function getStockItem($productId)
   return $this->_stockItemRepository->get($productId);
$productStock = $block->getStockItem($id);
echo $productStock->getQty().'<br />';
echo $productStock->getMinQty().'<br />';
echo $productStock->getMinSaleQty().'<br />';
echo $productStock->getMaxSaleQty().'<br />';
echo $productStock->getIsInStock().'<br />';

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.