Magento – Bundle product in stock even though one of the simple products is out of stock

bundled-productout-of-stock

I recently discovered a problem – if I have a bundle product with 3 simple products (all set as standard) and one of them is out of stock, the whole bundle product remains in stock.
If I try to add the bundle product to cart, it just displays the remaining 2 products, and i can add it to cart, which is not ok. If one of the simple products in the bundle is out of stock, the whole bundle should be out of stock.
Same goes if 2 are missing from the 3. It just displays the one remaining and I can add it to cart.

I have looked everywhere in frontend and can't find the solution

Does anyone know what I should modify in order for this to be ok?
Thanks a lot in advance!!

Best Answer

Add logic on the product page to show add to cart button if any of simple product have qty.

if($product->getTypeId() == 'bundle')
{
    $qty = $this->getBundleProductQty($product);    
}

Add function to product block file:

public function getBundleProductQty($_product)
{
    $selectionCollection = $_product->getTypeInstance()->getSelectionsCollection($_product->getTypeInstance()->getOptionsIds());

    $qty = false;
    foreach ($selectionCollection as $option) {
        $product_id   = $option->product_id;

        $bundleOption = Mage::getModel('catalog/product')->load($product_id);
        $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($bundleOption);
        if ($qty === false) {
            $qty = $stock->getQty();
        } else {
            $qty = min(array($qty, $stock->getQty()));
        }
    }
    return $qty;
}
Related Topic