Magento E-commerce Platform Q/ A

How to Add a Custom Table to Magento Admin Backend

In this article we will add a custom table to Magento admin interface. The table that will be displaying information from  the data base table: sales_flat_order_item.

238

Namespace - Ikantam

Module - KnowledgeBase

In the configuration file of the module we will create new item 'Order Items'

239

/app/code/local/Ikantam/KnowledgeBase/etc/config.xml

<config>
…
    <adminhtml>
        <menu>
            <sales>
                <children>
                    <order_items translate="title">
                        <title>Order Items</title>
                        <sort_order>20</sort_order>
                        <action>adminhtml/order_items/index</action>
                    </order_items>
                </children>
            </sales>
        </menu>
    </adminhtml>
…
</config>

In the <action> tag we will indicate the path to a page of the new menu item (in our case it is 'Order items'). The format of the record in our example is following:  <action>adminhtml/{controller}/{action}</action>

 

Then in 'contollers' folder of the module we will create a new controller: Ikantam_KnowledgeBase_Adminhtml_Order_ItemsController, which is inherited from: Mage_Adminhtml_Controller_Action.

240

In the configuration file we will add the root to the controller:

<config>
...    
<admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <knowledgebase before="Mage_Adminhtml">Ikantam_KnowledgeBase_Adminhtml</knowledgebase>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
…
</config>

}

Next we will create the action in the added controller. The action will highlight the new menu item and load the table.

 

The file:

/app/code/local/Ikantam/KnowledgeBase/controllers/Adminhtml/Order/ItemsController.php

<?php

class Ikantam_KnowledgeBase_Adminhtml_Order_ItemsController extends Mage_Adminhtml_Controller_Action
{

    public function indexAction()
    {
        $this->loadLayout()->_setActiveMenu('sales/order_items');
        $this->_addContent($this->getLayout()->createBlock('knowledgebase/adminhtml_order_items'));
        $this->renderLayout();
    }

}

 $this->_addContent($this->getLayout()->createBlock('knowledgebase/adminhtml_order_items'));  - adds block to  layout.

Next step is to create blocks necessary for the table.

The blocks structure illustrated below:

241

/app/code/local/Ikantam/KnowledgeBase/Block/Adminhtml/Order/Items.php

<?php

class Ikantam_KnowledgeBase_Block_Adminhtml_Order_Items extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'knowledgebase';
        $this->_controller = 'adminhtml_order_items';
        $this->_headerText = Mage::helper('adminhtml')->__('Orders Items');

        parent::__construct();
        $this->_removeButton('add');
    }
}

$this->_blockGroup = 'knowledgebase'; - indicate the name of the module.

$this->_controller = 'adminhtml_order_items'; - current block.

$this->_headerText = Mage::helper('adminhtml')->__('Orders Items');  - Table title.

/app/code/local/Ikantam/KnowledgeBase/Block/Adminhtml/Order/Items/Grid.php

<?php

class Ikantam_KnowledgeBase_Block_Adminhtml_Order_Items_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('order_items');
        $this->setDefaultSort('item_id');
    }


    protected function _prepareCollection()
    {

        $collection = Mage::getModel('sales/order_item')->getCollection();
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }


    protected function _prepareColumns()
    {
        $this->addColumn('item_id', array(
            'header' => Mage::helper('knowledgebase')->__('ID'),
            'sortable' => true,
            'width' => '60',
            'index' => 'item_id'
        ));

        $this->addColumn('order_id', array(
            'header' => Mage::helper('knowledgebase')->__('Order Id'),
            'sortable' => true,
            'width' => '60',
            'index' => 'order_id',
            'type'  => 'text'
        ));

        $this->addColumn('created_at', array(
            'header' => Mage::helper('knowledgebase')->__('Created At'),
            'sortable' => true,
            'width' => '60',
            'index' => 'created_at',
            'type'  => 'datetime'
        ));

        return parent::_prepareColumns();
    }


}

protected function _prepareCollection()  -  here we will add collection, that will appear in the table.

protected function _prepareColumns() – here we should indicate columns to be added to a table.

In case you need to get data from another data base table, you need to connect it in the collection with the help of join method.

Here is the example (we will add a bilingual user name to our table)

We will change method _prepareCollection

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('sales/order_item')->getCollection();
        $collection->join(array('og' =>'sales/order_grid'), 'main_table.order_id = og.entity_id', 'billing_name');
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }

The name of the table is passed as the first parameter to the join method (could be passed via array to set table alias). Second goes the condition, then columns (multiple columns should be passed via array).

We've added the column to the collection, and now need to show it in the table. Add these code lines (see below) to a method _prepareColumns():

    $this->addColumn('billing_name', array(
            'header' => Mage::helper('knowledgebase')->__('Billing Name'),
            'sortable' => true,
            'width' => '60',
            'index' => 'billing_name'

        ));

On the image below you can see added 'Billing Name' column in the table:

242

Also in the table columns it is possible to  not only display data base data but also create custom rendering. For example, let's make Order Id a link to the proper order page.

Add this block:  Ikantam_KnowledgeBase_Block_Adminhtml_Order_Items_Grid_Renderer_Order

243

<?php
class Ikantam_KnowledgeBase_Block_Adminhtml_Order_Items_Grid_Renderer_Order
    extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        $value =  $row->getData($this->getColumn()->getIndex());
        $html ='<a href="' . $this->getUrl('adminhtml/sales_order/view', array('order_id' => $value, 'key' => $this->getCacheKey())) . '" target="_blank" title="' . $value . '" >' . $value . '</a>';
        return $html;
    }
}

The variable: $value would contain order_id.

Here: Ikantam_KnowledgeBase_Block_Adminhtml_Order_Items_Grid  we will replace these lines:

        $this->addColumn('order_id', array(
            'header' => Mage::helper('knowledgebase')->__('Order Id'),
            'sortable' => true,
            'width' => '60',
            'index' => 'order_id'
        ));

To:

       $this->addColumn('order_id', array(
            'header' => Mage::helper('knowledgebase')->__('Order Id'),
            'sortable' => true,
            'width' => '60',
            'index' => 'order_id',
            'renderer' => 'Ikantam_KnowledgeBase_Block_Adminhtml_Order_Items_Grid_Renderer_Order',
        ));

As a result, Order Id  will become a link to the order.

244