0
votes

I want an additional column in the Order(s) Grid for Admin. Assuming its Customer Group Id.

My app/etc/modules/MyProject_Adminhtml looks like:

<?xml version="1.0"?>

<config>
    <modules>
        <MyProject_Adminhtml>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Sales />
            </depends>
        </MyProject_Adminhtml>
    </modules>
</config>

My app/code/local/MyProject/Adminhtml/etc/config.xml looks like:

<?xml version="1.0"?>
<config>
    <modules>
        <MyProject_Adminhtml>
            <version>1.0.0</version>
        </MyProject_Adminhtml>    
    </modules>

    <global>
          <blocks>
            <adminhtml>
                <rewrite>
                    <sales_order_grid>MyProject_Adminhtml_Block_Sales_Order_Grid</sales_order_grid>
                </rewrite>
            </adminhtml>
        </blocks>

      </global>

  </config>

And in app/code/local/MyProject/Adminhtml/Block/Sales/Order/Grid.php I have overridden Mage_Adminhtml_Block_Sales_Order_Grid

class MyProject_Adminhtml_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{

    protected function _prepareColumns()
    {
        .... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...

        $this->addColumn('customer_group_id', array(
            'header' => Mage::helper('sales')->__('Customer Group Id'),
            'index' => 'customer_group_id',
            'type'  => 'text',
        ));

         .... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
    }
}

Is there something I am missing because I don't see anything in Order Grid. I am using Magento 1.4.1.1

On Anda B's comment I wrote the following line:

var_dump($this->getLayout()->createBlock('MyProject_Adminhtml_Block_Sales_Order_Grid'));

in app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreateController.php

Then, I selected 'Create New Order' and Cancel the order to see the result of execution of var_dump, and I see the following:

/var/www/magento/var/report/72990635: line 10: syntax error near unexpected token `}' /var/www/magento/var/report/72990635: line 10: `#9 {main}";s:3:"url";s:80:"/index.php/admin/sales_order_create/cancel/key/0624033594dd63d9e145fc538f4c6bbb/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}'
2
does that field exist on the order object or do you need to add it? - Jonathan Day
I had not looked for that earlier, and right now when I looked at app/code/core/Mage/Sales/Model/Order.php I couldn't find it. However my perception has been that if a field existed in DB table sales_flat_order, it would appear here. And it does exist there. - Ozair Kafray

2 Answers

1
votes

In modules/etc you have Atzen_Adminhtml but your project is MyProject_Adminhtml. Except this problem the code should work: even if you don't have the customer_group_id in sales table, the new column should appear in the grid.

4
votes

You are almost there.

You will need to create a renderer for the GroupID as one does not exists in the core.

First, add the renderer to your addColumn like this:

$this->addColumn('customer_group_id', array(
        'header' => Mage::helper('sales')->__('Customer Group Id'),
        'index' => 'customer_group_id',
        'type'  => 'text',
        'renderer' => 'adminhtml/widget_grid_column_renderer_customergroup',
    ));

Now you will need to create the directory /app/code/local/MyProject/Adminhtml/Widget/Grid/Column/Renderer/ as it probably doesn't exist.

Now create a Customergroup.php file containing this class:

class MyProject_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract

{

 private static $_customergroups = array();

 public static function getCustomerGroupsArray() {
     if(count(self::$_customergroups) == 0) {
         $customer_group = Mage::getModel('customer/group');
         $customer_groups = $customer_group->getCollection()->toOptionHash();
         self::$_customergroups = $customer_groups;
     }
     return self::$_customergroups;
 }

public function render(Varien_Object $row){
       $value = $this->_getValue($row);
       $customer_groups = self::getCustomerGroupsArray();
       return isset($customer_groups[$value]) ? $customer_groups[$value] : false;
    }
}

And finally you will need to add this to the config.xml in MyProject. Put this:

<widget_grid_column_renderer_customergroup>Myproject_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup</widget_grid_column_renderer_customergroup>

right next to your other rewrite.

After you refresh your cache you should have your Group labels in your sales order grid.

PS.

if you want to add a filter to the top of the sales order grid to work with this column, add this to the 'addColumn' right after the renderer. (order is not actually important)

'options' => TheReadyStore_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup::getCustomerGroupsArray(),

and change the type from 'text' to 'options'

Cheers Roy