How can I add a column to the grid in the Admin section under Sales > Orders?
4 Answers
Inchoo wrote a great article on extending the order grid. And on the Atwix blog there's an article on adding a column from a different table.
The Inchoo blog post basically extends it with a custom extension. In case you're not familiar with writing your own extension I'd suggest you take a loot at Alan Storms tutorial or the one from Magento 4U.
You will be rewriting the Mage_Adminhtml_Order_Grid
block class adding a column to the _prepareColumns
method and extending the _prepareCollection
method with your custom field
Its a 2 step simple process. (for ex. i want to add Email Address and Order City in this order grid).
Copy this core file to you local first in same directory. app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php
Step 1 : Add following line of code in function _prepareColumns()
$this->addColumn('email', array(
'header' => Mage::helper('catalog')->__('Customer Email'),
'index' => 'email',
'type' => 'text'
));
$this->addColumn('city', array(
'header' => Mage::helper('catalog')->__('Order From City'),
'index' => 'city',
'type' => 'text'
));
Step 2 : Add following line of code in function _prepareCollection()
$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id' ,array('email'=> 'email', 'city'=> 'city' ));
$collection->addFieldToFilter('sales_flat_order_address.address_type', array('eq' => 'billing'));
The overall query will be like:
SELECT main_table
.*, sales_flat_order_address
.email
AS customer_email
, sales_flat_order_address
.city
FROM sales_flat_order_grid
AS main_table
INNER JOIN sales_flat_order_address
ON main_table.entity_id = sales_flat_order_address.parent_id WHERE (sales_flat_order_address.address_type = 'billing')
Modify the code according to your need.
Hope that helps.
You can use my answer from a previous post on Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue to add extra columns to the Sales Order grid thru an observer.
Rewrites are so yesterday ;)
You can do this using events. See a previous answer here:
Add Company column to Customers Admin Grid with Observer
which also includes a way to adjust the filters.
You simply need to adjust the grid you are targeting in the first bit of code:
if ($event->getBlock() instanceof
Mage_Adminhtml_Block_Newsletter_Subscriber_Grid
) {