Adding a custom attribute/column to the orders grid in Magento admin

Does your Magento order grid have all of the fields you need? Adding or removing columns is fairly simple. Let’s explore how.
Here we will discuss both:
– Adding a custom attribute/column to the order grid.
– Removing a column that you don’t need.

The grid is under Sales -> Orders.

Magento 1 Admin Panel. Sales - srcset= Orders” width=”1024″ height=”317″>

We are going to first copy the core file to a local directory, then code a bit.
Copy /app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php to /app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php. First, to make custom attribute available for inserting to grid we need to modify _prepareCollection function.
Here, we are working with database table fields and using SQL joins. Let`s suppose that we need to add a postcode column. Search for _prepareCollection function and simply add this line:

$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('postcode'));

before

return parent::_prepareCollection();

so function will look like:

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
        $collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('postcode'));
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

As you can see, we have joined the sales_flat_order_address table that stores the postcode field. To insert this column into the order grid, edit _prepareColumns() function. Here you can see how default columns are inserted, so using the addColumn method, we add our own postcode column.

$this->addColumn('postcode', array(
            'header' => Mage::helper('sales')->__('Postcode'),
            'index' => 'postcode',
        ));

That’s it.
Magento orders grid - custom column
Similarly, for removing a field, you just need to delete the corresponding portion of code.
A few more advanced examples are provided below.

Making a new custom Address column that consists of country, city and street:

$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('telephone','postcode','address'=>new Zend_Db_Expr('concat(sales_flat_order_address.country_id, ", ",sales_flat_order_address.city, ", ",sales_flat_order_address.street)')) );

Product SKUs of an order:

$collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus'  => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')));

The last, is an example of adding custom product attributes. It stores at catalog_product_entity_varchar table and we need to know its attribute_id:

$collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id AND `sales_flat_order_item`.parent_item_id IS NULL  ', null);
$collection->getSelect()->join('catalog_product_entity_varchar', '`catalog_product_entity_varchar`.attribute_id=144 AND `catalog_product_entity_varchar`.entity_id = `sales_flat_order_item`.`product_id`', array('models'  => new Zend_Db_Expr('group_concat(`catalog_product_entity_varchar`.value SEPARATOR ",")')));

Remember to look at the database and see which tables store the necessary attributes.
I hope this article was helpful for you!
Update. It is recommended to avoid rewriting and overring core classes. Take a look at two our articles that describe better way of adding columns to Magento admin grids: Adding a column to Magento orders grid – alternative way using layout handles and Adding a column to the customers grid in Magento admin. Alternative way.

You may also want to read: