Search code examples
phpmagento

Magento 1.9 getMediaGalleryImages() return empty from products collection


I am trying to pull products info from Magento 1.9 and one of the attributes I need to pull is "media_gallery", however, I had tried multiple approaches and none of them worked so far. Here is my code on getting the product collection:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    );

Here is my code on pulling the media gallery:

$productsArray = [];
$index = 0;
foreach ($products as $product) {
    ...
    $productsArray[$index]["gallery"] = $product->getMediaGalleryImages();
    ...
}

I have tried:

1) Mage::getModel('catalog/product')->load($product_id)->getMediaGalleryImages();
2) $product->load('media_gallery')->getMediaGalleryImages();
3) $attribute = $product->getResource()->getAttribute('media_gallery');
   $backend = $attribute->getBackend();
   $backend->afterLoad($product);
// None of those 3 approaches worked

The response I got had been gallery: {} This is how my product is set up on Magento Admin you can see I do have images under the product: enter image description here

Please help me and I appreciate your time!


Solution

  • The issue is that the images have been saved to the default store scope (StoreId 0).

    Try loading your product data using store ID = 0

    $product = Mage::getModel('catalog/product')->setStoreId(0)->load($Id);
    

    Depending on where your code is being used it's probably loading the data for one of the frontend store views rather than store 0.