0

I tried using isSaleable(), getSaleableItem(), is_in_stock, without success.

I'm new to Magento and I don't have much knowledge.

5

1 Answer 1

1

Try this in final_price.phtml file

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

and you can add condition like this

if ($productStockObj->getIsInStock())

Reference Link

9
  • It didn't work, it returns the value empty Sep 21 at 12:25
  • <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $productStockObj = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($productId); ?> <?php if ($productStockObj->getIsInStock()) : ?> OK <?php else : ?> NOK <?php endif; ?> Sep 21 at 12:27
  • I tested in magento2.4.6 and it's working for me. are you using a custom theme or module? Sep 21 at 12:50
  • 1
    app/design/frontend/{Theme}/{name}/Magento_Catalog/templates/product/price/final_price.phtml in this location can you please add code that I added the link in the answer and check if it's working or not? Sep 21 at 13:07
  • 1
    print the data and check print_r($productStockObj) Sep 21 at 13:09

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.