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 order_uuid column. Search for_prepareCollection function and simply add this line:
$collection->getSelect()
->joinLeft(
"sales_flat_order",
"main_table.increment_id = sales_flat_order.increment_id",
array('order_uuid'=>'sales_flat_order.order_uuid')
);
Before
$this->setCollection($collection);
return parent::_prepareCollection();
Then,importantly,change return parent::_prepareCollection();
to
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
so function will look like:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()
->joinLeft(
"sales_flat_order",
"main_table.increment_id = sales_flat_order.increment_id",
array('order_uuid'=>'sales_flat_order.order_uuid')
);
//Mage::log($collection->getSelect()->__toString());
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
As you can see, we have joined the sales_flat_ordertable that stores theorder_uuid 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 order_uuid column.
/* add a colomn */
$this->addColumn('order_uuid', array(
'header'=> Mage::helper('sales')->__('Order Uuid'),
'width' => '80px',
'type' => 'text',
'index' => 'order_uuid',
'sortable' => true,
'is_system' => false,
));
That’s it.
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!