【问题标题】:Create magento module to add column image and column category to product management table创建magento模块以将列图像和列类别添加到产品管理表
【发布时间】:2016-12-31 11:50:22
【问题描述】:

我有一项任务,将列图像和列类别添加到产品管理表(管理部分)。谁能告诉我工作流程(我必须遵循哪些步骤)?

我正在使用 Magento 1.9.2.4。

【问题讨论】:

    标签: php magento magento-1.9


    【解决方案1】:

    首先,要向现有产品管理表添加新列,您必须扩展 magento 块:Mage_Adminhtml_Block_Catalog_Product_Grid。 为此,您可以创建自定义模块 fe。命名为 XXX 并在您的 config.xml 文件中放入以下行:

    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>XXX_Adminhtml_Block_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
    

    现在在您的文件 XXX_Adminhtml_Block_Catalog_Product_Grid 中,您需要覆盖两个方法:_prepareCollection()

    class XXX_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
        // ...
        protected function _prepareCollection()
        {
            //...
            $collection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect('sku')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('attribute_set_id')
                ->addAttributeToSelect('type_id')
                ->addAttributeToSelect('thumbnail');
            //...
    

    还有第二种方法:

    protected function _prepareColumns()
    {
        //...
        $this->addColumn('product_image', array(
            'header'    => Mage::helper('frame')->__('Thumbnail'),
            'column_css_class' => 'vertical-align-middle',
            'width'     => '90px',
            'index'     => 'frame_left',
            'type'      => 'image',
            'escape'    => true,
            'sortable'  => false,
            'filter'    => false,
            'renderer'  => Mage::getBlockSingleton('xxx_adminhtml_block_catalog_product_grid_renderer_image')
        ));
        //...
    

    根据需要放置 product_image 列,此处添加列的顺序至关重要。最后一步是创建图像渲染器:

    class XXX_Adminhtml_Block_Catalog_Product_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
    {
        public function render(Varien_Object $row)
        {
            $thumbnail = $row->getThumbnail();
            $gridImageSrc = $this->getSkinUrl('images/np_thumb2.gif');
            if($thumbnail != 'no_selection') {
                $temp =  str_replace("\\","/", Mage::getBaseUrl('media') . 'catalog'. DS . 'product' . $thumbnail);
                $fileExistsRemote = @fopen($temp, 'r');
                if($fileExistsRemote) {
                    $gridImageSrc = $temp;
                }
                @fclose($fileExistsRemote);
            }
            $html = '<img ';
            $html .= 'id="' . $this->getColumn()->getId() . '" ';
            $html .= 'width="80" ';
            $html .= 'height="80" ';
            $html .= 'src="' . $gridImageSrc . '" ';
            $html .= 'class="grid-image vertical-align-middle"/>';
    
            return $html;
        }
    }
    

    您可以轻松添加类别小部件的类似方法示例代码可能并不完美,但应该可以工作。享受吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多