Magento Forum

   
How to get images fallery of all associated products? 
 
shoo
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2008-08-26
Poland, Lodz
 

Hi all,

I am having a little problem with getting Media Gallery Images for associated products of Grouped Product. I am going to sell products with different sizes/colors. Product stock is managed by external application and every color has different, unique id (sku) in there - I will have to use Grouped Product functionality. However, in the main product page I would like to display images of all colors and here what I think - I am going to create 3 different products, each for specific color - ie. red, green, blue.Then I will add a Grouped product and assign those items to it. And now the problem: how to display images of all associated products in the Grouped product page?

My solution is more or less this: rewrite media block to get all associated items’ galleries and add them to main product gallery.

Here is the code:

class Mods_MyMod_Block_Product_View_Media extends Mage_Catalog_Block_Product_View_Media
{
    
public function getGalleryImages()
    
{
        $ret 
parent::getGalleryImages();

        
$product $this->getProduct();
        if(
$product->getTypeId() == 'grouped')
        
{
            
/* all subproducts */
            
$associated $product->getTypeInstance()->getAssociatedProducts();
            if(
is_array($associated) && count($associated))
            
{
                
foreach($associated as $item)
                
{
                    $itemImages 
$item->getMediaGalleryImages();
                    if(
is_array($itemImages) && count($itemImages))
                    
{
                        
foreach($itemImages as $image)
                        
{
                            
/* add to main product gallery */                     
                            
$ret->addItem($imag$e);            
                        
}
                    }
                }
            }
        }
        
        
        
return $ret;
    
}

As you can see I am getting all subproducts for grouped products, and then I am trying to get their media gallery. However for some reason

$itemImages $item->getMediaGalleryImages();
returns NULL value. I am sure those items DO HAVE IMAGES still this returns empty. I tried reading the code but honestly, I have no idea why this happends. When I enter each of subproducts page images are displayed without problems.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brandlive_br
Guru
 
Avatar
Total Posts:  653
Joined:  2008-05-19
Buenos Aires, Argentina
 

The problem you have is that you’re not loading the associated product as an object.

Before doing the getMediaGalleryImages() do this:

<?php $item-obj = Mage::getModel('catalog/product')->load($item->getId()); ?>

And then:

<?php $itemImages = $item-obj->getMediaGalleryImages(); ?>

Hope that helps

--
Ezequiel

 
Magento Community Magento Community
Magento Community
Magento Community
 
shoo
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2008-08-26
Poland, Lodz
 

Hi,

Thanks a lot for this! This have indeed solved the issue but in order to clarify -

$product->getTypeInstance()->getAssociatedProducts();
does return an array of objects (var_dump says so at least) but they were probably not initialized correctly.

Nonetheless your solutions works perfectly fine, thanks!:)

 
Magento Community Magento Community
Magento Community
Magento Community
 
blazec
Jr. Member
 
Avatar
Total Posts:  27
Joined:  2010-01-08
Surabaya, Indonesia
 

Hi Shoo..

Thanks for this code, I really appreciate it that you want to share with us. but please bare with me, where do I have to put those class?! and do I have to write a config.xml for that?!

regards,

 
Magento Community Magento Community
Magento Community
Magento Community
 
blazec
Jr. Member
 
Avatar
Total Posts:  27
Joined:  2010-01-08
Surabaya, Indonesia
 

Hi again..

this is the code from your snippet earlier and a revision from the guy at brandlive team:

path: /app/code/local/MyMods/Catalog/Block/Product/View/Media.php

class MyMods_Catalog_Block_Product_View_Media extends Mage_Catalog_Block_Product_View_Media
{
    
public function getGalleryImages()
    
{
        $ret 
parent::getGalleryImages();
        
$product $this->getProduct();
        if(
$product->getTypeId() == 'grouped')
        
{
            
/* all subproducts */
            
$associated $product->getTypeInstance()->getAssociatedProducts();       
            if(
is_array($associated) && count($associated))
            
{
                
foreach($associated as $item)
                
{
                    $itemobj 
Mage::getModel('catalog/product')->load($item->getId());
                    
$itemImages $itemobj->getMediaGalleryImages();
                    
//$itemImages = $item->getMediaGalleryImages();
                    
if(is_array($itemImages) && count($itemImages))
                    
{
                        
foreach($itemImages as $image)
                        
{
                            
/* add to main product gallery */                     
                            
$ret->addItem($image);            
                        
}
                    }
                }
            }
        } 
else {
            $ret 
$product->getMediaGalleryImages();
        
}
        
return $ret;
    
}
}

