Removing Decimal Places in Currency Formatting for Magento Admin Attributes
If you’re developing an e-commerce site using Magento and dealing with currencies that don’t have decimal places like the Japanese Yen (Â¥), you might find yourself needing to remove the trailing “.00” from prices displayed in admin interfaces, especially when it comes to configurable product attributes.
The Default Currency Formatting Issue
By default, Magento’s core currency formatting logic treats all currencies as having a fixed number of decimal places. For most currencies like USD or EUR, this is fineâyou want to display the prices with cents (or fractions of a cent). However, for currencies without decimals like the Japanese Yen, this leads to undesired output.
Modifying the Core Currency Model
The core currency formatting is handled in app/code/core/Mage/Directory/Model/Currency.php. In the format function, you can modify the logic to handle specific locales differently. Here’s how you can adjust it for Japanese Yen:
public function format($price)
{
$locale = Mage::app()->getLocale()->getLocaleCode();
if ($locale != 'ja_JP') {
return $this->formatPrecision($price, 2, array(), true, null);
} else {
// Remove decimal for Japanese Yen
$formattedPrice = number_format($price, 0, '', '');
return Mage::helper('directory')->currencyFormat($formattedPrice, false, true);
}
}
This modification checks if the locale is ‘ja_JP’. If so, it removes all decimals from the price using number_format and then formats it again through Magento’s currency helper.
The Attribute Price Display Problem
While this solves the problem for product pages and carts, you might still see “.00” at the end of prices in your admin dropdown attributes. This is because Magento is pulling the price directly from the database without applying your custom formatting logic.
Solving the Admin Dropdown Issue
To fix this, you need to modify how Magento fetches and displays these prices in the admin interface. You’ll want to override the core Mage_Eav_Model_Entity_Attribute_Source_Configurable model.
Create a new file at app/code/local/Your_Namespace/Your_Module/Model/Source/Configurable.php. Extend the core model, and override the _getOptionText method:
public function _getOptionText($value)
{
$currency = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode());
return ($this->isMultiselect() ? $currency->format($this->_getOptionValue($value)) : $currency->format($value));
}
In this overridden method, you’re formatting the option value using the current store’s currency before returning it. This ensures that Magento applies your custom currency formatting rules when displaying attribute options in the admin interface.
Remember to Reindex and Flush Cache
After making these changes, remember to reindex and flush all caches:
php indexer.php --reindexall
rm -rf var/cache/*
This will ensure your modifications take effect across the entire site, both front-end and back-end.
Conclusion: A Clean Solution for Decimals in Admin Attributes
By modifying the core currency model to handle Japanese Yen specifically and overriding the configurable attribute source model, you can achieve a clean solution that removes decimals from prices displayed in both the admin interface of your Magento store and the customer-facing parts of the site. This approach keeps your database intact while ensuring consistent currency formatting across all areas.
Remember, when working with Magento core files, it’s always best to use a local copy so you don’t lose any changes during updates. If you’re comfortable with version control systems like Git, consider using them for your development environment.
For more advanced customizations and extensions in Magento, I recommend checking out the resources available at Laravel Company. They offer comprehensive guides and tutorials to help you get the most out of this powerful e-commerce platform.