【问题标题】:Cannot read product images on list page in Magento2无法在 Magento2 的列表页面上读取产品图片
【发布时间】:2016-01-19 11:46:08
【问题描述】:

我正在尝试在列表页面上加载产品图片。这是我的代码:

$_productCollection = $block->getLoadedProductCollection();
foreach ($_productCollection as $_product){

     $images = $product->getMediaGalleryImages();

$_product 变量很好。变量 $images 始终为空。

【问题讨论】:

    标签: gallery product magento2


    【解决方案1】:

    我可以用下面的代码解决这个问题:

    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $_productCollection = $block->getLoadedProductCollection();
    
    foreach ($_productCollection as $_product){
    
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);        
        $images = $product->getMediaGalleryImages();
    }
    

    【讨论】:

      【解决方案2】:

      您需要使用addMediaGalleryData 方法。

      更多详情请见this answer on magento.stackexchange.com

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多