Adding Customer Order Metrics to Magento Order Grid (1.7)
As an experienced Magento developer, I understand your frustration when trying to extend default functionality like the order grid. In this post, we’ll walk through how to add “Total Number of Orders” and “Total Spent” columns for customers in the order grid for Magento 1.7.
Understanding the Requirement
You want to display two additional columns in the order grid:
- Total Number of Orders: The count of all orders placed by a customer.
- Total Amount Spent: The sum of “grand total” amounts from all orders placed by a customer.
To achieve this, we’ll need to create a custom module and override the Mage_Adminhtml_Block_Sales_Order_Grid class.
Step 1: Create a Custom Module
First, let’s create a new module called “CustomerOrderMetrics”:
app
âââ code
âââ local
â âââ Mage
â âââ CustomerOrderMetrics
â âââ etc
â â âââ config.xml
â âââ Block
â âââ Adminhtml
â âââ Sales
â âââ Order
â âââ Grid.php
The config.xml file should look like this:
<?xml version="1.0"?>
<config>
<modules>
<Mage_CustomerOrderMetrics>
<version>1.0.0</version>
</Mage_CustomerOrderMetrics>
</modules>
<global>
<blocks>
<customerordermetrics>
<class>Mage_CustomerOrderMetrics_Block</class>
</customerordermetrics>
</blocks>
</global>
</config>
Step 2: Override the Order Grid Block
Next, let’s override the Grid.php file:
<?php
class Mage_CustomerOrderMetrics_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid {
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->join('sales/order_item', 'main_table.entity_id=order_item.order_id');
$collection->groupByAttributeName('customer_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns() {
$this->addColumn('total_orders', array(
'header' => Mage::helper('sales')->__('Total Orders'),
'index' => 'customer_id',
'renderer' => 'Mage_CustomerOrderMetrics_Block_Sales_Order_Grid_Renderer_TotalOrders'
));
$this->addColumn('total_amount_spent', array(
'header' => Mage::helper('sales')->__('Total Amount Spent'),
'index' => 'customer_id',
'renderer' => 'Mage_CustomerOrderMetrics_Block_Sales_Order_Grid_Renderer_TotalAmountSpent'
));
return parent::_prepareColumns();
}
}
Step 3: Create the Renderer Classes
Finally, let’s create two renderer classes for our columns:
TotalOrders.php:
class Mage_CustomerOrderMetrics_Block_Sales_Order_Grid_Renderer_TotalOrders extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row) {
return $this->getCount($row->getId());
}
private function getCount($customerId) {
// Count all orders for the customer
// Example: Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id', $customerId)->count();
}
}
TotalAmountSpent.php:
class Mage_CustomerOrderMetrics_Block_Sales_Order_Grid_Renderer_TotalAmountSpent extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
public function render(Varien_Object $row) {
return $this->getTotal($row->getId());
}
private function getTotal($customerId) {
// Sum the grand total of all orders for the customer
// Example: Mage::getResourceModel('sales/order')->getCollection()->addFieldToFilter('customer_id', $customerId)->getColumnValues('grand_total');
}
}
Conclusion
Adding custom columns to the Magento order grid requires a solid understanding of block overrides and renderer classes. In this post, we’ve created a complete solution for adding “Total Number of Orders” and “Total Amount Spent” columns.
Remember, Magento 1 is end-of-life as of June 2022. To ensure long-term security and stability, consider migrating to the latest version or using a platform like Laravel Company’s Laravel eCommerce framework for new projects Learn More.
For more advanced customizations in your Magento store, feel free to explore our other blog posts on best practices for Magento development.