Magento: Display Customer Lifetime Sales Stats on Order View Pages
As an e-commerce developer, you often need to extend the functionality of your Magento store to better serve your customers’ needs. A common request is to display key information about the customer’s purchasing history directly in the order view page. In this post, we’ll guide you through adding a customer’s lifetime sales and total orders count to the Order View screen.
Why Display Lifetime Sales and Total Orders?
Providing a quick overview of a customer’s spending habits and order frequency right from the Order View page can help your admin team:
- Identify VIP customers: Easily spot high-value customers who deserve special treatment.
- Optimize support: Quickly understand a customer’s purchasing history to handle inquiries more efficiently.
- Analyze trends: Gain insights into changing customer behavior over time.
Steps to Add Lifetime Sales and Total Orders
1. Find the Right File
Navigate to the app/design/adminhtml/default/default/template/sales/order/view/info.phtml file, where you can add your custom rows.
app/
âââ design/
âââ adminhtml/
âââ default/
â âââ theme.xml # Theme configuration
â âââ default/
â âââ template/
â âââ sales/
â âââ order/
â âââ view/
â âââ info.phtml <-- This is the file we need
2. Add Your Rows
Insert your custom rows just below the existing loop that displays customer account data.
<!-- Before -->
<?php foreach ($this->getCustomerAccountData() as $data): ?>
<tr>
<td class="label"><label><?php echo $data['label'] ?></label></td>
<td class="value"><strong><?php echo $data['value'] ?></strong></td>
</tr>
<?php endforeach; ?>
<!-- After -->
<tr>
<td class="label"><label>Lifetime Sales:</label></td>
<td class="value" id="customer_lifetime_sales"></td>
</tr>
<tr>
<td class="label"><label>Total Orders:</label></td>
<td class="value" id="customer_total_orders"></td>
</tr>
3. Fetch and Display the Data
Your current customer is available via $this->getOrder()->getCustomerId(). To fetch lifetime sales, you can use this query:
<?php $lifetimeSales = Mage::getModel('sales/order')->getCollection()
->addFieldToSelect(array('entity_id', 'grand_total'))
->addFieldToFilter('customer_id', $this->getOrder()->getCustomerId())
->setPageSize(1)
->getFirstItem();
$totalLifetimeSales = number_format($lifetimeSales->getGrandTotal(), 2);
?>
For total orders, use this query:
<?php $orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('customer_id', $this->getOrder()->getCustomerId());
$totalOrders = count($orderCollection);
?>
4. Update the Rows
Finally, assign your calculated values to the row elements using JavaScript. Add this script at the end of your info.phtml file:
<script type="text/javascript">
document.getElementById('customer_lifetime_sales').innerHTML = '<?php echo $totalLifetimeSales; ?>';
document.getElementById('customer_total_orders').innerHTML = '<?php echo $totalOrders; ?>';
</script>
Conclusion
In this post, we demonstrated how to extend the Magento Order View page with additional customer insights. By following these steps and using our example code, you can now display a customer’s lifetime sales and total orders directly on the order view screen.
Remember, while customizing core files (like info.phtml), always follow best practices for maintainability:
- Create a local copy of this file in your theme folder.
- Use theme inheritance to apply changes only once.
- Test thoroughly before deploying.
For more advanced customizations and insights into Magento development best practices, check out our detailed guides on Magento Development at Laravel Company.