Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento custom image attribute. How do I get in frontend?

Tags:

php

magento

I've added an image attribute with name and label e.g. New Image, new_image.

How do I fetch this from e.g. view.phtml? I thought it would be as simple as

<?php
    echo $this->helper('catalog/image')->init($_product, 'new_image')->resize(150, 150);
?>

But, this is throwing an error.

Does anybody know the correct way to do this?

Many thanks.

like image 451
Schonkton Avatar asked Jul 09 '10 11:07

Schonkton


5 Answers

Answer is too late for you but it might help others. Following answer applies to product view page.

First check that either this attribute have some value or not.

$isNewImage =$_product->getResource()->getAttribute('new_image')->getFrontend()->getValue($_product);

If it has some value selected from back end

<?php if ( $isNewImage != 'no_selection'):?>

$_newImg ='<img id="your-id" src="'.$this->helper('catalog/image')->init($_product, 'new_image')->resize(300,300).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';

 echo $_helper->productAttribute($_product, $_newImg , 'new_image'); 

<?php  endif; ?>
like image 162
Farrukh Avatar answered Oct 22 '22 16:10

Farrukh


You need to add custom placeholder image to your skin folder: images/catalog/product/placeholder/custom_image.jpg

like image 3
Roman Snitko Avatar answered Oct 22 '22 14:10

Roman Snitko


I had the same problem displaying in list but finally found a way to solve it. I found that the attribute is shown only in product view page not in list page. So what I did was load the product once again as:

$product=Mage::getModel('catalog/product')->load($_product->getId());

And when I used $product instead of $_product in the displaying image, i.e

helper('catalog/image')->init($product, 'new_image')->resize(150, 150); ?>

it worked.

I know this is not a good way to do it but I don't know any other way. Maybe we could do something to select the attribute in list to.

like image 3
bhab Avatar answered Oct 22 '22 15:10

bhab


Schonkton, I can verify that your code worked just fine in catalog/product/view.phtml. I take it that you added the image attribute to the attribute group... A side note is that the output from the echo will deliver an image link, so needs to be enclosed in an image tag.

    echo $this->helper('catalog/image')->init($_product, 'new_image')->resize(150, 150);
like image 1
Komra Moriko Avatar answered Oct 22 '22 16:10

Komra Moriko


Appears to be that you are using $_product from a collection - try to load it first:

$product = Mage::getModel('catalog/product')->load($_product->getId());
$this->helper('catalog/image')->init($product, 'new_image')->resize(150, 150);
like image 1
Anton Suslov Avatar answered Oct 22 '22 14:10

Anton Suslov