产品图像URL作为收集负载的一部分
-
16-10-2019 - |
题
是否可以将产品图像URL作为产品集合的一部分加载?
举些例子:
Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
这不会添加图像。
解决方案
这样做:
$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
}
这加载了后端模型并附加了媒体库属性 $product
在循环中,而不是重新加载整个产品模型。
其他提示
这些对我有用
$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);
}
我认为您应该能够从产品中访问图像URL,例如:
$collection = Mage::getModel('catalog/product')->getCollection();
foreach ($collection as $product) {
echo $product->getImageUrl();
}