簡體   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