Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Image Switcher - Almost there! 
 
bobbrodie
Sr. Member
 
Avatar
Total Posts:  132
Joined:  2008-04-02
Philadelphia, PA
 

Is there a php function to pull the images for a product by ID?

Thanks,
Bob

 
Magento Community Magento Community
Magento Community
Magento Community
 
MageParts
Guru
 
Avatar
Total Posts:  415
Joined:  2007-11-18
 

This is the code you are looking for, I’ve used the product id 4 as an example, just change it to whatever you need.

// To get base image url
$product Mage::getModel('catalog/product')->load(4)->getImage();

// To get small image url
$product Mage::getModel('catalog/product')->load(4)->getSmallImage();

// To get thumbnail url
$product Mage::getModel('catalog/product')->load(4)->getThumbnail();

Hope it helps ^^

 
Magento Community Magento Community
Magento Community
Magento Community
 
bobbrodie
Sr. Member
 
Avatar
Total Posts:  132
Joined:  2008-04-02
Philadelphia, PA
 

Thanks, that gets me a step closer. Here is the code that I’m testing but running into trouble with:

<script type="text/javascript">
    var 
selectmenu=document.getElementById('attribute<?php echo $_attribute->getAttributeId() ?>');
    
selectmenu.onchange=function(){
        
var productId spConfig.config.attributes[<?php echo $_attribute->getAttributeId() ?>].options[this.selectedIndex].products;
        
alert(productId);
        var 
message "<?php echo(Mage::getModel('catalog/product')->load(35)->getImage()); ?>";
        
alert(message);
    
}
</script>

When you change the dropdown in a configurable product, a message will pop up and give you the ID - that’s good.
The next message is the filename of product ID #35. The problem is that I need that 35 to be productId, and can’t do this without reloading the page.

Maybe this can be done with prototype?

I really appreciate any help I can get - whoever figures this out will be one of the greatest in the community.

 
Magento Community Magento Community
Magento Community
Magento Community
 
SeL_
Magento Team
 
Avatar
Total Posts:  1140
Joined:  2007-10-10
Paris, France
 

Hi,

What about creating an array with all ids and related image urls ?
Something like :

<script type="text/javascript">
    var 
selectmenu=document.getElementById('attribute<?php echo $_attribute->getAttributeId() ?>');
    
selectmenu.onchange=function(){
        
var productId spConfig.config.attributes[<?php echo $_attribute->getAttributeId() ?>].options[this.selectedIndex].products;
        
alert(productId);
<?php foreach ($productIds as $productId)?>
        
var message_<?php echo $productId?> "<?php echo(Mage::getModel('catalog/product')->load($productId)->getImage()); ?>";
<?php endforeach; ?>
        alert(eval
('message_'+productId));
    
}
</script>

I haven’t tested this. Let us know.

EDIT: combining with that thread : http://www.magentocommerce.com/boards/viewreply/87511/

 
Magento Community Magento Community
Magento Community
Magento Community
 
bobbrodie
Sr. Member
 
Avatar
Total Posts:  132
Joined:  2008-04-02
Philadelphia, PA
 

SeL,

Thank you -

After thinking about it I know that you can’t directly pass a JavaScript variable to PHP, and I really like your idea. I didn’t get it working since $productIds isn’t defined.

My plan is what you mention - create an array of the possibilities, and then work with that. I will need to create a PHP function to get a list of all the product ids and their images:

template/catalog/product/view/type/options:

<?php
    
foreach ($_product->getTypeInstance()->getUsedProducts() as $item{
        
echo $item->getId() . "<br />";
        echo 
$item->getImage() . "\n";
        
}
?>

I’d like to take this a step further and also get ALL the images for the associated products, maybe with

foreach($item->getGalleryImages as $subImage{
    
echo $subImage->getFile();
}

I also have figured out accessing the JSON dropdown through jQuery:

<script>
    
jQuery(document).ready(function(){
        jQuery
("#attribute<?php echo $_attribute->getAttributeId() ?>").change(function(){
            
var productId spConfig.config.attributes[<?php echo $_attribute->getAttributeId() ?>].options[this.selectedIndex-1].products;
            
alert(productId);
        
});
    
});
</script>

this.selectedIndex-1 takes care of the offset of “Choose an option...”.

When you choose an option from the dropdown, you receive the product id. Now I need to pull the image location from the PHP array and load that. I know how to swap images in jQuery, but I don’t know how to say in code how to get a single item from my PHP array.

Does this look worthwhile?

Thanks again,
Bob

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