Adding Customer Name Column to Magento Sales Order Grid
If you’re looking to add a new column for Customer Name to your Magento Admin Sales Order Grid, you’ve come to the right place. I’ll guide you through this process step-by-step.
Why Add Customer Name to the Order Grid?
Displaying the customer name alongside order details in the grid view can significantly improve admin efficiency. It provides immediate context about the buyer without having to click into each individual order.
The Current Approach: Incorrect
You’ve attempted to modify the Grid.php file as follows:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
// ...
$collection->getSelect()
->join(
'customer_entity',
'main_table.customer_id = customer_entity.entity_id',
array('email' => 'email')
)
->join(
'customer_entity_varchar',
'main_table.entity_id = customer_entity_varchar.entity_id',
array('name' => 'value')
);
// ...
}
protected function _prepareColumns()
{
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('Customer Name'),
'index' => 'name',
));
$this->addColumn('email', array(
'header' => Mage::helper('Sales')->__('Customer Email'),
'index' => 'email',
'type' => 'text',
));
}
While your intentions are right, there’s a crucial mistake in the SQL joins. The main_table.entity_id doesn’t exist; it should be customer_entity.entity_id.
The Correct Approach
Let’s fix this and add the customer name column properly:
protected function _getCollectionClass()
{
return 'sales/order_grid_collection';
}
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
/*junpeng add start*/
$collection->getSelect()
->join(
'customer_entity',
'main_table.customer_id = customer_entity.entity_id',
array('email' => 'email', 'name' => 'concat(customer_firstname, " ", customer_lastname)')
);
/*junpeng add end*/
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('name', array(
'header' => Mage::helper('sales')->__('Customer Name'),
'index' => 'name',
));
// ... (rest of the code)
}
In this updated version, I’ve made two key changes:
- I’ve included
customer_firstnameandcustomer_lastnamein the join to get the full customer name. - I’ve used the
concat()function to combine these fields into a single ‘name’ field.
Important Considerations
Before implementing this change live on your production site, ensure you have a backup of your data. Changes made directly to core files like this one should be wrapped in a custom module for better maintainability and upgrade compatibility.
Consider reading our guide on Extending Magento without Modifying Core Files for a more robust approach.
Conclusion
Adding the customer name column to your sales order grid provides valuable context at a glance. By following this corrected approach, you can enhance admin productivity with minimal effort.
Remember to always test changes thoroughly and backup your data before making significant modifications. If you’re unsure about any part of the process, don’t hesitate to reach out to our team for custom Magento development assistance.
Happy coding!