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

I need to access the data of the product’s media gallery image, but it’s “protected” ! 
 
dagobert renouf
Jr. Member
 
Avatar
Total Posts:  26
Joined:  2008-07-03
 

Hello everyone,

I’m currently creating a simple module that will change the product image depending on the option’s value selected by the user (example: the user choose blue, the product’s image become the one associated with the color blue).

To do this, I need to access the image_media_gallery data of the product in the options file, so I did this at the bottom of template\catalog\product\view\options.phtml:

<?php 
$_product 
$this->getProduct();
$_mgi $_product->getMediaGalleryImages();
?>

Then if I print_r the $_mgi variable, I can clearly see what I need in the array returned, but if I try to access it, I have a fatal error telling me _items is protected ...

here’s the print_r

Varien_Data_Collection Object
(
    
[_items:protected] => Array
        (
            
[1] => Varien_Object Object
                
(
                    
[_data:protected] => Array
                        (
                            
[value_id] => 1
                            [file] 
=> /b/o/body_test_green_1.jpg
                            [label] 
=> couleur__vert
                            [position] 
=> 1

And here’s how I try to access to a specific part of it :

echo $_mgi->_items[1]->data['file'];

I’m asking if there’s a solution to make it work, or if I’m wrong and should concider a different approach.

 
Magento Community Magento Community
Magento Community
Magento Community
 
joyously
Guru
 
Total Posts:  447
Joined:  2008-08-21
 

Protected data means you should be using an access function to get at the values in the correct way.
Look in the object’s definition (or one of the classes derived from it) for a function that gives you the part you want, and call that function instead of trying to access it directly.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dagobert renouf
Jr. Member
 
Avatar
Total Posts:  26
Joined:  2008-07-03
 

Hi joyously, you helped me out!

I just have basic oop knowledge but I’m not stuck anymore, I’ll keep you guys up with my module if it can help people out.

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