Magento – How to display bundled products in category list when simple product is out of stock

catalogconfigurationmagento-1

I'm trying to display the bundled product in the category product list on the frontend even when the simple product it concists of is out of stock. I only want to hide the add to cart button.

I've looked into changing the Mage_Bundle_Model_Product_Type class to return true on the stock check but that is not really a solution.

Is there a way to do this in a clean way either trough settings or by extending as little core code as possible?

Best Answer

You could create some method in one of the helpers

public function isBundleAvailable($_product) {
    $bundle = Mage::getModel('catalog/product')->load($_product->getId());
    $selectionCollection = $bundle->getTypeInstance(true)->getSelectionsCollection(
        $bundle->getTypeInstance(true)->getOptionsIds($bundle), $bundle
    );
    $items = array();

    foreach($selectionCollection as $option) {        
        $isAvailable = $option->getStockItem()->getIsInStock();
        if (!$isAvailable) {
            return false;
        }
    }
     return true;
}

Then in template you could use something like that:

<?php if (Mage::helper('my_module')->isBundleAvailable($_product)): ?>
    <?php //display add to cart button
<?php endif ?>