简体   繁体   English

在magento中检索产品自定义媒体图像标签

[英]Retrieve product custom media image label in magento

I have a custom block loading products on my front page that loads the four newest products that have a custom product picture attribute set via: 我在首页上有一个自定义块加载产品,它通过以下方式加载了四个具有自定义产品图片属性集的最新产品:

$_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);

What I am trying to do is grab the image_feature_front_right label as set in the back-end, but have been unable to do so. 我想要做的是获取image_feature_front_right设置的image_feature_front_right标签,但是无法这样做。 Here is my code for displaying the products on the front end: 这是我在前端显示产品的代码:

<?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>

I read that $this->getImageLabel($_product, 'image_feature_front_right') was the way to do it, but produces nothing. 我读到$this->getImageLabel($_product, 'image_feature_front_right')是这样做的方法,但什么也没产生。 What am I doing wrong? 我究竟做错了什么?

Thanks! 谢谢!

Tre TRE

It seems you asked this same question in another thread, so to help others who might be searching for an answer, I'll anser it here as well: 看来你在另一个帖子中问了同样的问题,所以为了帮助那些可能正在寻找答案的人,我也会在这里解答:

I imagine this is some sort of magento bug. 我想这是某种magento bug。 The issue seems to be that the Magento core is not setting the custom_image_label attribute. 问题似乎是Magento核心没有设置custom_image_label属性。 Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes - so you could do something like: 而对于默认的内置图像[image,small_image,thumbnail_image],它确实设置了这些属性 - 所以你可以这样做:

$_product->getData('small_image_label');

If you look at Mage_Catalog_Block_Product_Abstract::getImageLabel() it just appends '_label' to the $mediaAttributeCode that you pass in as the 2nd param and calls $_product->getData() . 如果查看Mage_Catalog_Block_Product_Abstract::getImageLabel()它只需将'_label'附加到作为第二个参数传入的$mediaAttributeCode并调用$_product->getData()

If you call $_product->getData('media_gallery'); 如果你打电话给$_product->getData('media_gallery'); you'll see the custom image label is available. 您将看到自定义图像标签可用。 It's just nested in an array. 它只是嵌套在一个数组中。 So use this function: 所以使用这个功能:

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 '';
}

It'd be prudent to extend the Magento core code (Ideally Mage_Catalog_Block_Product_Abstract , but I don't think Magento lets you override Abstract classes), but if you need a quick hack - just stick this function in your phtml file then call: 扩展Magento核心代码(理想情况下Mage_Catalog_Block_Product_Abstract ,但我认为Magento不允许你覆盖抽象类)是明智的,但是如果你需要快速破解 - 只需在你的phtml文件中粘贴这个函数,然后调用:

<?php echo getImageLabel($_product, 'image_feature_front_right')?>

Your custom block would need to inherit from Mage_Catalog_Block_Product_Abstract to give access to that method. 您的自定义块需要从Mage_Catalog_Block_Product_Abstract继承才能访问该方法。

You could also use the code directly from the method in the template: 您也可以直接从模板中的方法使用代码:

$label = $_product->getData('image_feature_front_right');
if (empty($label)) {
    $label = $_product->getName();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM