1

I am trying to get the stock status (is_in_stock) of an item. Currently, I am using the

\Magento\CatalogInventory\Model\Stock\StockItemRepository

which only accepts stock item id to fetch the product stock item data, is there any way we can fetch the stock using the product id?
Like:

$this->stockItemRepository->loadByProductId($product_id)->getIsInStock();
2
  • You want to check that product is_in_stock or not. Right? Nov 20, 2019 at 11:40
  • that's right , I want to check the is_in_stock
    – fmsthird
    Nov 20, 2019 at 11:46

5 Answers 5

2

You need to inject \Magento\CatalogInventory\Api\StockRegistryInterface in your file :

protected $stockRepository;

public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRepository
){
    $this->stockRepository = $stockRepository;
}

Now, Add this below code in your function :

$productStock = $this->stockRepository->getStockItem($productId);
echo $productStock['is_in_stock'];
2
  • let me check this
    – fmsthird
    Nov 21, 2019 at 3:18
  • okay. Tell me if still it's not working for you. Nov 21, 2019 at 4:17
2

Below script helpful to get stock data using object manager

$productId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($productId);
print_r($productStockObj->getData());

OR

Use of this Block Class

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class HelloWorld extends Template
{    
    protected $stockItemRepository;
        
    public function __construct(
        Context $context,        
        StockItemRepository $stockItemRepository
    )
    {
        $this->stockItemRepository = $stockItemRepository;
        parent::__construct($context);
    }
    
    public function getStockItem($productId)
    {
        return $this->stockItemRepository->get($productId);
    }
}

you can call to this in phtml file :

$id = 1;
$productStock = $block->getStockItem($id);
2
  • I'm using it on a model
    – fmsthird
    Nov 21, 2019 at 3:18
  • @pradipGarchar get() function load by stock item id not product id. Go to core file Magento\CatalogInventory\Model\Stock\StockItemRepository.php and check get function Aug 2, 2021 at 16:35
1

The below method will work in Magento 2.3.4

Magento\CatalogInventory\Model\Stock\StockItemRepository

But in Magento 2.3.5 it will not work. Instead, you have to use below one

Magento\CatalogInventory\Api\StockRegistryInterface
0

Use this code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
$stockItem = $stockRegistry->getStockItem($product_id);
$IsInStock = $stockItem->getIsInStock();
if ($IsInStock) {
    echo "Is in stock";
}
0

Try this,

$this->stockItemRepository->get($product_id)->getIsInStock();

Insted of this

$this->stockItemRepository->loadByProductId($product_id)->getIsInStock();
2
  • Is it working for you? Nov 20, 2019 at 14:53
  • no, it did not work
    – fmsthird
    Nov 21, 2019 at 3:17

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.