Last active
September 6, 2018 13:11
-
-
Save WooForce/197867353f55db315f9a88ba2c8a890e to your computer and use it in GitHub Desktop.
DHL Express - Change currency conversion rate with live data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'wf_dhl_conversion_rate', 'alter_conversion_rate',10,2); | |
function alter_conversion_rate($rate, $dhl_currency){ | |
if( !function_exists('get_woocommerce_currency')){ | |
return; | |
} | |
$from_currency = urlencode($dhl_currency); | |
$to_currency = urlencode(get_woocommerce_currency()); | |
try { | |
$result = file_get_contents("https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=$from_currency&to_currency=$to_currency&apikey=VY5GQQZFQ7WA3U6Q");// generate api key in the alphavantage website | |
if ( $result === FALSE ) { | |
throw new Exception("Unable to receive currency conversion response from Alpha Vantage API call ( https://www.alphavantage.co ). Skipping the shipping rates for the DHL as shop currency and the currency returned by Rates API differs."); | |
} | |
} | |
catch(Exception $e) { | |
//Please provide fopen permission in php.ini | |
return 0; | |
} | |
$result = json_decode($result,true); | |
if(!empty($result['Realtime Currency Exchange Rate']['5. Exchange Rate'])) | |
$converted_rate = $result['Realtime Currency Exchange Rate']['5. Exchange Rate']; | |
if(!empty($converted_rate)){ | |
$rate = $converted_rate; | |
} | |
return $rate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment