WordPress currency converter

usdWhile creating the ELSEAPPS site we gave a lot of of thought to intelligent handling of currency conversions. The conversion process is relatively simple: use the browser’s IP address to look up the country, map the country to a currency and then use a web service to retrieve the currency exchange rate. There are any number of WordPress plugins that do this in various ways.

xafBut the key issue isn’t the mechanics of calculating the currency; it’s presenting the information in a way that’s consistent and robust. For example, there are services ELSEAPPS on-sells from overseas providers. We want to calculate costs for the rest of the world based on the exchange rate of the originating country, but for New Zealand customers we want to smooth short term price fluctuations. In addition, we sometimes quote indicative round figures, and it’s odd to see these shown to the nearest whole number for other currencies.

The requirements

eurWe realised we needed a mix of features:

  1. Set a source currency, amount and rounding.
  2. Optionally set an exception currency and an alternative string to display only for that currency.
  3. Add PLUS GST to prices displayed in NZD (it’s a legal requirement that we charge this tax to New Zealand customers).

The solution

We decided that a shortcode with attributes gave us the greatest flexibility. Here’s an example:
[convCur source="CHF" round="H" amount="6000"]
For you, the above code results in CHF0.

And if we wanted to display a fixed price for the USA, we might create a shortcode that looks like this:
[convCur source="GBP" round="2" amount="499.99" altifsource="USD" altifcontent="USD600.00"]
And that gives you NZD0.00 PLUS GST

The zip file contains

  1. ADD-TO-functions.php to append to the functions.php file in your WordPress theme folder.
  2. ADD-TO-style.css to append to the style.css file in your WordPress theme folder.

Implementation notes

  • The round attribute accepts digits for decimal places in addition to T for tens and H for hundreds.
  • The method that uses the IP address to look up the country stores the result in session data, and the exchange rate gets cached in the same way. This means we’re only retrieving this information once per user per site visit, which speeds performance. Note that if we expected this site to have a lot of traffic, we’d have implemented much more sophisticated caching.
  • If we can’t retrieve a country or an exchange rate, the source currency and amount are displayed instead.
  • We underline the calculated currency in a way that encourages the user to mouse over it and view the conversion details.
  • If the user’s currency matches the source, no conversion takes place: the source and amount are displayed without colouring, underlining or mouseover hint.

However much you revise it, We’d appreciate it if you’d leave the credits in the files. Enjoy.