2

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?

  • Is possible to workout, the manually change added attribute values like if order placed I need manually change the attribute value in sales order grid, first of all how can I creat attribute and how can I Integrate to sales order table,, Is possible? – zus Sep 27 '17 at 11:25
  • Is your Issue Solved? – zus Oct 5 '17 at 11:18
  • yes, see below accepted answer. – Latheesan Oct 5 '17 at 12:01
  • could you please help @ magento.stackexchange.com/q/196004/57334 – zus Oct 5 '17 at 12:14
3

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

  • Ah man, all that effort and one silly typo ruins it. Thanks, spot on. – Latheesan Mar 2 '16 at 13:08
  • If it is solution you can mark as accepted – Prashant Valanda Mar 2 '16 at 13:11
  • It shows me this error, ' Column not found: 1054 Unknown column 'order.sales_executive' in 'field list' '. Any help? @PrashantValanda – Kazim Noorani Dec 9 '16 at 11:56
  • Have you left join attribute in collection as in question – Prashant Valanda Dec 9 '16 at 12:25
  • I did the above thing. @PrashantValanda – Kazim Noorani Dec 9 '16 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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