简体   繁体   English

Magento中的自定义图像属性在未设置任何图像时生成错误,并从产品视图页面调用

[英]Custom Image Attribute in Magento generates error when not set any image and called from product view page

I have created a custom image attribute and added it to default attribute set in Magento. 我创建了一个自定义图像属性,并将其添加到Magento中的默认属性集中。 My new attributes are img_support and img_upgrade . 我的新属性是img_supportimg_upgrade I've displayed both images on product view page using this piece of code: 我使用以下代码在产品视图页面上显示了这两个图像:

<?php if ($_product->getimg_upgrade()):
 $_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'img_upgrade')->resize(85).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'"  "/>';
 echo $_helper->productAttribute($_product, $_img, 'img_upgrade');
 endif; 
?>

Same code with the second custom image attribute name will display img_support image. 具有第二个自定义图像属性名称的相同代码将显示img_support图像。 When I set images and select them for these two attributes they'll display fine but when there is no image uploaded and selected for these attributes and I try to show it on product view page then it generates an error. 当我设置图像并为这两个属性选择它们时,它们将显示正常,但是当没有上传和选择这些属性的图像时,我尝试在产品视图页面上显示它然后它会生成错误。 Something like this: 像这样的东西:

a:5:{i:0;s:25:"Image file was not found.";i:1;s:3911:"#0 C:\Program Files\EasyPHP-    5.3.8.0\www\mymagentoroot\app\code\core\Mage\Catalog\Helper\Image.php(163):
Mage_Catalog_Model_Product_Image->setBaseFile('no_selection').....

And a long trace after that. 之后有很长的痕迹。 So basically it fetches the image even though there is no image selected. 所以基本上它即使没有选择图像也会取出图像。 Can anyone tell me how can I display my custom images in product view page of selected products? 谁能告诉我如何在所选产品的产品视图页面中显示我的自定义图像? I have, say 10 products with default attribute set. 我有10个产品, 默认属性设置。 So all of these 10 products CAN be set with img_support and img_upgrade but I want to show the custom images only on 5 selected products. 因此,所有这10个产品都可以使用img_support和img_upgrade设置,但我想仅在5个选定的产品上显示自定义图像。 If the images are set display. 如果图像设置显示。 Otherwise don't. 否则不要。 How can i do this? 我怎样才能做到这一点? If there is any other proper or better way to do this then help me out here. 如果有任何其他正确或更好的方法来做到这一点,那么帮助我在这里。 Thanks! 谢谢!

When a product has no image for a given attribute, Magento uses a placeholder image instead. 当产品没有给定属性的图像时,Magento会使用占位符图像。
Basically, each image attribute must have a placeholder image in the skin directory, named with the corresponding attribute code. 基本上,每个图像属性必须在皮肤目录中具有占位符图像,并使用相应的属性代码命名。
In your case, you should create two new placeholder images for your two new attributes img_support and img_upgrade , in this skin directory : images/catalog/product/placeholder/your_attribute_code.jpg (either with a new image, or with a copy of one of the base placeholders) 在您的情况下,您应该在此皮肤目录中为您的两个新属性img_supportimg_upgrade创建两个新的占位符图像: images/catalog/product/placeholder/your_attribute_code.jpg (使用新图像或其中一个的副本)基地占位符)

This problem also happens if you upload image for that attribute then suddenly remove image file via FTP. 如果您上传该属性的图像然后突然通过FTP删除图像文件,也会发生此问题。 It is not good to show the site down. 显示该网站是不好的。 To summarize, there are 3 ways to fix this: 1) Answer of @blmage: use place holder 2) Use try catch: 总结一下,有3种方法可以解决这个问题:1)@blmage的答案:使用占位符2)使用try catch:

<?php 
try {
if ($_product->getimg_upgrade()):
 $_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'img_upgrade')->resize(85).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'"  "/>';
 echo $_helper->productAttribute($_product, $_img, 'img_upgrade');
 endif; 
}
catch (Exception $e) {

}
?> 
  1. Check if the file is existed 检查文件是否存在

    getBaseMediaPath(); getBaseMediaPath(); $baseFile = $baseDir . $ baseFile = $ baseDir。 $_product->getImgUpgrade(); $ _product-> getImgUpgrade(); if (file_exists($baseFile)) { } ?> if(file_exists($ baseFile)){}?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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