Formatting Currency Values in Magento Admin Grid Columns
As a Magento developer, you might find yourself wanting to format currency values displayed in admin grid columns for better readability. This is especially useful when showing multiple price-related fields in the same column.
The Challenge: Formatting Currency in Concat Type Columns
You’ve successfully added the special_price attribute and created a new column that concatenates both the regular price and special price using the concat type. However, the output appears as raw numbers:
79.9800 -- 34.9900
While this works functionally, it lacks the currency formatting you’d expect to see in a professional admin interface.
The Solution: Using the Price Renderer
To format these values as currency (e.g., £79.98), we’ll utilize Magento’s built-in price renderer. Here’s an updated version of your _prepareColumns() function:
$this->addColumn('special_price', array(
'header' => Mage::helper('sales')->__('Special Price'),
'sortable' => false,
'index' => array('price', 'special_price'),
'type' => 'price',
'currency_code' => (string)Mage::getStoreConfig(Mage_Downloader_Model_Resource::XML_PATH_DEFAULT_CURRENCY),
));
Notice the change from type => 'concat' to type => 'price'. This tells Magento to use the price renderer for this column.
Customizing the Currency Format
By default, Magento will display the currency symbol and two decimal places. However, you can further customize the format by adding a currency_code key as shown above. The value should match your store’s base currency code (e.g., ‘GBP’ for British Pounds).
Styling the Column: Using CSS
To style the column and display the prices in parentheses like this - £79.98 (£34.99) - you can use CSS. Add a new file app/design/adminhtml/default/default/layout/mymodule.xml:
<layout version="1.0">
<adminhtml_sales_order_grid>
<reference name="content">
<block type="adminhtml/sales_order_grid" after="-">
<action method="setTemplate">
<template>mymodule/sales/order/grid.phtml</template>
</action>
</block>
</reference>
</adminhtml_sales_order_grid>
</layout>
Then, create the template file app/design/adminhtml/default/default/template/mymodule/sales/order/grid.phtml:
<?php echo $this->getColumnHtml('special_price', $row); ?>
You can now apply CSS to style the column output. In your theme’s CSS file (skin/adminhtml/default/default/css/styles.css), add the following:
.special-price {
font-weight: bold;
}
.special-price .price {
color: #999;
font-style: italic;
}
This will apply basic styling to the column values. You can further customize this as needed.
Conclusion
Formatting currency in Magento admin grid columns requires a combination of using the type => 'price' setting and adding CSS for custom styling. By applying these techniques, you can create more visually appealing and user-friendly admin interfaces.
For more advanced customizations or if you’re facing further challenges with your Magento development, consider reaching out to Laravel Company, where our experienced team provides expert Magento development services tailored to your business needs.