2022-10-24 15:26:43 - 米境通跨境电商
MagentoProductImagesFullURLPathInsteadofCached
下面的代码适用于具有图像的产品,但是对于没有图像的产品,占位符小图像不会显示。
echoMage::getModel('catalog/product_media_config')->getMediaUrl($_product->getSmallImage());
相关讨论
1
2
3
4
5
6
7
8
9
//getimagefullurl
echo$imageUrl=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$_product->getImage();
//getimageusingcustomsizewithurl
echo$imageCacheUrl=Mage::helper('catalog/image')->init($_product,'image')->resize(135,135);
?>
相关讨论
影响您要执行的操作的代码是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//file:app/code/core/Mag/Catalog/Helper/Image.php
//class:Mage_Catalog_Helper_Image
/**
*ReturnImageURL
*
*@returnstring
*/
publicfunction__toString()
{
try{
//...
}catch(Exception$e){
$url=Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return$url;
}
有趣的一行是
1
$url=Mage::getDesign()->getSkinUrl($this->getPlaceholder());
因此在您的代码中,您需要测试$_product->getSmallImage()的返回值,如果它为false或null,请改用Mage::getDesign()->getSkinUrl($this->getPlaceholder());。
您可能希望检查$_product->getSmallImage()以查看未设置任何值时返回的内容。
哦,我刚刚检查了一下:getPlaceholder()是一个函数,不是一个神奇的吸气剂。这是函数:
1
2
3
4
5
6
7
8
publicfunctiongetPlaceholder()
{
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'));
如果$_product->getSmallImage()不存在,则在您的phtml文件中为
。
根据您的评论进行更新:
特别是在用于生成显示可写小图像的HTML的.phtml文件中:
1
2
3
4
5
6
7
8
9
$testSmallImageExists=$_product->getSmallImage();
if($testSmallImageExists)
{
echoMage::getModel('catalog/product_media_config')->getMediaUrl($_product->getSmallImage());
}
else
{
echo($this->helper('catalog/image')->init($_product,'small_image'));
}