+359 884 629534 / +359 897 858176

Woocommerce – add a new currency and conversion rate

woocommerce

WooCommerce is a great plugin for creation of e-stores. Despite its rich functionality you can encounter some problems related with currency. These problems and their solution are described below:

1. Currency of a country which isn't in the list. How to add it?

2. How the currency units can be changed (example: лв) according to the newly added currency?

3. How can we make WooCommerce allow Paypal, although our currency is not supported?

1. If we don't find the desired currency in the dropdown list on the main settings page of WooCommerce, we need to add it. Let's say we need to add 'Bulgarian Lev (лв.)'. We can do this by adding the following code in functions.php of our theme.

add_filter( 'woocommerce_currencies', 'add_bgn_currency' );

function add_bgn_currency( $currencies ) {
 $currencies['BGN'] = __( 'Bulgarian Lev (лв.)', 'woocommerce' );
 return $currencies;
}

$currencies['BGN'] - instead of 'BGN' you can put another string. It's important to know, however, that from here onwards our currency will be identified with this string.

'Bulgarian Lev (лв.)' - this is the string we wish to appear in the dropdown list with the currencies. (For your information the filter is applied in woocommerce\admin\settings\settings-init.php)

2. Now we need to add currency units. We can add the following below the previous one:

add_filter('woocommerce_currency_symbol', 'add_bgn_currency_symbol', 10, 2);

function add_bgn_currency_symbol( $currency_symbol, $currency ) {
 switch( $currency ) {
 case 'BGN': $currency_symbol = 'лв.'; break;
 }
 return $currency_symbol;
}

case 'X' - we have declared $currencies['X'] in the previous code, where X in our case is BGN

$currency_symbol = 'лв.' - the currency symbol which we are going to use in our prices. (For your information the filter is applied in woocommerce\woocommerce-core-functions.php)

If the currency symbol appears before the price not behind it, and you wish this to change, this can be done form Admin Panel -> WooCommerce-> Settings-> Catalogue-> Price settings-> from the dropdown list 'currency sign position' we choose 'Right (with distance)'. This will make the price to look like '100 лв.'

3. he first problem we can encounter when we wish to use Paypal with the new currency is an error when we try to access the Paypal settings in the WooCommerce Admin Panel. To make Paypal available is the first thing to do. UPDATE: We use the next filter to add 'BGN' to the paypal valid currencies array:

add_filter( 'woocommerce_paypal_supported_currencies', 'add_bgn_paypal_valid_currency' );     
    function add_bgn_paypal_valid_currency( $currencies ) {  
     array_push ( $currencies , 'BGN' );
     return $currencies;  
    } 

(For information the filter is applied in woocommerce/classes/gateways/paypal/class-wc-paypal.php)

Another problem is that Paypal doesn't work with BGN. Then it will be good if before the payment we can transform the sums in a Paypal supported currency, for example euro. We can add the following filter after the rest:

add_filter('woocommerce_paypal_args', 'convert_bgn_to_eur');
function convert_bgn_to_eur($paypal_args){
	if ( $paypal_args['currency_code'] == 'BGN'){
		$convert_rate = 1.954; //set the converting rate
		$paypal_args['currency_code'] = 'EUR'; //change BGN to EUR
		$i = 1;

		while (isset($paypal_args['amount_' . $i])) {
			$paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
			++$i;
		}

	}
return $paypal_args;
}

In the code we check if $paypal_args['currency_code'] == 'X', where X was initialized in $currencies['X'] from point 1. In our case X is BGN.But 'BGN' isn't supported by Paypal, that's why we change it to 'EUR'. The rest is currency transformation of the sums to pay from leva to euro. (For your information the filter is applied in woocommerce\classes\gateways\paypal\class-wc-paypal.php)

If the information above was helpful to you, please share it in the social networks by using the buttons below the article.

 
The following two tabs change content below.

svetto

SEO expert at Devseon
Svetoslav Stoyanov is an SEO expert at Devseon. He has worked for many companies in USA and Europe and has gained a vast experience in optimizing web sites and reaching top positions in the major search engines.

46 Responses to Woocommerce – add a new currency and conversion rate

  1. Instead of modifying WooCommerce core file, use the following function for modifying the valid currency array
    add_filter(‘woocommerce_paypal_supported_currencies’, ‘mRova_add_currency_in_paypal’);
    function mRova_add_currency_in_paypal($array){
    return array_push($array, ‘BGN’)
    }

    Reply
  2. On line 4 in example 1 you should be using your theme’s own namespace for internationalization, instead of “woocommerce”.

    In regards to plugin modification — imho this is absolutely not acceptable. Besides — the list of currencies in the is_valid_for_use() function lists all currencies that are supported by PayPal. The limitation does not come from WooCommerce, rather it comes from PayPal.

    Reply
    • Bader says:

      Hi Georgi
      I followed the code above as-is … added to the child theme functions.php (not working)
      also, i changed line 4 from ‘woocommerce’ to ‘my theme name’ also not working (also to the child theme)
      what can possibly be wrong?
      I’m totally open to any suggestions
      Thanks

      Reply
  3. Our friend Georgi, always here to offer help :). Under the section “Paypal valid currencies alternative” I offered a way that doesn’t need plugin modification. This is the way I prefer. If you can offer a better way to enable Paypal with the new currency you are welcome to present it.

    Reply
  4. Thanks for this tutorial! I chose the second option using the ‘TRY’ currency rather than changing the core php file. It works very well. I am using Trinidad & Tobago Dollars.

    For other interested persons, here’s the actual code I inserted in my theme’s functions.php file. Note in step 3 that the $convert_rate should be the rate you multiply the USD value by to arrive at your local currency.

    ————————————————————
    /*Step 1 – Code to use ‘TRY’ (Turkey Lira) currency to display Trinidad & Tobago Dollars in WooCommerce:*/

    add_filter( ‘woocommerce_currencies’, ‘add_custom_currency’ );

    function add_custom_currency( $currencies ) {
    $currencies["TRY"] = ‘Trinidad & Tobago Dollar’;
    return $currencies;
    }

    /*Step 2 – Code to add Trinidad & Tobago currency symbol in WooCommerce:*/

    add_filter(‘woocommerce_currency_symbol’, ‘add_custom_currency_symbol’, 10, 2);

    function add_custom_currency_symbol( $currency_symbol, $currency ) {
    switch( $currency ) {
    case ‘TRY’: $currency_symbol = ‘TT$’; break;
    }
    return $currency_symbol;
    }

    /*Step 3 – Code to change ‘TRY’ currency to ‘USD’ before checking out with Paypal through WooCommerce:*/

    add_filter(‘woocommerce_paypal_args’, ‘convert_try_to_usd’);
    function convert_try_to_usd($paypal_args){
    if ( $paypal_args['currency_code'] == ‘TRY’){
    $convert_rate = 6.45; //set the converting rate
    $paypal_args['currency_code'] = ‘USD’; //change TRY to USD
    $i = 1;

    while (isset($paypal_args['amount_' . $i])) {
    $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
    ++$i;
    }

    }
    return $paypal_args;
    }
    ————————————————————

    Reply
    • Hey Andy, I’m from Trinidad as well. I was wondering if you could help me out with this? I probably missed out something. Please email me at paulsamaroo@gmail.com when you get a chance. It would be nice to chat with you :)

      Reply
  5. Hi,

    I Like your code and used it for my clients website. it works fine. but i made some modification in it.

    1) step 3 adding code in woocommerce core file. i put this filter code in my template theme function.php. and its work fine. so no more core editing for conversion rate.

    2) you added manual corrancy rate. i update that with google exchange rate with some code. so it will take automatic code from google.

    Here is my Code :

    function convert_inr_to_usd($paypal_args){
    if ( $paypal_args['currency_code'] == ‘INR’){
    $convert_rate = get_exchange_rate(); //Set converting rate
    $count = 1;
    while( isset($paypal_args['amount_' . $count]) ){
    $paypal_args['amount_' . $count] = round( $paypal_args['amount_' . $count] / $convert_rate, 2);
    $count++;
    }
    }
    return $paypal_args;
    }

    add_filter(‘woocommerce_paypal_args’, ‘convert_inr_to_usd’);

    function get_exchange_rate($from=’USD’, $to=’INR’) {
    $url = “http://www.google.com/ig/calculator?hl=en&q=1%s=?%s”; //url for the currency convertor
    $result = wp_remote_retrieve_body($response = wp_remote_get(sprintf($url, $from, $to))); // fetches the result from the url
    if(is_wp_error( $response )) {
    return FALSE;
    }
    $result = explode(‘”‘,$result);
    $result = str_replace(chr(160), ”, substr( $result[3], 0, strpos($result[3], ‘ ‘) ) );
    return ( $result == 0 ) ? FALSE : $result;
    }

    Reply
    • Rashid says:

      I want same function with 2checkout instead of paypal. can you please edit the following code to me that when user will click on checkout button and page will redirect to 2checkout then currency will change in $ automatically.

      add_filter( ‘woocommerce_currencies’, ‘add_custom_currency’ );
      function add_custom_currency( $currencies ) {
      $currencies["PKR"] = ‘Pakistani Rupee’;
      return $currencies;
      }

      add_filter(‘woocommerce_currency_symbol’, ‘add_custom_currency_symbol’, 10, 2);
      function add_custom_currency_symbol( $currency_symbol, $currency ) {
      switch( $currency ) {
      case ‘PKR’: $currency_symbol = ‘Rs.’; break;
      }
      return $currency_symbol;
      }

      Reply
  6. Hi. Please help Error showing in my site (Fatal error: Cannot redeclare convert_inr_to_usd() (previously declared in )

    Please check below code

    function is_valid_for_use() {
    if ( ! in_array( get_woocommerce_currency(), apply_filters( ‘woocommerce_paypal_supported_currencies’, array( ‘AUD’, ‘BRL’, ‘CAD’, ‘MXN’, ‘NZD’, ‘HKD’, ‘SGD’, ‘USD’, ‘EUR’, ‘JPY’, ‘TRY’, ‘NOK’, ‘CZK’, ‘DKK’, ‘HUF’, ‘ILS’, ‘MYR’, ‘PHP’, ‘PLN’, ‘SEK’, ‘CHF’, ‘TWD’, ‘THB’, ‘GBP’, ‘RMB’, ‘INR’) ) ) ) return false;

    add_filter( ‘woocommerce_paypal_args’, ‘convert_inr_to_usd’ );
    function convert_inr_to_usd($paypal_args){
    if ( $paypal_args['currency_code'] == ‘INR’){
    $convert_rate = 54; //set the converting rate
    $paypal_args['currency_code'] = ‘USD’; //change INR to USD
    $i = 1;

    while (isset($paypal_args['amount_' . $i])) {
    $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
    ++$i;
    }

    }
    return $paypal_args;
    }
    return true;
    }

    Reply
  7. Rania says:

    Thanks, this was very helpful. Everything has worked for me except for the exchange actually working when I checkout and use Paypal. I have followed the instructions in the initial posting – I have added the code below to functions.php and added my currency (AED) to wp-content/plugins/woocommerce/classes/gateways/paypal/class-wc-paypal.php. Can anyone help?

    Step 1:
    add_filter( ‘woocommerce_currencies’, ‘add_aed_currency’ );

    function add_aed_currency( $currencies ) {
    $currencies['AED'] = __( ‘UAE Dirhams’, ‘woocommerce’ );
    return $currencies;
    }

    Step 2:

    add_filter(‘woocommerce_currency_symbol’, ‘add_aed_currency_symbol’, 10, 2);

    function add_aed_currency_symbol( $currency_symbol, $currency ) {
    switch( $currency ) {
    case ‘AED’: $currency_symbol = ‘AED’; break;
    }
    return $currency_symbol;
    }

    Step 3:
    add_filter(‘woocommerce_paypal_args’, ‘convert_aed_to_usd’);
    function convert_aed_to_usd($paypal_args){
    if ( $paypal_args['currency_code'] == ‘USD’){
    $convert_rate = 3.67; //set the converting rate
    $paypal_args['currency_code'] = ‘USD’; //change AED to USD
    $i = 1;

    while (isset($paypal_args['amount_' . $i])) {
    $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
    ++$i;
    }

    }
    return $paypal_args;
    }

    Thanks!

    Reply
  8. svetto says:

    We found a better solution with the latest version of Woocommerce. Please check the updated article.

    Reply
  9. Hi, I am having issues with the last part of your code, returning a fatal error.

    I have removed it (conversion rate) and website is working again.

    Should I double check any point?

    Thank you very much.

    Reply
      • Stevie says:

        ok i got it to work now:


        dd_filter('woocommerce_paypal_args', 'convert_krw_to_usd');
        function convert_krw_to_usd($paypal_args){
        if ( $paypal_args['currency_code'] == 'KRW'){
        $convert_rate = 1142.35; //Set converting rate
        $count = 1;
        while( isset($paypal_args['amount_' . $count]) ){
        $paypal_args['amount_' . $count] = round( $paypal_args['amount_' . $count] / $convert_rate, 2);
        $count++;
        }
        }
        return $paypal_args;
        }

        Reply
  10. Stevie says:

    I’m now looking to get this to work with the Woocommerce Subscription extension..

    any one have any ideas? . .

    Reply
  11. Hi Svetto

    I implemented your workaround on my site to convert from UAE Dirhams (which is not paypal supported) to USD. Luckily the rate is fixed.

    BUT as our launch special we have offered a 10% discount coupon – and this is not being converted – so the discount (say 43 AED) is being sent to paypal as 43 USD. Obviously not a good thing.

    What can I do to have the discount also converted.

    Many thanks.

    Reply
  12. Thanks for the reply.

    Working now. Tested with and without shipping and with and without discount.

    This is the code I have used in the end for step 3.

    add_filter(‘woocommerce_paypal_args’, ‘convert_aed_to_usd’);
    function convert_aed_to_usd($paypal_args){
    if ( $paypal_args['currency_code'] == ‘AED’){
    $convert_rate = 3.68; //set the converting rate
    $paypal_args['currency_code'] = ‘USD’; //change AED to USD
    $i = 1;

    while (isset($paypal_args['amount_' . $i])) {
    $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
    ++$i;
    }

    if ( $paypal_args['discount_amount_cart'] > 0 ) {
    $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
    }
    }

    return $paypal_args;
    }

    Reply
    • Dear Cat,

      please be so kind and email me how and what exactly did you do in order to get it working. I’m having the exact same problem as you, trying to set up a store for a client in Dubai…

      Thx

      Reply
      • Hi Agraf

        So sorry I didn’t get a note that you had replied – please email me on cat at vinefruit dot net and I will send you the file I used if this is still an issue for you. It is working fine on my site.

        Thanks

        Reply
    • but didn’t work with shipping the amount of shipping in my order like 90 AED and when i pay in paypal 90$ not converted

      Reply
  13. littleantz says:

    Woocommerce has just updated, is this still working?

    Anyone who has full final working code?

    Thanks

    Reply
    • Hi Stevvo,
      Thanks for your code. But I have a problem with the code.
      Its converting ZAR to USD but the rand value is not converted to dollar value. Its taking as R290= $290.
      I am using the following code.

      add_filter(‘woocommerce_paypal_args’, ‘convert_zar_to_usd’);
      function get_currency($from_Currency=’USD’, $to_Currency=’ZAR’) {
      $url = “http://www.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency”;

      $ch = curl_init();
      $timeout = 0;
      curl_setopt ($ch, CURLOPT_URL, $url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

      curl_setopt ($ch, CURLOPT_USERAGENT,
      “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)”);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
      $rawdata = curl_exec($ch);
      curl_close($ch);
      $data = explode(‘bld>’, $rawdata);
      $data = explode($to_Currency, $data[1]);

      return round($data[0], 2);
      }

      function convert_zar_to_usd($paypal_args){
      if ( $paypal_args['currency_code'] == ‘ZAR’){
      $convert_rate = get_currency(); //Set converting rate
      $paypal_args['currency_code'] = ‘USD’; //change ZAR to USD
      $i = 1;

      while (isset($paypal_args['amount_' . $i])) {
      $paypal_args['amount_' . $i] = round( $paypal_args['amount_' . $i] / $convert_rate, 2);
      ++$i;
      }

      if ( $paypal_args['discount_amount_cart'] > 0 ) {
      $paypal_args['discount_amount_cart'] = round( $paypal_args['discount_amount_cart'] / $convert_rate, 2);
      }
      }

      return $paypal_args;
      }

      Please help me its very urgent I am struggling since 3 days to overcome this problem.
      Thanks,
      Suma.

      Reply
  14. augustine says:

    All the above has been very helpful
    I am still have issues with step 3
    Any one has the full working code for the above
    where is the code placed for step 3.
    would be great to hear from any of the great people out here.
    regards

    Reply
  15. Thanks for the article! Do you know how to get this to work with the Stripe gateway extension? When using a custom currency, WooCommerce doesn’t allow the Stripe gateway to be enabled (I’m converting the currency to USD for PayPal on checkout, but not working for Stripe)…

    Reply
  16. Hello. Nice article. I need to use 2checkout gateway plugin on woocommerce but i can not. My default currency is Naira and 2checkout does not support the currency. i get the error “Gateway Disabled: 2checkout does not support your store currency”. I don’t mind if the shopping cart is in usd just before payment but i need my products displayed in Naira. Anything i can do?

    Reply
  17. Hello,

    Thanks for all this code above, quite useful.

    I try to modify it in order to checkout in euros instead of usd. it’s OK I can convert to euros but all the time my paypal checkout is in usd…

    Please help
    Thx

    Reply
  18. I just tested step 3 and it is working great! I am using WP 3.6.1 and WooCommerce 2.0.18. But it is really frustrating to keep changing the exchage rate every single day. Can anyone tell me how to make it take the current exchange rate from a certain bank or institution?

    Reply
  19. Thanks for the article!

    Like “augustine” I also have some problems with step 3. I am using WP 3.7.1 and WC 2.0.19. Everything works but the currency is still shown as USD and not BGN. It will be great when you can exactly describe the steps – also for newbies like me :-)

    Best regards!

    Reply
  20. hey guys,
    i live in a pretty volatile country with pretty volatile exchange rates so I thought you might appreciate what I did here to fetch a conversion rate from yahoo and put it into the code for point 3.
    I used Adam Pierce´s class (found at http://www.doctort.org/adam/general/currency-calculation-in-php.html).

    What you do is make a new php file called fx.php and paste the code he publishes inside.
    Then save it to the same folder: woocommerce/classes/gateways/paypal/class-wc-paypal.php
    Then you change this line in the code explained in point 3:
    $convert_rate = 1.954;
    For these:
    require ‘fx.php’; // Create an object to convert BGN to EUR.
    $convert_rate = new ForeignExchange(‘BGN’, ‘EUR’); //set the converting rate. These must be the official three letters that yahoo uses for currencies.

    And that’s it! I THINK it works.

    Reply
  21. sorry, i just want to ask you, i live in Indonesia, and $1 = IDR 11.000. i’m stuck at the step 3 (last code where to convert). can you help me give the right code? because my products from IDR 105.000 become $150.000 and its not right. please help, thanks!

    Reply
  22. Rashid says:

    Dear All,

    I want same function with 2checkout instead of paypal. can you please edit the following code to me that when user will click on checkout button and page will redirect to 2checkout then currency will change in $ automatically.

    add_filter( ‘woocommerce_currencies’, ‘add_custom_currency’ );
    function add_custom_currency( $currencies ) {
    $currencies["PKR"] = ‘Pakistani Rupee’;
    return $currencies;
    }

    add_filter(‘woocommerce_currency_symbol’, ‘add_custom_currency_symbol’, 10, 2);
    function add_custom_currency_symbol( $currency_symbol, $currency ) {
    switch( $currency ) {
    case ‘PKR’: $currency_symbol = ‘Rs.’; break;
    }
    return $currency_symbol;
    }

    Read more: https://devseon.com/en/wordpress-tutorials/woocommerce-add-a-paypal-unsupported-currency/#ixzz336ifOFy1

    Reply
  23. Hello, I did everything as you did in the third stage but was unable to do because the new versions of Woocommerce not have the Classes folder, but I made in another way it worked.

    I used the plugin PayPal Currency Converter PRO for WooCommerce, it converts the rate of online currency without having to go back to edit again.

    Only configured to convert AOE (Angola) to USD and functioned normally in paypal

    Reply
  24. Hi, im just wondering about the convertion… im using SAR and base on google SAR 1.00 = 0.27 USD but when i add the “0.27″ on $convert_rate = 0.27;

    and when i pay a worth SAR 1.00 using paypal it converted to 3.70 USD which is wrong it should be 0.27 USD

    what is the correct convertion rate?

    also what is the “2″ in $convert_rate, 2); ??

    thanks

    Reply
  25. Hello! Thank you very much for the tutorial.
    I can’t code at all and it is a bit confusing.
    I was finally able to find the class-wc-gateway-paypal.php where I probably should add the new code.
    However I have no idea where.
    Please give me a hand I just want to know where to paste the new code.
    I don’t have issues with the other things as the new version of Woocommerce support BGN.
    Thank you very much in advance!

    Reply
  26. islam fotouh says:

    if this code will convert the shipping Cost because i tested many plugins and they can convert just subtotal and shipping cost always still same

    Reply
  27. Hi Svetto,

    My problem is pretty similar to all the above
    I wanted Fedex real time rates to show on my site. Except the default currency in woocommerce for UAE is AED and the FedEx default currency for UAE is DHS
    So i created my own currency using similar codes above. Basically Step 1 and Step 2
    Im using Payfort, which is similar in function to PayPal
    Payfort doesn’t recognise the new currency and only recognises the default currency for Uae in the woocommerce drop down

    Need a code for STEP 3 so that i get the realtime Fedex rates as well as having Payfort accept payments.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top