E
E
Evgeniy Toropov2014-06-25 10:38:28
PHP
Evgeniy Toropov, 2014-06-25 10:38:28

How to display image attribute value in Magento?

1. There is a product page
2. There is a block output on this page of the type = catalog/product_view_media
3. There is a method that receives a list of images in this block $this->getGalleryImages()
4. Each image can refer to Base Image / Small Image / Thumbnail / [custom media attributes]
The question is: how to get the type (Base Image / Small Image / ...) that an image belongs to from the resulting collection using $this->getGalleryImages() ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Gerasimenko, 2014-06-25
@evgeniytoropov

Not directly. Does not contain a collection of required information:

/**
     * Retrive media gallery images
     *
     * @return Varien_Data_Collection
     */
    public function getMediaGalleryImages()
    {
        if(!$this->hasData('media_gallery_images') && is_array($this->getMediaGallery('images'))) {
            $images = new Varien_Data_Collection();
            foreach ($this->getMediaGallery('images') as $image) {
                if ($image['disabled']) {
                    continue;
                }
                $image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']);
                $image['id'] = isset($image['value_id']) ? $image['value_id'] : null;
                $image['path'] = $this->getMediaConfig()->getMediaPath($image['file']);
                $images->addItem(new Varien_Object($image));
            }
            $this->setData('media_gallery_images', $images);
        }

        return $this->getData('media_gallery_images');
    }

You can see from the code that the collection is created on the fly. The $image object doesn't have the required type attribute initially. And this attribute is not entered into the collection by updating the $image data. Only through comparison with the data of the $_product object. You should compare by the file attribute of $image from the collection.
$_product->getImage() # /a/s/asics-men-s-gel-kayano-xii.jpg
$_product->getSmallImage() # /a/s/asics-men-s-gel-kayano-xii-2.jpg
$_product->getThumbnail() # /a/s/asics-men-s-gel-kayano-xii-2.jpg
...
$_image->getFile() # в цикле даст список путей выше, 
# если только изображение не исключено из коллекции в бэкэнде

66a736e10142ffc2cefe003a2af18b1252c17935

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question