php Magento 自定义图像属性。我如何进入前端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3212199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento custom image attribute. How do I get in frontend?
提问by Schonkton
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
我如何从例如 view.phtml 中获取它?我以为就这么简单
<?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.
非常感谢。
采纳答案by Komra Moriko
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.
Schonkton,我可以验证您的代码在 catalog/product/view.phtml 中工作正常。我认为您将图像属性添加到属性组中...附带说明是,echo 的输出将提供图像链接,因此需要包含在图像标签中。
echo $this->helper('catalog/image')->init($_product, 'new_image')->resize(150, 150);
回答by bhab
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 $productinstead of $_productin the displaying image, i.e
当我使用$product而不是$_product在显示图像中时,即
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.
我知道这不是一个好方法,但我不知道任何其他方法。也许我们可以做一些事情来选择列表中的属性。
回答by Farrukh
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; ?>
回答by Roman Snitko
You need to add custom placeholder image to your skin folder: images/catalog/product/placeholder/custom_image.jpg
您需要将自定义占位符图像添加到您的皮肤文件夹: images/catalog/product/placeholder/custom_image.jpg
回答by Anton Suslov
Appears to be that you are using $_product from a collection - try to load it first:
似乎是您正在使用集合中的 $_product - 尝试先加载它:
$product = Mage::getModel('catalog/product')->load($_product->getId());
$this->helper('catalog/image')->init($product, 'new_image')->resize(150, 150);
回答by Joseph Mastey
There are only a few valid string values for that init call: image, small_imageand 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_imageto be one of those three images in the administration panel.
该 init 调用只有几个有效的字符串值:image、small_image和thumbnail,对应于这些图像的产品属性。如果您想以这种方式调用图像,则需要new_image在管理面板中将其设置为这三个图像之一。
Otherwise, look for "gallery" methods on the product, as those will give you all your images to iterate through.
否则,请在产品上寻找“图库”方法,因为这些方法将为您提供所有图像以供迭代。
Thanks, Joe
谢谢,乔
回答by TonkBerlin
But, this is throwing an error.
但是,这是抛出错误。
I bet your error in var/reportis like
我敢打赌你在var/report 中的错误就像
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.
确保您将图像上传到产品。如果产品没有新图像,我建议使用 bhab 中的示例来防止错误。
So, make sure
所以,请确保
- attribute new_imageexist
- attribute is in your current attribute set
- view.phtmluses bhabs code
- a new_image -image is given in product
- 属性new_image存在
- 属性在您当前的属性集中
- view.phtml使用 bhabs 代码
- 产品中给出了一个 new_image -image
回答by serdarsenay
Farrukhs code is close to the best answer,
Farrukhs 代码接近最佳答案,
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
尽管他假设我们默认总是有一个 $_helper 对象,如果不是这种情况,只需调用 echo where $_newImg 属性被转换
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!
其次,我在 jquery scriptoluscious 弹出窗口中显示我的图像,这段代码使该窗口在弹出窗口中打开站点本身,而不是在第 3 层显示我的图像属性......感觉就像电影开场一样,当您继续单击弹出框内的弹出框直到获得图像时……只是想分享一下这种奇怪的感觉!