and this is my config file:
path: /app/code/local/MyMods/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <
modules>
         <
MyMods_Catalog>
            <
version>0.0.1</version>
        </
MyMods_Catalog>
    </
modules>
    <global>
        <
models>
            <
catalog>
                <
rewrite>
                    <
product>MyMods_Catalog_Block_Product_View_Media</product>
                </
rewrite>
            </
catalog>
        </
models>
   </global>
</
config>

and this is the xml for activating the mods.
path: /app/etc/modules/MyMods_All.xml

<?xml version="1.0"?>
<config>
    <
modules>
    <
MyMods_Catalog>
            <
active>true</active>
            <
codePool>local</codePool>      
            <
depends>
                    <
Mage_Catalog />
            </
depends>
        </
MyMods_Catalog>
    </
modules>
</
config>

I already implementing it but still have an error report from magento. can someone put me on the right direction?!

 
Magento Community Magento Community
Magento Community
Magento Community
 
cocos
Jr. Member
 
Total Posts:  24
Joined:  2009-12-11
South Korea, Seoul
 

<?php if ($_product->getImage() != \'no_selection\' && $_product->getImage()): ?>

\”getImage() method of Mage_Catalog_Model_Product class\”

where is it? (where is declared file?)

please help me…

 
Magento Community Magento Community
Magento Community
Magento Community
 
jonasfr
Member
 
Total Posts:  31
Joined:  2008-09-27
 

hi all!

i think after updating to 1.4.1.0 $associated = $product->getTypeInstance()->getAssociatedProducts(); returns an empty array :-( i found your thread, because i did a similar customization for project ... did you find any solution for this yet??

thanks in advance,

jonas

 
Magento Community Magento Community
Magento Community
Magento Community
 
ibmjango
Jr. Member
 
Total Posts:  2
Joined:  2010-06-08
 

Does this codes really works? cause somewhere i read that there is bit complication after doing this..any suggestion ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Netismine
Sr. Member
 
Avatar
Total Posts:  174
Joined:  2008-10-28
 
Brandlive (Brasil) - 03 February 2009 04:05 AM

The problem you have is that you’re not loading the associated product as an object.

Before doing the getMediaGalleryImages() do this:

<?php $item-obj = Mage::getModel('catalog/product')->load($item->getId()); ?>

And then:

<?php $itemImages = $item-obj->getMediaGalleryImages(); ?>


Hope that helps

--
Ezequiel

There has to be a less expensive way to do it - loading product is a heavy operation, so if you have 10+ products in collection, your frontend will surely feel it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
accepto
Jr. Member
 
Total Posts:  2
Joined:  2010-10-15
 

Here is my solution for the problem:

in the grouped.phtml file I included in the loop <?php foreach ($_associatedProducts as $_item): ?>
.....

echo ‘<img src=“‘.$this->helper(’catalog/image’)->init($_item, ‘thumbnail’)->resize(77, 77).’” width="77"” onmouseover=changeImage(“‘.$this->helper(’catalog/image’)->init($_item, ‘image’).’")>’;

....

to display a thumb plus a little javascript (and the end of the file)

<script type="text/javascript\">
function changeImage(imageUrl){
$(’image’).src = imageUrl;
}
</script>

where image is the id of the large product image.

 
Magento Community Magento Community
Magento Community
Magento Community
 
PixelPaul
Jr. Member
 
Total Posts:  9
Joined:  2010-06-27
 

i have an easy step by step guide here that may help…

http://www.pixelpaul.net/magento-use-associated-product-images-group-product-image/

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top