24

TL;DR: How do I load the product images/gallery without loading the whole product?

I want to load the images on a product. What I do in the .phtml

$_popularCollection = $this->getPopularCollection();
foreach ($_popularCollection as $_product):
    // the rest
    $mediaGallery = $_product->getMediaGalleryImages();
endforeach;
//the rest

What I do in the Block class:

public function getPopularCollection() {
    // http://magento.stackexchange.com/q/5838/3089

    // no category
    if ( is_null( $this->getCategoryId() ) )
        return false;

    /** @var Mage_Catalog_Model_Category $category */
    $category = Mage::getModel('catalog/category')->load( (int)$this->getCategoryId() );

    /** @var Mage_Catalog_Model_Resource_Product_Collection $_popularCollection */
    $_popularCollection = Mage::getModel('catalog/product')->getResourceCollection();
    $_popularCollection->addAttributeToSelect('*');
    $_popularCollection->setStoreId(Mage::app()->getStore()->getId());
    $_popularCollection->addCategoryFilter($category);

    return $_popularCollection;
}

This works But I load everything: $_popularCollection->addAttributeToSelect(*);

I tried $_popularCollection->addAttributeToSelect('media_gallery'); But it doesn't seem to work.

3 Answers 3

25

addAttributeToSelect('media_gallery') is not working as media_gallery will come from complicated logic.

You can get easy media_gallery data inside at loop by loading the full product model But it will take a lot of memory in this case.

Use Backend model of media_gallery attribute & afterload of product Model

For getting media_gallery attribute value without full product load (Mage::getModel('catalog/product')->load($ProductID)) .

You can use afterLoad(), using this function call backend model of media_gallery eav attribute load the media image. This is much faster than full model load(Mage::getModel('ctalog/product')->load($ProductID))

foreach($his->getPopularCollection() as $product){
    $attributes = $product->getTypeInstance(true)->getSetAttributes($product);
    $media_gallery = $attributes['media_gallery'];
    $backend = $media_gallery->getBackend();
    $backend->afterLoad($product); 
    $mediaGallery = $product->getMediaGalleryImages();
        /* get Image one by  using loop*/
        foreach ($product->getMediaGalleryImages() as $image) {
        echo ($image->getUrl());
        }     

echo "<br/>..........................................<br/>";


}
2
  • 1
    Yes that worked
    – janw
    Commented Apr 30, 2015 at 8:20
  • nice! it worked Commented Mar 3, 2018 at 6:41
19

The accepted answer works, but involves loading all the product attributes into memory when perhaps they are unnecessary.

To take a slightly more surgical approach (tested in 1.9.2.2 CE) do

$attributeCode = 'media_gallery';
$attribute = $product->getResource()->getAttribute($attributeCode);
$backend = $attribute->getBackend();
$backend->afterLoad($product);

This way you only load the media_gallery attribute itself.

1
  • 1
    It's much better solution than accepted answer
    – SeStro
    Commented Aug 3, 2018 at 11:15
5

I know the question is old and that it already has a correct answer, but it is not bad to recommend another method that can be useful to the community: Before $_product->getMediaGalleryImages()

You can try:

$_product->load('media_gallery');//load media gallery attributes
$mediaGallery = $_product->getMediaGalleryImages();

Taken from: https://stackoverflow.com/questions/7890616/get-product-media-gallery-images-from-a-product-collection-in-magento#23804663

2
  • 1
    Nice find! However, I must point out that the solution described by +amit-bera above is significantly faster and less memory and database intensive. [Edit: The approach by +aaron-bonner is actually even faster.]
    – Tyler V.
    Commented Mar 8, 2017 at 23:01
  • No. This a bad answer. You can't load specific data on a product this way, it just calls beforeLoad() and afterLoad() on the product, and does not change the already existing data on the model because the database call won't return anything from this function. Look at the load() function definition on Mage_Core_Model_Abstract and Mage_Core_Model_Resource_Db_Abstract. What you're doing here is just almost a full load of the product.
    – OddBrew
    Commented May 24, 2019 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.