Here is the PHP function for convert currency rate.
Pass which currency rate you want to get based on default currency. Here, default currency is “USD”. You can change it to anything such as get_option('woocommerce_currency')
. And it will return current rate of the currency.
function convertCurrency($toCurrency){
$default_currency = "USD"; // Default currency
if($toCurrency == $default_currency) {
return 1;
}
$url = "http://free.currencyconverterapi.com/api/v5/convert?q=".$default_currency."_".$toCurrency."&compact=y";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$data = json_decode( $result );
$var = $default_currency.'_'.$toCurrency;
$rate = $data->$var->val;
return $rate;
}
Example :
print convertCurrency("INR");
Thanks.