WPCS - WordPress Currency Switcher Professional

WordPress currency switcher base currency: how prices are converted

The base currency of the WordPress currency switcher is the currency you write your prices in. WPCS converts every price from it: an amount in the base currency multiplied by the exchange rate of the currency the visitor has selected. Most questions about wrong amounts come from three things: rates entered the wrong way round, prices written in another currency, and confusing the base currency with the currency visitors see first. Here is how the base currency works, how to price in another currency anyway, and what happens when you change it.

What the base currency is in the currency switcher

Open Settings → Currency Switcher, tab Currencies. Each currency row starts with a radio button; the selected one marks the base currency. Its tooltip says what it means: this should be the currency in which your prices are written. There is always exactly one.

The base currency has the rate 1. The rate of every other currency says how many units of that currency equal one unit of the base currency. With INR as the base, the USD rate is about 0.012 (one rupee is 0.012 dollars), not 83. A rate typed the other way round makes the converted prices thousands of times too high.

When a page is built, the plugin calculates amount × rate for the visitor's currency, where the value of the interest field next to the rate is added to the rate. Amounts in the base currency itself are shown as written, without any calculation. With INR as the base and [wpcs_price value=8300] on a page:

Visitor's currencyRateShown
INR (base)18,300.00
USD0.01299.60
EUR0.01191.30

Symbols, decimals and separators come from each currency's row; the arithmetic is always the same.

Write each price once, in the base currency

[wpcs_price value=200] has no attribute for a currency: the value is always read as an amount in the base currency. The same holds for amounts read from custom fields with meta_value and for amounts converted in PHP with $WPCS->wpcs_exchange_value(). You set every price once; when the rates are updated, all converted prices follow, and nothing has to be entered again.

So choose as the base the currency you think and invoice in. If your price list is in rupees, the base is INR and every value is in rupees, even if most visitors will see dollars.

Prices written in another currency

There is no way to tell [wpcs_price] that one particular amount is in euros while the base is US dollars. [wpcs_price value=250 EUR] is read as 250 in the base currency, and the EUR is ignored. Three ways to handle such prices:

  • Your own amount for every currency. If the price should be exactly 250 euros, 270 dollars and 215 pounds, use [wpcs_price type=fixed value=EUR:250,USD:270,GBP:215]. Nothing is converted, but every currency of the site needs an amount; see fixed prices per currency.
  • Convert the amount to the base yourself and write that number. Simple, but the base amount does not follow later rate changes of the original currency.
  • Let a small shortcode do the conversion on every page view, with the current rates.

The shortcode below takes an amount in any currency of your list, converts it to the base currency with $WPCS->back_convert() (amount divided by the rate) and passes the result to [wpcs_price]:

/*
 * [price_from currency=EUR value=250]
 * The value is in the given currency; WPCS shows it in the visitor's currency.
 * Child theme functions.php or a code snippets plugin.
 */
add_shortcode('price_from', function ($atts) {
    global $WPCS;
    $atts = shortcode_atts(array('currency' => '', 'value' => 0), $atts);
    if (empty($WPCS)) {
        return '';
    }
    $amount     = (float) $atts['value'];
    $code       = strtoupper($atts['currency']);
    $currencies = $WPCS->get_currencies();
    if (isset($currencies[$code]) && $code !== $WPCS->default_currency) {
        // the same amount in the base currency, at today's rate
        $amount = $WPCS->back_convert($amount, $currencies[$code]['rate'], 6);
    }
    return do_shortcode('[wpcs_price value=' . $amount . ']');
});

Visitors who selected EUR see exactly 250 euros; everybody else, including visitors in the base currency, sees 250 euros converted at the current rates. The output is a normal [wpcs_price] price, so it is also redrawn correctly on sites with I am using cache plugin on my site enabled.

Changing the base currency

Click the radio button of another currency on the Currencies tab. The plugin saves the new base at once, asks the aggregator selected on the Options tab for the rates of all other currencies against it, and puts them into the rate fields. Check them and press Save options right away: until you save, the site uses the new base currency with the old rates.

If a rate field shows an error instead of a number, the aggregator could not deliver it; the free plans of some rate services accept only one base currency. Exchange rate errors explains the messages, and rates can always be set manually.

What does not change is your prices. Every value=200 written while the base was USD now means 200 of the new base currency. Decide on the base before you write prices; if you change it later, edit every converted price (searching the Posts and Pages lists for wpcs_price finds them in the content). Fixed prices with type=fixed, GeoIP rules and the welcome currency are not affected.

Base currency, welcome currency and current currency

These three are independent, and only the first one concerns how you enter prices:

CurrencyWhat it decidesWhere it is setIn PHP
BaseThe currency of every amount you writeRadio button on the Currencies tab$WPCS->default_currency
WelcomeWhat a first-time visitor sees when no GeoIP rule matchesWelcome currency on the Options tab$WPCS->get_welcome_currency()
CurrentWhat this visitor sees nowThe visitor's choice in the switcher, GeoIP rules or the welcome currency$WPCS->current_currency

Your prices can be in INR while new visitors start in USD: set INR as the base and USD as the welcome currency. Choosing the first currency by country is covered in set currency by visitor country. In your own code, read the base with $WPCS->default_currency; the rates come from $WPCS->get_currencies(), and on the front end they already include the interest.

Checklist

  • The radio button is on the currency your prices are written in, and its rate is 1.
  • Every other rate is "units of that currency per one unit of the base", not the reverse.
  • Every value and every price field holds a plain number in the base currency.
  • Prices that must be exact in another currency use type=fixed or the [price_from] shortcode.
  • After a change of the base currency: rates checked, Save options pressed, converted prices re-entered.

If the amounts still do not add up, open a topic on the WPCS support forum with your base currency, the rates from the Currencies tab and the shortcode you use.