【问题标题】:Call to Get Category Images & Display in List调用获取类别图像并在列表中显示
【发布时间】:2014-02-15 11:37:26
【问题描述】:

我目前已设置为在 magento 自定义网格中显示我的目录图像 - 我似乎很接近,但似乎无法调用最终图像 url 来显示 - 谁能帮助我?

我也尝试从 /media/remote/cache 调用它们,但无济于事:-(

公共函数渲染(Varien_Object $row) {

    $p = Mage::getModel('catalog/product')->load($row->getproduct_id());
    $html = '<img src="' . Mage::getBaseUrl('media').'catalog/product/'.'" width="50" height="50" alt="' . $p->getname() . '" />';
    return $html;
}

【问题讨论】:

    标签: image list magento categories


    【解决方案1】:

    你必须像下面这样创建你的自定义渲染

    只需将其添加到您的网格中

      $this->addColumn('thumbnail',
                    array(
                        'header'=> Mage::helper('catalog')->__('Thumbnail'),
                        'type'  => 'image',
                        'width' => '100',
                        'index' => 'thumbnail',
                        'renderer' => 'NAMESPACE_YOURMODULE_Block_Widget_Grid_Column_Renderer_Image',
                ));
    

    并在

    处创建渲染类
    \NAMESPACE\YOURMODULE\Block\Widget\Grid\Column\Renderer\Image.php
    

    并使用正确的类名将以下代码添加到此文件中

    class NAMESPACE_YOURMODULE_Block_Widget_Grid_Column_Renderer_Image 
        extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
    protected static $showImagesUrl = null;
    protected static $showByDefault = null;
    
    protected static $imagesWidth = null;
    protected static $imagesHeight = null;
    protected static $imagesScale = null;
    
    public function render(Varien_Object $row)
    {
        return $this->_getValue($row);
    }
    
    protected static function initSettings()
    {
        if(!self::$showImagesUrl)
            self::$showImagesUrl = (int)Mage::getStoreConfig('yourmodule/images/showurl') === 1;
        if(!self::$showByDefault)
            self::$showByDefault = (int)Mage::getStoreConfig('yourmodule/images/showbydefault') === 1;
        if(!self::$imagesWidth)
            self::$imagesWidth = Mage::getStoreConfig('yourmodule/images/width');
        if(!self::$imagesHeight)
            self::$imagesHeight = Mage::getStoreConfig('yourmodule/images/height');
        if(!self::$imagesScale)
            self::$imagesScale = Mage::getStoreConfig('yourmodule/images/scale');
    }
    
    
    
    protected function _getValue(Varien_Object $row)
    {
        self::initSettings();
    
        $noSelection    =   false;
        $dored          =   false;
        if ($getter = $this->getColumn()->getGetter())
        {
            $val = $row->$getter();
        }
    
        $val = $val2 = $row->getData($this->getColumn()->getIndex());
        $noSelection = ($val == 'no_selection' || $val == '') ? true : $noSelection;
        $url = Mage::helper('yourmodule')->getImageUrl($val);
    
        if(!Mage::helper('yourmodule')->getFileExists($val)) 
        {
          $dored = true;
          $val .= "[*]";
        }
    
        $dored = (strpos($val, "placeholder/")) ? true : $dored;
        $filename = (!self::$showImagesUrl) ? '' : substr($val2, strrpos($val2, "/")+1, strlen($val2)-strrpos($val2, "/")-1);
    
        $val = ($dored) ? 
                ("<span style=\"color:red\" id=\"img\">$filename</span>") :
                "<span>". $filename ."</span>";
    
        $out = (!$noSelection) ? 
                ($val. '<center><a href="#" onclick="window.open(\''. $url .'\', \''. $val2 .'\')" title="'. $val2 .'" '. ' url="'.$url.'" id="imageurl">') :
                '';
    
        $outImagesWidth = self::$imagesWidth ? "width='".self::$imagesWidth."'":'';
        if(self::$imagesScale)
            $outImagesHeight = (self::$imagesHeight) ? "height='".self::$imagesHeight."'":'';
        else
            $outImagesHeight = (self::$imagesHeight && !self::$imagesWidth) ? "height='".self::$imagesHeight."'":'';
    
        //return '<img src="'.(Mage::helper("catalog/image")->init($productItem, "small_image")->resize(self::$imagesWidth)).'" '.$outImagesWidth.' '.$outImagesHeight.' alt="" />';
    
    
    
    
         $out .= (!$noSelection) ? 
                    "<img src=".(Mage::helper("catalog/image")->init($row, $this->getColumn()->getIndex())->resize(self::$imagesWidth))." ".$outImagesWidth." ".$outImagesHeight." border=\"0\"/>" :
    //                "<img src=".$url." ".$outImagesWidth." ".$outImagesHeight." border=\"0\"/>" :
    //                "" :
                    "<center><strong>[".__('NO IMAGE')."]</strong></center>";
    
            return $out. ((!$noSelection)? '</a></center>' : '');
        }
    

    希望这对你有帮助。

    【讨论】:

    • 我似乎没有运气 - 我过去使用过这个:load($ _item['product_id']); $i=0; foreach 但它对我不起作用:-(你还有其他想法吗?
    • 你可以使用像 echo Mage::helper('catalog/image')->init($products, 'small_image')->resize(163,100); // 如果你有产品集合对象,resize 函数用于调整图像大小
    • 其中 $products = Mage::getModel('catalog/product')->load(1); //产品编号
    • 还是没有运气怎么办?更近一点? $p = Mage::getModel('catalog/product')->load($row->getproduct_id()); $html = 'getsmall_image() . '" width="50" height="50" alt=" ' . $p->getname() . '" />';返回 $html;
    • 哪里 $row->getproduct_id() 应该来自 for 循环作为集合对象?看到这肯定会帮助你stackoverflow.com/questions/2474117/…
    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    相关资源
    最近更新 更多