How to Add a Customer Group ID Column to Magento’s Admin Order Grid
Adding a custom column to Magento’s admin order grid involves several steps, including module configuration, block override, and column definition. Let’s go through the process step by step.
Step 1: Configure Your Module
Your current app/etc/modules/MyProject_Adminhtml file is correctly configured:
<?xml version="1.0"?>
<config>
<modules>
<MyProject_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Sales />
</depends>
</MyProject_Adminhtml>
</modules>
</config>
This file tells Magento that your module is active and depends on the Mage_Sales module.
Step 2: Define Your Module Configuration
Your app/code/local/MyProject/Adminhtml/etc/config.xml looks almost correct. However, you need to include the <resource> tag for your module:
<?xml version="1.0"?>
<config>
<modules>
<MyProject_Adminhtml>
<version>1.0.0</version>
</MyProject_Adminhtml>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>MyProject_Adminhtml_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
<resources>
<myproject_adminhtml_setup>
<setup>
<module>MyProject_Adminhtml</module>
</setup>
</myproject_adminhtml_setup>
</resources>
</global>
</config>
Step 3: Override the Sales Order Grid Block
In your app/code/local/MyProject/Adminhtml/Block/Sales/Order/Grid.php, you have correctly overridden the Mage_Adminhtml_Block_Sales_Order_Grid class:
class MyProject_Adminhtml_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
// ... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
$this->addColumn('customer_group_id', array(
'header' => Mage::helper('sales')->__('Customer Group Id'),
'index' => 'customer_group_id',
'type' => 'text',
));
// ... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
}
}
You have added a new column named customer_group_id with the appropriate configuration. However, Magento doesn’t recognize this new column because you haven’t updated the database schema.
Step 4: Update the Database Schema
To update the database schema, run the following command in the root directory of your Magento installation:
php -f bin/magento setup:upgrade
This command will run all the upgrade scripts defined by all installed modules. In this case, it will execute the setup script for your MyProject_Adminhtml module.
Step 5: Verify Your Changes
After running the upgrade command, clear the cache:
php -f bin/magento cache:flush
Then, refresh the sales order grid in the admin panel. You should now see a new column named “Customer Group Id” displaying the customer group IDs for each order.
Conclusion
Adding a custom column to Magento’s admin order grid involves several steps that require a good understanding of Magento’s architecture and module development processes. By following this guide, you can easily extend the functionality of the admin panel to meet your specific business requirements.
Remember, when working with core files like controllers and blocks, it’s always best practice to create a custom extension or use Magento 2 for better performance and security features. You can find more detailed guides on Laravel Company to help you further customize your Magento store.
Happy coding!