0

I am trying to extend the admin order grid by adding a single column. I don't want to add all the logic again in my code, so the only method I'm calling is _prepareColumns():

protected function _prepareColumns()
{
    $this->addColumn('customer_group', array(
        'header'=> Mage::helper('sales')->__('Customer Group'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'increment_id',
    ));
    return parent::_prepareColumns();
}

I've read this article on overriding methods, but I don't know if I'm following it correctly. My module file at ~/app/etc/modules/Namespace_Module.xml includes the following:

<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Adminhtml />
                <Mage_Core />
                <Mage_Sales />
            </depends>
        </Namespace_Module>
    </modules>
</config>

My config.xml file contains:

<config>
    ...
    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <sales_order_grid>Namespace_Module_Block_Adminhtml_Sales_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
    ...
</config>

On my testing site, I get only this Customer Group column on the grid. I am expecting all the columns AND this column. Am I wrong to think it will work like this?

My code is all in a custom module. Thanks.

A picture might help. I'm trying to edit this:

Mage Adminhtml Sales Order Grid

3
  • 1
    The depends from /app/etc/modules/Namespace_Module.xml does not influence class inheritance. In your new module, what class does your new grid block extends? Is the Mage_Adminhtml_Block_Sales_Order_Grid? Do you have rewrite rules in your module's config.xml file? Commented Jul 4, 2013 at 15:40
  • The class is Namespace_Module_Block_Adminhtml_Sales_Order_Grid and it extends Mage_Adminhtml_Block_Widget_Grid. Yes, please see the edit for config.xml code. Thx.
    – Jongosi
    Commented Jul 4, 2013 at 15:53
  • 3
    It should extend the Mage_Adminhtml_Block_Sales_Order_Grid, not the Mage_Adminhtml_Block_Widget_Grid. Commented Jul 4, 2013 at 16:16

3 Answers 3

0

Thanks to @PandaWebStudio for pointing out my error. Yes, I am an idiot.

I was extending the original class Mage_Adminhtml_Block_Widget_Grid, which will add the single column. To get what I wanted, I needed to extend the class from Magento's sales order grid (which itself extends Mage_Adminhtml_Block_Widget_Grid):

Mage_Adminhtml_Block_Sales_Order_Grid

All works perfectly now. Thanks.

1
  • 1
    You're welcome, and another thing I've noticed is that the new column index is increment_id, which I think is not what you want. The index should be the column name in the sql query. I think you also have to override the _prepareCollection() to get the customer group info from db. Commented Jul 4, 2013 at 16:22
0

There is a better solution to add a column to the order grid, see my complete answer/solution in: Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue

1
  • It's bit complicated solution, instead we can achieve it simply by rewriting single file. Commented Dec 2, 2013 at 9:35
0

To add customer group column in Sales Order grid. You can do it by rewriting Mage_Adminhtml_Block_Sales_Order_Grid class

 <?php
  class [CompanyName]_[ModuleName]_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
  {
    protected function _preparePage()
    {

        $this->getCollection()
             ->getSelect()
             ->join( array('customer'=> 'customer_entity'), 'customer.entity_id = main_table.customer_id', array('customer.group_id'));
        return parent::_preparePage();
    }

    protected function _prepareColumns()
    {


        $groups = Mage::getResourceModel('customer/group_collection')
            ->addFieldToFilter('customer_group_id', array('gt'=> 0))
            ->load()
            ->toOptionHash();
        $this->addColumn('customer_group',
            array(
                'header'=> Mage::helper('sales')->__('Customer Group'),
                'width' => '70px',
                'index' => 'group_id',
                'type'  => 'options',
                'options' => $groups,
            ));

        $this->addColumnsOrder('customer_group', 'shipping_name');
        return parent::_prepareColumns();
    }
  }
?>

I hope above code will be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.