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.
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; ?>
You need to add custom placeholder image to your skin folder: images/catalog/product/placeholder/custom_image.jpg
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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With