Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Product Media Gallery Images from a Product Collection in Magento

Tags:

php

magento

I have a collection of products in Magento that I would like to be able to get the media gallery images out of. However I am finding that I have to iterate though my collection and load the product again to get the getMediaGalleryImages() function to work properly.

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', 4)
    ->addAttributeToFilter('status', 1);

foreach($products as $product) {
    $_product = Mage::getModel('catalog/product')->load($product->getId());

    $product->getMediaGalleryImages();      // This returns nothing
    $_product->getMediaGalleryImages();     // Returns the Collection of Images
}

Obviously I could continue just reloading the product each time, but that would add quite a bit of overhead to the time required to run this code.

Is there a way to add in the media gallery images to the collection?

like image 478
Josh Pennington Avatar asked Oct 25 '11 14:10

Josh Pennington


4 Answers

You can use

$product->load('media_gallery');

before getMediaGalleryImages (on the products you loaded in the collection).

like image 114
Matthias Kleine Avatar answered Oct 17 '22 16:10

Matthias Kleine


One simple method for future reference:

Outside your foreach add

$mediaBackend = Mage::getModel('catalog/product_attribute_backend_media');
$mediaGalleryAttribute = Mage::getModel('eav/config')->getAttribute(Mage::getModel('catalog/product')->getResource()->getTypeId(), 'media_gallery');
$mediaBackend->setAttribute($mediaGalleryAttribute);

and then do the foreach:

foreach ($productCollection as $product) {
    $mediaBackend->afterLoad($product);
}

You will have then the gallery loaded on product.

like image 28
user2239352 Avatar answered Oct 17 '22 17:10

user2239352


load product's cached image using collection by following codes

Mage::helper('catalog/image')->init($_product, 'small_image')->resize(135);

//or

Mage::helper('catalog/image')->init($_product, 'thumbnail')->resize(135);

//or

Mage::helper('catalog/image')->init($_product, 'image')->resize(135);

this is the collection which i used

$collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('small_image') //or
        ->addAttributeToSelect('thumbnail')  //or
        ->addAttributeToSelect('image');
like image 37
Deepak Mallah Avatar answered Oct 17 '22 17:10

Deepak Mallah


You can create a helper class, and use it every time you need media gallery images to be loaded for a collection of products:

class My_Package_Helper_Media extends Mage_Core_Helper_Abstract {
    public function addMediaGalleryAttributeToProductCollection( &$productCollection )
    {
        $storeId = Mage::app()->getStore()->getId();

        $ids = array();
        foreach ( $productCollection as $product ) {
            $ids[] = $product->getEntityId();
        }

        $resource = Mage::getSingleton( 'core/resource' );
        $conn = Mage::getSingleton( 'core/resource' )->getConnection( 'catalog_read' );
        $select = $conn->select()
            ->from(
                   array( 'mg' => $resource->getTableName( 'catalog/product_attribute_media_gallery' ) ),
                   array(
                         'mg.entity_id', 'mg.attribute_id', 'mg.value_id', 'file' => 'mg.value',
                         'mgv.label', 'mgv.position', 'mgv.disabled',
                         'label_default' => 'mgdv.label',
                         'position_default' => 'mgdv.position',
                         'disabled_default' => 'mgdv.disabled'
                         )
                   )
            ->joinLeft(
                       array( 'mgv' => $resource->getTableName( 'catalog/product_attribute_media_gallery_value' ) ),
                       '(mg.value_id=mgv.value_id AND mgv.store_id=' . $storeId . ')',
                       array()
                       )
            ->joinLeft(
                       array( 'mgdv' => $resource->getTableName( 'catalog/product_attribute_media_gallery_value' ) ),
                       '(mg.value_id=mgdv.value_id AND mgdv.store_id=0)',
                       array()
                       )
            ->where( 'entity_id IN(?)', $ids );

        $mediaGalleryByProductId = array();

        $stmt = $conn->query( $select );
        while ( $gallery = $stmt->fetch() ) {
            $k = $gallery[ 'entity_id' ];
            unset( $gallery[ 'entity_id' ] );
            if ( !isset($mediaGalleryByProductId[$k]) ) {
                $mediaGalleryByProductId[$k] = array();
            }
            $mediaGalleryByProductId[$k][] = $gallery;
        }
        unset( $stmt ); // finalize statement

        // Updating collection ...
        foreach ( $productCollection as &$product ) {
            $productId = $product->getEntityId();
            if ( isset( $mediaGalleryByProductId[ $productId ] ) ) {
                $product->setData( 'media_gallery', array( 'images' => $mediaGalleryByProductId[ $productId ] ) );
            }
        }
        unset( $mediaGalleryByProductId );
    }
}

Sample usage:

$coll = Mage::getResourceModel('catalog/product_collection')
    ->setStoreId( Mage::app()->getStore()->getId() )
    ->addAttributeToFilter( 'sku', array( 'in' => array( 'AAA', 'BBB' ) ) );
Mage::helper('my_package/media')->addMediaGalleryAttributeToProductCollection( $coll );
like image 1
Dmitri Sologoubenko Avatar answered Oct 17 '22 16:10

Dmitri Sologoubenko