下面的代碼適用於具有圖像的產品,但對於沒有圖像的產品,佔位符小圖像不顯示。Magento Product Images完整的URL路徑而不是緩存
echo Mage :: getModel('catalog/product_media_config') - > getMediaUrl($ _product-> getSmallImage());
下面的代碼適用於具有圖像的產品,但對於沒有圖像的產品,佔位符小圖像不顯示。Magento Product Images完整的URL路徑而不是緩存
echo Mage :: getModel('catalog/product_media_config') - > getMediaUrl($ _product-> getSmallImage());
影響,你想要做什麼的代碼是
//file: app/code/core/Mag/Catalog/Helper/Image.php
//class: Mage_Catalog_Helper_Image
/**
* Return Image URL
*
* @return string
*/
public function __toString()
{
try {
//...
} catch (Exception $e) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
有趣的線是
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
因此,在你的代碼,你需要測試的$_product->getSmallImage()
的返回值,如果它是虛假的還是空的使用Mage::getDesign()->getSkinUrl($this->getPlaceholder());
代替。
您可能要檢查$_product->getSmallImage()
看什麼的時候沒有設置值返回。
哦,我只是檢查:getPlaceholder()
是一個函數不是魔術吸氣。這是函數:
public function getPlaceholder()
{
if (!$this->_placeholder) {
$attr = $this->_getModel()->getDestinationSubdir();
$this->_placeholder = 'images/catalog/product/placeholder/'.$attr.'.jpg';
}
return $this->_placeholder;
}
所以,你必須揭開一些$this
(提示$this->_getModel()
是Mage::getModel('catalog/product_image')
)
或削減長話短說只是回落到默認:
echo ($this->helper('catalog/image')->init($_product, 'small_image'));
在PHTML文件,如果$_product->getSmallImage()
不存在。
更新您的評論如下:
Specifcically在.phtml
文件,您正在使用,以生成顯示小圖片的HTML代碼,你可以寫:
$testSmallImageExists = $_product->getSmallImage();
if($testSmallImageExists)
{
echo Mage::getModel('catalog/product_media_config')->getMediaUrl($_product->getSmallImage());
}
else
{
echo ($this->helper('catalog/image')->init($_product, 'small_image'));
}
或者只是簡單地使用
echo ($this->helper('catalog/image')->init($_product, 'small_image'));
我敢肯定,是標準的Magento的方式。
這裏我需要放置這段代碼:'echo_Mage :: getModel('catalog/product_media_config') - > getMediaUrl($ _product-> getSmallImage() );' 工作的產品有圖像?在下面的文件中 app/code/core/Mage/Catalog/Helper/Image.php – fresher
<?php
// get image full url
echo $imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $_product->getImage();
// get image using custom size with url
echo $imageCacheUrl = Mage::helper('catalog/image')->init($_product, 'image')->resize(135,135);
?>
您是否將代碼放在下面的文件中,以便爲具有圖像的產品工作? 'app/code/core/Mage/Catalog/Helper/Image.php' – fresher