简体   繁体   中英

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. 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. What am I doing wrong?

Thanks!

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. The issue seems to be that the Magento core is not setting the custom_image_label attribute. Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes - so you could do something like:

$_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() .

If you call $_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:

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

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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