How to show configurable product stock status in magento? [closed]
-
13-12-2019 - |
Question
I want to show the stock status of child product when the configurable option is selected in the frontend.
For example product C1 has S1 ( 1 qty ) and S2 (10 qty ) children. "Only X left Threshold" is set to 5. So when user selects S1 option, it has to show the message "only 1 left", if S2 option is selected then no need to show message as its qty is more than the threshold set.
Solution
$_product
is your configurable product
.
To get all its simple use :
$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);
So you might have something like :
foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple)->getQty();
echo $simple->getName()." with size ".$simple->getSize()." have a stock of $stock";
echo '<br/>';
}
For M2
$_product->getTypeInstance(true)->getUsedProducts ( null, $_product);
So you might have something like :
foreach ($_product->getTypeInstance(true)->getUsedProducts ( null, $_product) as $simple) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
echo $simple->getName()." with size ".$simple->getSize()." have a stock of " . $StockState->getStockQty($simple->getId(), $simple->getStore()->getWebsiteId());
echo '<br/>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange