Nhan Nguyen
Nhan Nguyen

Reputation: 307

Create magento module to add column image and column category to product management table

I have a task adding a column image and column category to product management table (admin section). Can anyone tell me the workflow (just which steps I have to follow)?

I'm using Magento 1.9.2.4.

Upvotes: 0

Views: 981

Answers (1)

jimmy
jimmy

Reputation: 21

First of all, to add new column to existing product management table you must extend magento block: Mage_Adminhtml_Block_Catalog_Product_Grid. To do so, you could create your custom module fe. named XXX and in your config.xml file put these lines:

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

Now in your file XXX_Adminhtml_Block_Catalog_Product_Grid you need to overwrite two methods: _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');
        //...

and the second method:

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')
    ));
    //...

Put product_image column as you want, the order of adding column is crucial here. The last step is to create your image renderer:

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;
    }
}

Similar way you can easly add category widget Sample code may not being perfect but should works. Enjoy.

Upvotes: 2

Related Questions