Product image url as part of collection load
-
16-10-2019 - |
Question
Is it possible to have the product image url loaded as part of the product collection?
for examples:
Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
This doesnt add the image.
Solution
Do it as such:
$collection = Mage::getModel('catalog/product')->getCollection();
$backendModel = $collection->getResource()->getAttribute('media_gallery')->getBackend();
foreach($collection as $product){
$backendModel->afterLoad($product); //adding media gallery to the product object
var_dump($product->getData()); //you should see media gallery information here now
}
This loads the backend model and appends the media gallery attribute for the $product
in the loop instead of re-loading the entire product model.
OTHER TIPS
These worked for me
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('image');
foreach($collection as $product){
(string)Mage::helper('catalog/image')->init($product, 'image');
//Or with resize
(string)Mage::helper('catalog/image')->init($product, 'image')->resize(200);
}
I think you should be able to access the image URL from the product such as:
$collection = Mage::getModel('catalog/product')->getCollection();
foreach ($collection as $product) {
echo $product->getImageUrl();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange