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?

Bogz
  • 565
  • 1
  • 10
  • 30

5 Answers5

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
                }
            }
Amit Bera
  • 7,581
  • 7
  • 31
  • 57
  • 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 product – Bogz Aug 15 '14 at 10:45
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.

Morgy
  • 99
  • 9
Quovadisqc
  • 81
  • 7
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.

Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
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.

Nemery
  • 399
  • 2
  • 11
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';
}
Matt Doran
  • 2,050
  • 2
  • 16
  • 18