2

I want to check is "Manage Stock" is set as Yes or No with specific product. Can anyone help us to get value of product "Manage Stock" attribute value.

3

Please place below code in any block file.

namespace Meetanshi\HelloWorld\Block;
class HelloWorld 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);
    }
}
?>

You can call getStockItem(YOUR_PRODUCT_ID) by passing product Id as parameter.

If you are placing above code in block than do it as below.

$id = YOUR_PRODUCT_ID;
$productStock = $block->getStockItem($id);
var_dump($productStock->getData());

You will get manage stock value of passing product.

Thanks

|improve this answer|||||
  • You are most welcome :) – Meetanshi Nov 18 '17 at 10:35
2

You can get Manage Stock attribute value using stock registry.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');

$stockItem=$stockRegistry->getStockItem($_product->getId());

echo $stockItem->getData('manage_stock');

echo $stockItem->getData('use_config_manage_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.