影响你想要做什么的代码是
//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() 的返回值,如果它是 false 或 null,请改用 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'));
如果$_product->getSmallImage() 不存在,则在您的 phtml 文件中。
根据您的评论更新:
特别是在.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 方式。