Woocommerce Paypal Currency Converter

Conor

Elite Member
Joined
Nov 7, 2012
Messages
3,621
Reaction score
6,217
I spent a long time today trying to find a working plugin to get Woocommerce to convert the product prices to USD automatically, when a user checks out with Paypal. I didn't find anything free or usable.

So, with my newfound, although still limited PHP knowledge, and a LOT of Googling, I decided to put something together from scratch.

Simply paste this into functions.php, and it should do its thing when you click "Checkout". It automatically gets the latest currency conversion from Google, which I think was a nice touch, though definitely the most difficult.

Code:
// Enable the currency in WC
add_filter( 'woocommerce_currencies', 'add_zar_currency' );  
  
function add_zar_currency( $currencies ) {  
 $currencies['ZAR'] = __( 'South African Rand (R)', 'woocommerce' );
 return $currencies;  
}
add_filter('woocommerce_currency_symbol', 'add_zar_currency_symbol', 10, 2);  
  
function add_zar_currency_symbol( $currency_symbol, $currency ) {  
 switch( $currency ) {  
 case 'ZAR': $currency_symbol = 'R'; break;  
 }  
 return $currency_symbol;  
}
add_filter( 'woocommerce_paypal_supported_currencies', 'add_zar_paypal_valid_currency' ); 

function add_zar_paypal_valid_currency( $currencies ) {    
     array_push ( $currencies , 'ZAR' );  
     return $currencies;    
    }
add_filter('woocommerce_paypal_args', 'convert_zar_to_usd');

// Automatic Conversion Bit
function convert_zar_to_usd($paypal_args){
$amount = urlencode(1);
    $from_Currency = urlencode("ZAR"); // Important. It's the currency you're converting
    $to_Currency = urlencode("USD"); // Also important, this needs to be a Paypal supported currency, or it won't work.


    $url = "http://www.google.com/finance/converter?a=$amount&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]);


    $amnt = round($data[0], 3);
  
    if ( $paypal_args['currency_code'] == 'ZAR'){  
        $convert_rate = $amnt;
        $paypal_args['currency_code'] = 'USD';
        $i = 1;  


// Without the IF statements here, the thing gives totally wrong figures. 
        while (isset($paypal_args['amount_' . $i])) {
if ($amnt < 1) {  
            $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] * $convert_rate, 2);
}
elseif ($amnt > 1) {  
            $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] / $convert_rate, 2);
}   
            ++$i;
        }  
  
    }  
return $paypal_args;  
}

NOTE: The currency I've used is ZAR, because I'm in South Africa. But if you want to change it to your local currency, simply replace "ZAR" with your local currency in Notepad, with "Match Case" ticked. Also, remember to change line 5 and line 12 to your currency symbol of choice.

I'll try put together a frontend interface to do all this automatically, perhaps sometime in the future.

Thoughts? Critiques? It's a messy, rough script, but it works for me at least.
 
Nice effort Conor.I will forward this tweak to my partner.Thanks
 
Hi,
The code working perfectly. It is converting ZAR To USD. But the thing is its not converting the R450 t0 Some USD price. Its just converting R450 to $450. Please help me to convert to exact dollar amount of my ZAR price.

Thanks,
Suma.
 
Hi,
The code working perfectly. It is converting ZAR To USD. But the thing is its not converting the R450 t0 Some USD price. Its just converting R450 to $450. Please help me to convert to exact dollar amount of my ZAR price.

Thanks,
Suma.

That's strange. Are you using PayPal as your primary gateway? PM me your site once you get to 15 posts and I can take a look for you.
 
Yes Paypal is the primary gateway. I din't understand about 15 posts. whta's that? I need to resolve it soon as its very urgent to me. Can You people help me?

Regards,
Suma.
 
Yes Paypal is the primary gateway. I din't understand about 15 posts. whta's that? I need to resolve it soon as its very urgent to me. Can You people help me?

Regards,
Suma.
 
I spent a long time today trying to find a working plugin to get Woocommerce to convert the product prices to USD automatically, when a user checks out with Paypal. I didn't find anything free or usable.

So, with my newfound, although still limited PHP knowledge, and a LOT of Googling, I decided to put something together from scratch.

Simply paste this into functions.php, and it should do its thing when you click "Checkout". It automatically gets the latest currency conversion from Google, which I think was a nice touch, though definitely the most difficult.

Code:
// Enable the currency in WC
add_filter( 'woocommerce_currencies', 'add_zar_currency' );  
  
function add_zar_currency( $currencies ) {  
 $currencies['ZAR'] = __( 'South African Rand (R)', 'woocommerce' );
 return $currencies;  
}
add_filter('woocommerce_currency_symbol', 'add_zar_currency_symbol', 10, 2);  
  
function add_zar_currency_symbol( $currency_symbol, $currency ) {  
 switch( $currency ) {  
 case 'ZAR': $currency_symbol = 'R'; break;  
 }  
 return $currency_symbol;  
}
add_filter( 'woocommerce_paypal_supported_currencies', 'add_zar_paypal_valid_currency' ); 

