How to check if Configurable Product is out of stock?
Asked Answered
G

5

6

We all know that a configurable product in magento is associated with simple product.

If the simple products associated to the configurable product becomes Inventory = 0, it means that the configurable product is out of stock

So the question is how do i detect if Configurable Product is out of stock? i want to detect so I can display in front-end the "Out of Stock" text.

something like this

if($configurable_product->isOutOfStock()) {
   echo "Out of Stock";
}

How can i do this in Magento?

Geyserite answered 15/8, 2014 at 8:2 Comment(0)
M
8
if (!$configurable->isSaleable() ||$configurable_product->getIsInStock()==0){
// out of stock
}

For checking child simple product:

$allProducts = $configurable->getTypeInstance(true)
                ->getUsedProducts(null, $configurable);
            foreach ($allProducts as $product) {
                if (!$product->isSaleable()|| $product->getIsInStock()==0) {
                    //out of stock for check child simple product
                }
            }
Maintenon answered 15/8, 2014 at 8:45 Comment(1)
your first code doesn't work for me. however your second code works so i used even if its slower since i need to loop all products associated with the configurable productGeyserite
T
0
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
$qty = $stockItem->getData('qty');
$inStock = $stockItem->getData('is_in_stock');

if ($qty < 1 || $inStock == 0) {
    // OutOfStock
}

I prefer to double check with qty since products won't always be out of stock on qty == 0 depending on config settings.

Thermoscope answered 15/8, 2014 at 15:43 Comment(0)
H
0
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('type_id', array('eq' => 'configurable'));

Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($_productCollection);

This shows only the configurable products that are in stock.

Helton answered 1/5, 2015 at 7:30 Comment(0)
B
0

Just a slight update/correction to Quovadisqc's answer. When defining $qty it should be

$qty = $stockItem->getData('qty'); // correct

Instead of what's currently there,

$qty = $stockItem->setData('qty'); // incorrect

I'd post this as a comment but I don't have enough rep.

Bomber answered 24/7, 2015 at 14:19 Comment(0)
G
0

In the foreach loop of products the following if statement works.

if ($product->getIsInStock() === '1' && $product->isSaleable() === true) {
    echo 'this product is in stock';
}
Grocery answered 20/8, 2015 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.