Conor
Elite Member
- Nov 7, 2012
- 3,621
- 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.
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.
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.