function add_zar_paypal_valid_currency( $currencies ) {    
     array_push ( $currencies , 'ZAR' );  
     return $currencies;    
    }
add_filter('woocommerce_paypal_args', 'convert_zar_to_usd');

// Automatic Conversion Bit
function convert_zar_to_usd($paypal_args){
$amount = urlencode(1);
    $from_Currency = urlencode("ZAR"); // Important. It's the currency you're converting
    $to_Currency = urlencode("USD"); // Also important, this needs to be a Paypal supported currency, or it won't work.


    $url = "http://www.google.com/finance/converter?a=$amount&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]);


    $amnt = round($data[0], 3);
  
    if ( $paypal_args['currency_code'] == 'ZAR'){  
        $convert_rate = $amnt;
        $paypal_args['currency_code'] = 'USD';
        $i = 1;  


// Without the IF statements here, the thing gives totally wrong figures. 
        while (isset($paypal_args['amount_' . $i])) {
if ($amnt < 1) {  
            $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] * $convert_rate, 2);
}
elseif ($amnt > 1) {  
            $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] / $convert_rate, 2);
}   
            ++$i;
        }  
  
    }  
return $paypal_args;  
}

NOTE: The currency I've used is ZAR, because I'm in South Africa. But if you want to change it to your local currency, simply replace "ZAR" with your local currency in Notepad, with "Match Case" ticked. Also, remember to change line 5 and line 12 to your currency symbol of choice.

I'll try put together a frontend interface to do all this automatically, perhaps sometime in the future.

Thoughts? Critiques? It's a messy, rough script, but it works for me at least.

Hi! thank you so much for sharing this, appreciate your effort. This might come in handy to my next projects.
 
Hi

This is exactly what I have been looking for but with a small difference... I need to display 5 currencies to the users.
I am using a currency switch already and this is working fine, however, if a non-support currency is selected by the user, PayPal module becomes hidden.
I would like the system to convert any currency into USD before redirecting to PayPal.

Any idea on how to accomplish this?

Thank you
 
For anyone who noticed that Google selfishly disabled their entire finance API, so I've made a couple of updates and improvements to the code:
  • The plugin now supports shipping and discounts
  • The conversion rate is more accurate (More decimal places when rounding up the amount)
  • You only have to change 3 lines of code, according to your currency (Line 16 and 24).
  • You can also change the Paypal currency if you like, but it must be a Paypal supported currency (Line 25)
Code:
<?php
/*
Plugin Name: Paypal for South Africa
Plugin URI: https://lizyd.com
Description: Paypal support for local currencies.
Version: 2
Author: Conor Campbell
Author URI: mailto:[email protected]
License:
License URI:
*/

add_filter( 'woocommerce_paypal_supported_currencies', 'add_paypal_valid_currency' );

function add_paypal_valid_currency( $currencies ) {  
    $currencies[] = 'ZAR'; // Your Currency
  
    return $currencies;
    }
add_filter('woocommerce_paypal_args', 'convert_zar_to_usd');
// Automatic Conversion Bit
function convert_zar_to_usd($paypal_args){

    $from = "ZAR"; // Your Currency
    $to = "USD"; // Paypal Supported Currency
    $from_Currency = urlencode($from);
    $to_Currency = urlencode($to);


        // create curl resource
        $ch = curl_init();

        // set url
        curl_setopt($ch, CURLOPT_URL, "https://free.currencyconverterapi.com/api/v5/convert?q={$from_Currency}_{$to_Currency}&compact=ultra");

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        $output = curl_exec($ch);
        // close curl resource to free up system resources
        curl_close($ch);
        $data = explode(':', $output);
        $data = explode(" ", $data[1]);
        $amnt = round($data[0], 6);

    if ( $paypal_args['currency_code'] == $from){
        $convert_rate = $amnt;
        $paypal_args['currency_code'] = $to;
        $i = 1;
        $j = 1;


        // Without the IF statements here, the thing gives totally wrong figures.
        while (isset($paypal_args['amount_' . $i])) {

            if ($amnt < 1) {
                $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] * $convert_rate, 2);
            }
            elseif ($amnt > 1) {
                $paypal_args['amount_' . $i] = round($paypal_args['amount_' . $i] / $convert_rate, 2);
            }  

            ++$i;
        }

        while (isset($paypal_args['shipping_' . $j])) {
            if ($amnt < 1) {
                $paypal_args['shipping_' . $j] = round($paypal_args['shipping_' . $j] * $convert_rate, 2);
            }
            elseif ($amnt > 1) {
                $paypal_args['shipping_' . $j] = round($paypal_args['shipping_' . $j] / $convert_rate, 2);
            }  
            ++$j;
        }

        // discount currency conversion for paypal
        if (isset($paypal_args['discount_amount_cart'])) {
            if ($amnt < 1) {
                $paypal_args['discount_amount_cart'] = round($paypal_args['discount_amount_cart'] * $convert_rate, 2);
            }else if ($amnt > 1) {
                $paypal_args['discount_amount_cart'] = round($paypal_args['discount_amount_cart'] / $convert_rate, 2);
            }

        }

    }

 return $paypal_args;
}
 
Back
Top