[英]Grabbing a custom product image label in Magento?
I have a block on my front page that is grabbing the two newest products with a custom product image called image_feature_front_right
. 我的首页上有一个街区,该街区正在使用名为image_feature_front_right
的自定义产品图片来image_feature_front_right
两个最新产品。
I use this code as the query: 我使用以下代码作为查询:
$_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(2);
I am able to get: 我可以得到:
echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4)
图片: echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4)
echo $_product->getProductUrl()
产品网址: echo $_product->getProductUrl()
$this->stripTags($_product->getName(), null, true)
产品名称: $this->stripTags($_product->getName(), null, true)
The only thing I am unable to get is the custom images label. 我唯一无法获得的是自定义图像标签。 I have tried echo $this->getImageLabel($_product, 'image_feature_front_right')
, but that seems to do nothing. 我试过echo $this->getImageLabel($_product, 'image_feature_front_right')
,但这似乎无济于事。
Can anyone point me in the right direction? 谁能指出我正确的方向?
Thanks! 谢谢!
Tre 特雷
I imagine this is some sort of magento bug. 我想这是某种magento错误。 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可以让您覆盖Abstract类)是明智的选择,但是如果您需要快速修改-只需将此函数粘贴到phtml文件中,然后调用:
<?php echo getImageLabel($_product, 'image_feature_front_right')?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.