0

1 exception(s): Exception #0 (Magento\Framework\Exception\NoSuchEntityException): The stock item with the "2056" ID wasn't found. Verify the ID and try again.

-----In block File--------------
public function getLoadProduct()
{
    $product_id = $this->getCurrentProduct()->getId();
    return $this->productloader->create()->load($product_id);
}
 public function getStockItem($productId)
{
    return $this->stockItemRepository->get($productId);
}

         ------phtml file-------------
          $productLoad = $block->getLoadProduct();
          $productStockData = $block->getStockItem($productLoad->getId());
          echo $productStockData->getQty();   

2 Answers 2

0

you can use this code.

<?php
namespace Vendor\Extension\Block;

class MyCustomClass extends \Magento\Framework\View\Element\Template
{    
    protected $_stockItemRepository;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        array $data = []
    )
    {
        $this->_stockItemRepository = $stockItemRepository;
        parent::__construct($context, $data);
    }
    
    public function getStockItem($productId)
    {
        return $this->_stockItemRepository->get($productId);
    }
}
0

you can use this code

namespace Vendor\Module\Block;

class ProductGetItem extends \Magento\Framework\View\Element\Template
{    
    protected $_stockItemInterface;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\CatalogInventory\Api\StockRegistryInterface $_stockItemInterface,
        array $data = []
    )
    {
        $this->_stockItemInterface = $_stockItemInterface;
        parent::__construct($context, $data);
    }
    
    public function getStockItem($product)
    {
        if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
            $productTypeInstance = $product->getTypeInstance();
            $usedProducts = $productTypeInstance->getUsedProducts($product);
            $count = 0; 
            foreach ($usedProducts  as $child) { 
                $productQty = $this->_stockItemInterface->getStockItem($child->getId());
                round($count+=$productQty->getData('qty'));
            } 
            return $count;
        } else {
            $productQty = $this->_stockItemInterface->getStockItem($product->getId());
            return round($productQty->getData('qty'));
        }
        
    }
    public function getStockItemById($productId)
    {
        $productQty = $this->_stockItemInterface->getStockItem($productId);
        return round($productQty->getData('qty')); 
    }
}

or objectManager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$productTypeInstance = $product->getTypeInstance();
$productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');
if ($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
    $usedProducts = $productTypeInstance->getUsedProducts($product);
    $count = 0; 
    foreach ($usedProducts  as $child) { 
        $productQty = $productStockObj->getStockItem($child->getId());
        round($count+=$productQty->getData('qty'));
    } 
    echo $count;
} else {
    $productQty = $productStockObj->getStockItem($product->getId());
    echo round($productQty->getData('qty'));
}

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.