Magento产品图像完整的URL路径而不是缓存

时间:2014-03-05 19:56:12

标签: magento

以下代码适用于包含图片的产品,但对于没有图片的产品,占位符小图片不会显示。

  

echo Mage :: getModel('catalog / product_media_config') - > getMediaUrl($ _product-> getSmallImage());

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

影响您要执行的操作的代码是

//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文件中

根据您的评论进行更新:

具体在您用于生成显示您可以编写的小图像的HTML的.phtml文件中:

$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方式。

相关问题