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.
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.
Fatal error: Call to a member function getId() on null in /srv/ambushco-/app/code/core/Mage/Catalog/Model/Resource/Product/Attribute/Backend/Media.php on line 76
Whether I call load()
on the collection or not, it results in the same error.
Commented
Dec 8, 2016 at 1:11
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();
}