Skip to content

Display Stock on product widget

I am using magento 2.4.6 with martfury theme. I want to display stock level on product that’s displayed via widget. Also, I have MSI enabled for the store.
I am using the below code to display the stock level, but it throws me error. can you please suggest how to do it. I am a beginner in magento.

$ObjectManager = MagentoFrameworkAppObjectManager::getInstance();
// $StockState = $ObjectManager->get(‘MagentoCatalogInventoryApiStockStateInterface’);
echo $StockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());

enter image description here