tr3*_*ine 1 product image magento
我在首页上有一个自定义块加载产品,它通过以下方式加载了四个具有自定义产品图片属性集的最新产品:
$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(4);
Run Code Online (Sandbox Code Playgroud)
我想要做的是抓住image_feature_front_right后端设置的标签,但是无法这样做.这是我在前端显示产品的代码:
<?php foreach($_productCollection as $_product) : ?>
<div class="fll frontSale">
<div class="productImageWrap">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4) ?>" />
</div>
<div class="salesItemInfo">
<a href="<?php echo $this->getUrl($_product->getUrlPath()) ?>"><p class="caps"><?php echo $this->htmlEscape($_product->getName());?></p></a>
<p class="nocaps"><?php echo $this->getImageLabel($_product, 'image_feature_front_right') ?></p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我读到这$this->getImageLabel($_product, 'image_feature_front_right')是做到这一点的方法,但什么也没有产生.我究竟做错了什么?
谢谢!
TRE
看来你在另一个帖子中问了同样的问题,所以为了帮助那些可能正在寻找答案的人,我也会在这里解答:
我想这是某种magento bug.问题似乎是Magento核心没有设置custom_image_label属性.而对于默认的内置图像[image,small_image,thumbnail_image],它确实设置了这些属性 - 所以你可以这样做:
$_product->getData('small_image_label');
Run Code Online (Sandbox Code Playgroud)
如果你看一下,Mage_Catalog_Block_Product_Abstract::getImageLabel()只需将'_label'附加到$mediaAttributeCode你传入的第二个参数并调用$_product->getData().
如果您打电话,$_product->getData('media_gallery'); 您将看到自定义图像标签可用.它只是嵌套在一个数组中.所以使用这个功能:
function getImageLabel($_product, $key) {
$gallery = $_product->getData('media_gallery');
$file = $_product->getData($key);
if ($file && $gallery && array_key_exists('images', $gallery)) {
foreach ($gallery['images'] as $image) {
if ($image['file'] == $file)
return $image['label'];
}
}
return '';
}
Run Code Online (Sandbox Code Playgroud)
扩展Magento核心代码是明智的(理想情况下Mage_Catalog_Block_Product_Abstract,但我认为Magento不允许你覆盖抽象类),但如果你需要快速破解 - 只需在你的phtml文件中粘贴这个函数,然后调用:
<?php echo getImageLabel($_product, 'image_feature_front_right')?>
Run Code Online (Sandbox Code Playgroud)