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
FROMsales_flat_order_grid
ASmain_table
LEFT JOINsales_flat_order
ASorder
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:
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:
Any ideas what I am doing wrong here?