0

I have the following that shows a bundle of products on the product page:

if ($_product->getAttributeText('bundle') == yes && $simplecount > 1):
$product_ids = explode(',',$_product->get_bundle_products());

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('entity_id', array('in' => $product_ids));

echo'<div class="bun2c" style="text-align:center; float:right;">';
foreach($collection as $eachproduct){
echo '<a href="' . $eachproduct->getProductUrl() . '"><img class="bundle" src="' . $eachproduct->getImageUrl() . '" title="' . $eachproduct->getName() . '"></a>';
$productId = $eachproduct->getId();
$simplecount = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getQty();
}

This works fine but I need not to show this when one of the bundle products are out of stock.

I have tried with item quatity($simplecount) but does not work.

Ive used Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getIsInStock() but this only works when every item is out of stock

1

I managed to get what I wanted and here is what I did,

First I calculated the items that were in stock:

$fullstock = 0;
$inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getIsInStock();
$fullstock += $inStock;

I then got the product count of the bundle:

$product_count = count($collection);

I then used $fullstock == $product_count in my if statement

The complete code is as follows

$ids = array();
$quote = Mage::getSingleton('checkout/session')->getQuote();
$fullstock = 0;
    foreach ($quote->getAllItems() as $item) {
        $ids[$item->getProductId()] = $item->getProductId(); //used array key here to avoid duplicates
    }
$product_ids = explode(',',$_product->getbundle_products());
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', array('in' => $product_ids));
    foreach($collection as $eachproduct){
        $productId = $eachproduct->getId();
        $inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($eachproduct)->getIsInStock();
        $fullstock += $inStock;
}
$product_count = count($collection);
    if ($_product->getAttributeText('bundle') == yes && $fullstock == $product_count):  
        echo'<div class="fgfgdfd" style="text-align:center; float:right;">';
    foreach($collection as $eachproduct){
        echo '<a href="' . $eachproduct->getProductUrl() . '"><img class="bundle" src="' . $eachproduct->getImageUrl() . '" title="' . $eachproduct->getName() . '"></a>';
            $productId = $eachproduct->getId();
if (isset($ids[$productId])) {
    echo '<span class="bundle-check">&#10003;</span>';
}
$combined_price += $eachproduct->getFinalPrice()/100*$_product->getbundle_discount();
}
setlocale(LC_MONETARY, 'en_GB.UTF-8');
echo '<div><p class="availability in-stock">Save <span class="in-stock1">' . money_format('%.2n', $combined_price) . '</span> <span class="when-you"> When you buy</span> <span class="with-with"> with</span> the bundle</p></div></div>';
endif;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.