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?
You can use
$product->load('media_gallery');
before getMediaGalleryImages (on the products you loaded in the collection).
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.
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');
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With