如何将产品媒体库与产品系列一起加载?
有人可以给我一个关于如何加载产品媒体库和收藏的提示吗?
我得到的集合是这样的:
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($storeId)
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
foreach ($collection as $product) {
var_dump($product->getMediaGalleryImages());
}
但是 getMediaGalleryImages() 返回 null
。我知道我可以使用 $product = Mage::getModel('catalog/product')->load($product->getId())
单独加载每个产品,但我想避免因为这会造成不必要的工作量。
Can anybody give me a hint about how to load product's media gallery along with the collection?
I'm getting the collection like this:
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($storeId)
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
foreach ($collection as $product) {
var_dump($product->getMediaGalleryImages());
}
But getMediaGalleryImages() returns null
. I know that I can load each product separately with $product = Mage::getModel('catalog/product')->load($product->getId())
but I want to avoid this, because it causing unnecessary workload.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果有人正在寻找另一种方法,我发现这是可行的(只有一种情况,所以不能保证!):
请务必先执行
$collection->addAttributeToSelect('image');
,然后在循环浏览集合产品时,执行以下操作:不确定所有这些是否有必要,但我很着急。在我的特定情况下,我尝试使用
$product->getImageUrl();
获取图像 url,这种方法对我有用。希望它对其他人有帮助。
In case anyone’s looking for another approach on this, I found this to work (in just one case so no guarantees!):
Be sure to do
$collection->addAttributeToSelect(’image’);
first, then when looping through the collection products, do:Not sure if all of this is necessary but I’m in a hurry. In my particular case I was trying to get the image url using
$product->getImageUrl();
and this approach worked for me.Hope it helps someone else.
我最近不得不做同样的、最快的方法:
I had to do the same recently, fastest method:
试试这个
try this
可以直接在循环中使用:
It can be used directly in loop:
您将必须使用:
参考:http://www .magentocommerce.com/boards/viewthread/29639/
You are going to have to use:
Reference: http://www.magentocommerce.com/boards/viewthread/29639/
处理涉及产品的自定义集合时的秘密武器是
init
方法的第三个参数......至少对我来说是这样。 这样我就不需要加载整个产品并运行昂贵的查询。因此,拥有代表
Mage_Catalog_Model_Product
实例的自定义$product
但来自我的自定义集合,我可以这样做:我还需要添加
image
属性到我的自定义集合,我通过向其中添加->addAttributeToSelect(['image'])
来实现这一点。您还可以相应地调整图像大小:
The secret sauce when working with custom collections involving products is the third parameter of
init
method... at least it was for me. This way I don't need to load the whole product and run expensive queries.So, having my custom
$product
which represents an instance ofMage_Catalog_Model_Product
but from my custom collection, I can do:I also needed to add the
image
attribute to my custom collection, and I did that by adding->addAttributeToSelect(['image'])
to it.You can also resize your image accordingly:
这是将媒体库添加到集合中的函数:
下面是其用法的示例:
Here is a function to add the media gallery to a collection:
An example of it's usage below: