GetGalleryImages在Magento主页上返回null
-
16-10-2019 - |
题
我们已经为客户端扩展了主页滑块,以便它们可以在此领域中配备产品。
作为其中的一部分,我们希望在三个图像插槽中获取产品的主要图像,然后从媒体库中获得两个图像(理想情况下是随机的,但如果是通过ID,则不是世界的尽头)。
为了更好地理解,请参阅迄今为止我们拥有的内容的屏幕截图: -
我们正在使用以下内容为该模块构建集合: -
$featured_products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->AddAttributeToFilter('featured', array('eq' => 1));
获取产品的主要图像没有问题,这与以下各项完美合作: -
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(225); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
这很简单,可以使所有三个图像插槽使用此主图像,如上图所示。
但是,当我们尝试致电GetGalleryImages时,这总是返回 NULL
(例如): -
<?php if (count($this->getGalleryImages()) > 0): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
<?php endforeach; ?>
<?php endif; ?>
请有人建议您在主页上调用画廊图像的最佳方法。我们可以将某些内容包括到集合构建中,还是需要添加观察者。
提前致谢。
解决方案 3
我们最终解决了这个问题,正如@marius指出的那样,需要看 getMediaGalleryImages
.
我们最终使用的代码是: -
<div class="images">
<?php $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages(); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
<?php } ?>
</div>
在这里,我们加载了产品的媒体图像,并确保我们仅显示3个插槽的3个插槽。有兴趣知道,您的答案是否可以实现更好的方法@Marius。
其他提示
我认为这里的主要问题是哪种类型 $this
在您的上下文中。这是一个简单的块,返回是正常的 null
如果该方法在块中不存在。
另一方面。。。这是您如何获取假设变量的产品的图像 $_product
是产品本身的实例:
$gallery = $_product->getMediaGalleryImages();
因此,就您而言,您可能需要这样的东西:
<?php foreach ($featured_products as $_product) : ?>
<?php if (count($_product->getGalleryImages()) > 0): ?>
<?php foreach ($_product->getGalleryImages() as $_image): ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" />
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach;?>
@zigojacko @marius最近,我遇到了一种情况,我需要从产品集合中获取所有媒体库图像,而不会再次加载产品模型,因为它会增加执行时间并使用额外的内存。因此,我进行了研发,并在您在收藏集中加载的产品上getMediagalleryimages之前找到了解决方案。
$product->load('media_gallery');
在下面检查代码段:
$products = Mage::getModel("catalog/product")->getCollection();
$galleryImagesLink = [];
$product->load('media_gallery');
$_images = $product->getMediaGalleryImages();
foreach ($_images as $image) {
$galleryImagesLink[] = Mage::getModel('catalog/product_media_config')->getMediaUrl($image->getFile());
}
Zigojacko 要求更好地做他的工作代码
这里是:无需加载完整产品
<div class="images">
<?php $_images = $product->load('media_gallery')->getMediaGalleryImages(); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
<?php } ?>
</div>
旧问题,但是常见问题...所以也许这可以帮助其他用户
更好的是,编写自己的方法来获取媒体_gallery值,因此请求将更快地处理。这样的东西...
protected $_resource = null;
public function getResource()
{
if (is_null($this->_resource)) {
$this->_resource = Mage::getSingleton('core/resource');
}
return $this->_resource;
}
protected function _getMediaPosition($valueId)
{
$data = array();
$readConnection = $this->getResource()->getConnection('core_read');
$query = $readConnection->select()
->from($this->getResource()->getTableName('catalog_product_entity_media_gallery_value'))
->where('value_id = ?', $valueId);
$results = $readConnection->fetchAll($query);
if (! empty($results)) {
$data = $results[0];
}
return $data;
}
public function getMediaGalleryImages($productId)
{
$data = array();
$gallery = array();
$readConnection = $this->getResource()->getConnection('core_read');
$query = $readConnection->select()
->from($this->getResource()->getTableName('catalog_product_entity_media_gallery'))
->where('entity_id = ?', $productId);
$results = $readConnection->fetchAll($query);
if (! empty($results)) {
foreach ($results as $mediaGalleryRow) {
$gallery[] = array_merge($mediaGalleryRow, $this->_getMediaPosition($mediaGalleryRow['value_id']));
}
}
foreach ($gallery as $i => $element) {
if ($element['disabled'] == 1) {
continue;
}
$obj = new Varien_Object();
$obj->setFile($element['value']);
$data[$element['position'] . '_' . $i] = $obj;
}
ksort($data);
return $data;
}
因此,在您的块中添加该代码后,模板代码将是
<div class="images">
<?php $_images = $this->getMediaGalleryImages($product->getId()); ?>
<?php if($_images){?>
<?php $i=0; foreach($_images as $_image) if ($i++ < 5) { $i++; ?>
<img class="gallery" src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail', $_image->getFile())->resize(255); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
<?php } ?>
</div>