3

I am using Magento 2.3.1

I want to display stock status on the listing page. So, how can I get "Stock Status" attribute value on the listing page?

I have tried below code:

<?php 
 $_productCollection = $block->getLoadedProductCollection();
 foreach ($_productCollection as $_product){

    $stock_status = $_product->getResource()->getAttribute('quantity_and_stock_status')->getFrontend()->getValue($_product);

 } 
?>

And tried below as well:

<?php 
     $_productCollection = $block->getLoadedProductCollection();
     foreach ($_productCollection as $_product){

        $default_stock_status = $_product->getData('quantity_and_stock_status');

     } 
?>

But for each scenario, I got "Out of stock" even though the product is in stock here quantity_and_stock_status is the default "Stock Status" attribute.

enter image description here

4 Answers 4

10

You can try the following code

public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->stockRegistry = $stockRegistry;
}
// you can pass $product object to the function
public function getStockStatus($product)
{
    return $this->stockRegistry->getStockItem($product->getId());
}

You can get all the stock details by

$stockItem = $this->stockRegistry->getStockItem($product->getId());
$stockData = $stockItem->getData();
$stockItem->getIsInStock();

Hope it helps.

4
  • I tried this as well and it only gives the product quantity but I need to display the stock status values e.g. In Stock, Out of Stock. Aug 2, 2019 at 7:09
  • you can use getIsInStock() method
    – Mohit Rane
    Aug 2, 2019 at 7:14
  • to get all the stock details you can use this class Magento\CatalogInventory\Api\StockRegistryInterface
    – Mohit Rane
    Aug 2, 2019 at 7:16
  • check updated answer
    – Mohit Rane
    Aug 2, 2019 at 7:46
9

If you are using the only simple products in your system then using the Stock Status field check then Product status is Ok.

But when you have a configurable or bundle or group product then it is not a good idea to use the Stock Status field to check stock status. Because of these types of product's stock status, it depends on its child Products status.

That is this, use of $_product->isAvailable() and $_product->isSaleable() is the best idea.See at https://github.com/devamitbera/magento2/blob/2.3-develop/app/code/Magento/Catalog/view/frontend/templates/product/list.phtml#L80

3
  • correct! need to check for $_product->isSaleable() Aug 2, 2019 at 9:40
  • @Amit What exactly do $_product->isAvailable() mean? is it says that products is enabled or disabled? Aug 3, 2019 at 3:30
  • use the magento 2 custom stock status of mageants it will help to your store
    – nikunj
    Jun 2, 2021 at 11:32
4

Inject \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository, in constructor

e.g:

protected $_stockItemRepository;

public function __construct(
    \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
)
{
    $this->_stockItemRepository = $stockItemRepository;
}

Use function to get stock data using product id

public function getStockItem($productId)
    {
        return $this->_stockItemRepository->get($productId);
    }

Then you may call in your phtml file

$id = YOUR_PRODUCT_ID;
$productStock = $block->getStockItem(1225);
echo $productStock->getQty(); echo '<br />';
echo $productStock->getMinQty(); echo '<br />';
echo $productStock->getMinSaleQty(); echo '<br />';
echo $productStock->getMaxSaleQty(); echo '<br />';
echo $productStock->getIsInStock(); echo '<br />';

Source: http://blog.chapagain.com.np/magento-2-get-product-stock-quantity-and-other-stock-information/

3

Note:- Object Manager is not recommended way.

You can inject class in your constructor this way

<?php 
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $_productCollection = $block->getLoadedProductCollection();

     foreach ($_productCollection as $_product){

        $StockState = $objectManager->get('\Magento\CatalogInventory\Model\Stock\StockItemRepository');

        $StockState->get($_product->getId());
        echo $StockState->getIsInStock();

     } 
?>

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.