Magento custom image attribute. How do I get in frontend?
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
<?p开发者_如何转开发hp
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);
There are only a few valid string values for that init call: image
, small_image
and thumbnail
, corresponding to the product attributes for those images. If you want to call the images that way, you'll need to set the new_image
to be one of those three images in the administration panel.
Otherwise, look for "gallery" methods on the product, as those will give you all your images to iterate through.
Thanks, Joe
Farrukhs code is close to the best answer,
Although he assumes that we always have a $_helper object by default, if this is not the case just call echo where $_newImg attribute is cast
Secondly, I display my image in a jquery scriptoluscious popup window, and this code causes that window to open the site itself in the popup window, than on the 3rd level it displays my image attribute... It almost feels like the movie inception, when you keep clicking the popup within the popup frame until you get the image...Just wanted to share the bizzareness!
But, this is throwing an error.
I bet your error in var/report is like
Image not found
Make sure you uploaded the image to the product. I suggest to use the example from bhab to prevent errors if products dont have the new image.
So, make sure
- attribute new_image exist
- attribute is in your current attribute set
- view.phtml uses bhabs code
- a new_image -image is given in product
精彩评论