繁体   English   中英

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

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

我创建了一个自定义图像属性,并将其添加到Magento中的默认属性集中。 我的新属性是img_supportimg_upgrade 我使用以下代码在产品视图页面上显示了这两个图像:

<?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; 
?>

具有第二个自定义图像属性名称的相同代码将显示img_support图像。 当我设置图像并为这两个属性选择它们时,它们将显示正常,但是当没有上传和选择这些属性的图像时,我尝试在产品视图页面上显示它然后它会生成错误。 像这样的东西:

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').....

之后有很长的痕迹。 所以基本上它即使没有选择图像也会取出图像。 谁能告诉我如何在所选产品的产品视图页面中显示我的自定义图像? 我有10个产品, 默认属性设置。 因此,所有这10个产品都可以使用img_support和img_upgrade设置,但我想仅在5个选定的产品上显示自定义图像。 如果图像设置显示。 否则不要。 我怎样才能做到这一点? 如果有任何其他正确或更好的方法来做到这一点,那么帮助我在这里。 谢谢!

当产品没有给定属性的图像时,Magento会使用占位符图像。
基本上,每个图像属性必须在皮肤目录中具有占位符图像,并使用相应的属性代码命名。
在您的情况下,您应该在此皮肤目录中为您的两个新属性img_supportimg_upgrade创建两个新的占位符图像: images/catalog/product/placeholder/your_attribute_code.jpg (使用新图像或其中一个的副本)基地占位符)

如果您上传该属性的图像然后突然通过FTP删除图像文件,也会发生此问题。 显示该网站是不好的。 总结一下,有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. 检查文件是否存在

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

暂无
暂无

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

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