Changing Currency Symbol Format in Magento
If you’re looking to modify how currency symbols are displayed in your Magento store, specifically needing to change from US$50 to $50(USD), this guide will walk you through the process for both Magento 1.5 and the upcoming upgrade to 1.7.
Understanding the Currency Symbol Format
In Magento, the default format for displaying currency symbols is set in the system configuration under System > Configuration > General > General. The symbol is determined by the country selected for the base currency.
For example:
- USD: $ (US Dollar)
- EUR: ⬠(Euro)
Customizing Currency Display Format
To achieve your desired format of $50(USD), you’ll need to create a custom extension that overrides the core template files responsible for rendering prices. Here’s a step-by-step guide:
-
Create a new module: In your Magento root directory, navigate to
app/codeand create a new folder under the vendor namespace (e.g.,app/code/local/Vendor/CurrencyFormat). -
Implement the observer: Create an XML configuration file
etc/config.xml:
<config>
<modules>
<Vendor_CurrencyFormat>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Core />
</depends>
</Vendor_CurrencyFormat>
</modules>
</config>
- Create the observer: In
app/code/local/Vendor/CurrencyFormat/etc, create an events.xml file:
<events>
<catalog_product_get_final_price>
<observers>
<custom_format_currency_observer>
<type>singleton</type>
<class>Vendor_CurrencyFormat_Model_Observer</class>
<method>formatCurrency</method>
</custom_format_currency_observer>
</observers>
</catalog_product_get_final_price>
</events>
- Implement the observer class: Create
app/code/local/Vendor/CurrencyFormat/Model/Observer.php:
class Vendor_CurrencyFormat_Model_Observer extends Mage_Core_Model_Abstract
{
public function formatCurrency(Varien_Event_Observer $observer)
{
$product = $observer->getProduct();
$price = $product->getFinalPrice();
if ($product->getStoreId() == 2) { // Assuming your website ID is 2
return "$" . number_format($price, 2) . "(" . Mage::app()->getLocale()->currency(Mage_Directory_Model_Currency::NO_CURRENCY)->getCurrencySymbol() . ")";
}
return $product->getFinalPrice();
}
}
Upgrading to Magento 1.7
If you’re planning an upgrade to Magento 1.7, the process remains largely the same. However, Magento 1.7 introduces the concept of layout XML files for price display.
To apply your custom formatting in version 1.7:
- Create a new
app/design/frontend/default/your_package/layoutdirectory if it doesn’t exist. - Add the following XML to a new file named
catalog.xml:
<layout version="0.1.0">
<default>
<reference name="head">
<action method="addItem" xsi:type="itemType"><type>skinjs</type><name>custom_price_format.js</name></action>
</reference>
<reference name="catalog_product_view">
<action method="setTemplate" xsi:type="string">catalog/product/price.phtml</xsi:type>
<!-- Define the template file for your custom price rendering -->
</reference>
</default>
</layout>
- Create a new JavaScript file named
custom_price_format.jsinapp/design/frontend/default/your_package/js:
function formatCurrency(price) {
return "$" + price.toFixed(2) + "(" + getCurrencySymbol() + ")";
}
function getCurrencySymbol() {
var currency = document.getElementById('currencies');
if (currency) {
return currency.value;
}
return '';
}
Conclusion
Changing the currency symbol format in Magento requires a custom extension to override the core template files responsible for rendering prices. By following this guide, you should now be able to display your product prices as $50(USD) on your website with ID 2.
Remember to replace Vendor, CurrencyFormat, and your_package with your actual vendor name and package structure.
For more advanced customizations, consider using Magento’s extension development tools or contacting our experts at Laravel Company.
Happy coding!