Magento 1 – currency rates
A not very common task for Magento developers is to work with currency rates. I had previously my first contact with it. For a multistore shop with many different currencies it was my task to create a module with sets prices for different currencies based on current rate from external resources.
Magento 1 – currency rates
You may only install a currency rate module and your individual currency prices are computed on the fly. You do not have to worry about rates and price computation. If you want to allow your customer to set ranges for currencies, this is more complex. A customer wants to set 3 ranges, where he can set min, max an default rates. So prices do not change every day.
Database
Currency rates are stored in directory_currency_rate table. This table may look like the following screenshot:
As you can see, our shop uses EUR as base currency and allows many other currencies like CHF, DKK, GBP, … The table shows the rate you need it you want to convert currency_from to currency_to. With this information, Magento converts 1 EUR into 7.44 DKK.
If you look into this table after a fresh Magento installation, you may discover, that there are only 4 lines for EUR to USD and USD to EUR conversion. You have to add other currencies by yourself.
Adding currencies
You can add all known currencies in Magento backend. Just go to Configuration -> General -> Currency Setup -> Currency Options. A multi select list allows to add every possible currency to you storeview. If you allow currencies, they are available to choose in frontend. All prices are recomputed by defined rates. But wait…which rates?
Manage and save rates
If you add currencies, they have by default not the correct rate. You see all rates here: System -> Manage Currency -> Rates
You see a grid of all base currencies (for me only EUR) and all available currencies for this shop. The grid can be read the following way:
1 EUR = 1.0695 CHF and 1EUR = 8.3391 HKD
You can Import current rates from a defined service (drop down box) and a click on import. Don’t forget to save new rates. There are many different extensions to get rates from different sources. All known extensions update rates by cron at night.
Programmatically get rates
If you want to work with currency rates in you code, you may find the following two methods useful. The first (getConfigAllowCurrencies) returns all active currencies for your shop. Based on this you can get all rates with getCurrencyRates().
1 2 | $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies(); $currencyRates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies)); |
The second param of getCurrencyRates() is an array of ISO 4217 currency codes and looks like this:
1 | array("AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", ...); |
Conclusion
I showed you detailed information on how to work with currency rates under Magento.