Magento – Magento : How to get small images of all items in bundled product

associate-productsbundled-productmagento-1.9product-imagesproduct-view

I want to show all images of item(product) of bundled product on product listing page ,is there any Magento function to do so, please guide me way to do it.

I created bundled product with 4 items say a,b,c,d

Now on product listing page I want to show all 4 small_images of these product .

How can I do it ?

Best Answer

Try this:

 $model= Mage::getModel('catalog/product');
 //$product is a product object
 if($product->getTypeId() == 'bundle'){
    $bundles = $product->getTypeInstance(true)->getChildrenIds($product->getId(), false);

    $item = array();
    foreach($bundles as $index => $items){
        foreach($items as $id){
            $item[] = $id;
            //break; //only get first item
            //un-comment above line if you want only one (first) item.
        }
    }

    if($item){
          $child = $model
                    ->getCollection()
                    ->addAttributeToSelect(array('name', 'price', 'image', 'small_image'))
                    ->addAttributeToFilter('entity_id', array('in' => $item))
                    ->setOrder('price', 'DESC')
                    ->load()
                    ;
    }

   foreach($child as $item){
        echo $item->getName();
        echo "<br>";
        echo '<img id="image" src="'.$this->helper('catalog/image')->init($item, 'small_image')->keepAspectRatio(true)->keepFrame(false)->resize(120, 100).'" alt="'.$_helper->productAttribute($item, $item->getName(), 'name').'"/>';
        echo "<br>";
        echo "<br>";
   }
}

Key Points

  1. $product is a product object.

This should work. Good luck.

Related Topic