如何解决Magento 1.9 getMediaGalleryImages() 从产品集合中返回空
我正在尝试从 Magento 1.9 中提取产品信息,我需要提取的属性之一是“media_gallery”,但是,我尝试了多种方法,但到目前为止都没有奏效。这是我获取产品集合的代码:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributetoSelect('*')
->joinField(
'qty','cataloginventory/stock_item','qty','product_id=entity_id','{{table}}.stock_id=1','left'
);
这是我拉媒体库的代码:
$productsArray = [];
$index = 0;
foreach ($products as $product) {
...
$productsArray[$index]["gallery"] = $product->getMediagalleryImages();
...
}
我试过了:
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
我得到的回应是gallery: {}
这是我的产品在 Magento Admin 上的设置方式,您可以看到我的产品下确实有图像:
请帮助我,感谢您的时间!
解决方法
问题是图像已保存到默认存储范围 (StoreId 0)。
尝试使用商店 ID = 0 加载您的产品数据
$product = Mage::getModel('catalog/product')->setStoreId(0)->load($Id);
根据您使用代码的位置,它可能会加载前端商店视图之一的数据,而不是商店 0。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。