1

In magento 2 how to get product quantity and minimum quantity value customly created modules?

2 Answers 2

1

Inject the StockRegistryInterface interface in your custom block:

 /**
 * @var \Magento\CatalogInventory\Api\StockRegistryInterface
 */
private $stockRegistry;

/**
 * Constructor for DI.
 *
 * @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
 */
public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->stockRegistry = $stockRegistry;
}

/**
 * Get the product stock data and methods.
 *
 * @return \Magento\CatalogInventory\Api\StockRegistryInterface
 */

public function getStockRegistry()
{
    return $this->stockRegistry;
}

Use below code to get the stock details like product qty, availability etc:

$stockRegistry = $block->getStockRegistry();

/** @var \Magento\Catalog\Model\Product $product */
$product = your product object..

// Get stock data for given product.
$productStock = $stockRegistry->getStockItem($product->getId());

// Get quantity of product.
$productQty = $productStock->getQty();

You also asked for minimum qty, do you want to get minimum allowed qty in cart or you mean it to other qty?

0

You get data of stock quantity:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.