0

I want to display Magento product qty in the popup. when the customer clicks on add to cart button the popup shows the product available in stock and if the product is not available popup displays the product not available in stock.

2
  • Hey, It is quite new functionality that You have to implement with few unknowns. How should the popup look like? With buttons OK and Cancel? Or just a popup message on the top right corner? Should the customer see the exact available QTY or just info if product is available? What about products with Backorders or not managable by Stocks (if You have any in the shop). What if a product has multiple Stock Managements / Werhauses? Do You have implemented anything so far and you are stuck? Or just want the whole solution?^^ Yo can always look for extension to install Sep 15, 2022 at 7:28
  • if customerr shuld see just info if product is available, without exact QTY, then It culd be quite easy. But it depends on shop config and some project specific changes. But in general You can use $product->isSaleable() method as default. But I guess You will need to include some specific project logic and we wont help You with that, cause noone knows that here ;) Sep 15, 2022 at 7:34

1 Answer 1

0

First, you have to get stockItem with product id. So you have to add the getStockItem function in Helper.

public function __construct(...
\Magento\CatalogInventory\Api\StockRegistryInterface $stockItemRepository,
...){
...
$this->stockItemRepository = $stockItemRepository;
...
}
public function getStockItem($productId)
    {
        return $this->stockItemRepository->getStockItem($productId);
    }

And get the Qty with product Id.

$qty = $yourHelper->getStockItem($_product->getId())->getQty();

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.