Sales Order – Adding Custom Sales Order Attribute to Order Grid

order-gridsales-order

I have created a custom sales order attribute using the installer of my custom mage module like this:

$installer = new Mage_Sales_Model_Mysql4_Setup;
try
{
    $installer->addAttribute('order', 'sales_rep', array
    (
        'type'            => 'varchar',
        'backend_type'    => 'varchar',
        'frontend_input'  => 'varchar',
        'is_user_defined' => true,
        'label'           => 'Sales Rep',
        'visible'         => true,
        'required'        => false,
        'user_defined'    => false,
        'searchable'      => false,
        'filterable'      => false,
        'comparable'      => false,
        'default'         => ''
    ));
}
catch (Exception $ex) { }
$installer->endSetup();

This attribute value is normally set when orders are created programatically.

Now, I would like to show this attribute on the sales order grid, so I have duplicated this file:

app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php

To

app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php

and amended the _prepareCollection() method like this:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    $select = $collection->getSelect();
    $select->joinLeft(array
    (
        'order' => Mage::getModel('core/resource')->getTableName('sales/order')),
        'order.entity_id=main_table.entity_id',
        array('sales_rep' => 'sales_rep')
    );
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

I've verified this is generating the correct SQL by doing this:

exit($collection->getSelect());

and it outputs:

SELECT main_table.*, order.sales_rep FROM sales_flat_order_grid AS main_table
LEFT JOIN sales_flat_order AS order ON order.entity_id=main_table.entity_id

I've also verified that this returns result by executing the above query in phpMyAdmin and I get the following:

enter image description here

Having done all the test, I proceeded to update the _prepareColumns() method on Grid.php by adding the following (just before status):

$this->addColumn('sales_rep', array(
    'header' => Mage::helper('sales')->__('Sales Rep'),
    'index' => 'sales_rep',
    'type'  => 'options',
    'width' => '70px'
));

Now, when I refresh the sales order grid in magento backend, I am not getting any values back, it looks like this:

enter image description here

Any ideas what I am doing wrong here?

Best Answer

Error in column code use below code.

$this->addColumn('sales_rep', array(
    'header' => Mage::helper('sales')->__('Sales Rep'),
    'index' => 'sales_rep',
    'type'  => 'text',
    'width' => '70px'
));

you have used 'type' => 'options' and not define options there