【问题标题】:Magento Product Images Full URL Path Instead of CachedMagento 产品图片完整 URL 路径而不是缓存
【发布时间】:2023-04-10 21:19:01
【问题描述】:

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

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

【问题讨论】:

  • 您是否将代码放在下面的文件中以适用于具有图像的产品? app/code/core/Mage/Catalog/Helper/Image.php

标签: magento


【解决方案1】:

影响你想要做什么的代码是

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

【讨论】:

  • 我需要在哪里放置此代码:` echo Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getSmallImage());` 适用于具有图像的产品?在下面的文件 app/code/core/Mage/Catalog/Helper/Image.php
【解决方案2】:
<?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
  • 我在答案的末尾添加了一个示例。如果这不能回答您的问题,请扩展您的问题以准确解释您要实现的目标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 2013-01-06
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多