What can we help you with?

How to convert Base currency to DHL currency, dynamically? (Code Snippet)

Converting Store’s Base Currency to DHL Currency

The code snippets have to be added to the Theme Functions (functions.php) file of the activated theme of your website. Also, the code snippets work only with ELEX WooCommerce DHL Express Shipping plugin with Print Label.

You can convert your store’s base currency to DHL currency using either of the following currency exchange APIs:

  1. Alpha Vantage API
  2. Free Currency Converter API

For Alpha Vantage API

The following code snippet facilitates you to convert base currency to DHL currency, by fetching live currency conversion rates from Alpha Vantage. But remember, to use this code snippet, you need to claim your API key from Alpha Vantage website and replace your API key with the one given in the code snippet (check comment line message) below.

/*Provides conversion rate for the provided from and to currencies*/

add_filter( 'wf_dhl_conversion_rate', 'alter_conversion_rate',10,3);
function alter_conversion_rate($rate, $dhl_currency, $shop_currency){
if( !function_exists('get_woocommerce_currency')){
return;
}

$from_currency = urlencode($dhl_currency);
$to_currency = urlencode($shop_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 $rate;
}

$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;
}

For Free Currency Converter API

Use the following code snippet if you are using the Free Currency Converter API for currency conversion.

/*Provides conversion rate for the provided from and to currencies*/
add_filter( 'wf_dhl_conversion_rate', 'alter_conversion_rate',10,3);
function alter_conversion_rate($rate, $dhl_currency, $shop_currency){
if( !function_exists('get_woocommerce_currency')){
return;
}
$from_currency = urlencode($dhl_currency) ;
$to_currency = urlencode($shop_currency) ;
try {
$result = file_get_contents("http://free.currencyconverterapi.com/api/v5/convert?q=".$from_currency."_".$to_currency."&compact=y");

if ( isset($result->query) ) {
throw new Exception("Unable to receive currency conversion response from Currency Converter API call. Skipping the shipping rates for the DHL as woocommerce currency and the currency returned by Rates API differs.");
}
}
catch(Exception $e) {
//Please provide fopen permission in php.ini
return $rate;
}
$conversion_rate_key = $from_currency."_".$to_currency;
$result = json_decode($result,true);
if(!empty($result[$conversion_rate_key]))
$converted_rate = $result[$conversion_rate_key]['val'];

if(!empty($converted_rate)){
$rate = $converted_rate;
}

return $rate;
}

For both the code snippets to work, you need to do the following changes:

  1. Update the php.ini file with the following values –
    allow_url_fopen = ON
    allow_url_include = ON
  2. Restart your Apache server once the above changes are made.

Once you include the given code snippet, for any base currency that you select, the rates will be converted to the preferred DHL currency (which is selected in plugin settings page).

Suppose, you have selected “Euro” as DHL Currency in plugin settings, and “US dollars” as the base currency. Then, the code snippet will convert all rates in Euros by fetching dynamic conversion rates online.

 


To explore more details about the plugins, go check out ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label.

Read the article for setting up DHL Express understand the plugin, in detail. Or check out the product documentation section for more related articles.

You can also check out other WooCommerce and WordPress plugins in ELEX.

Previous How to remove Signature options from DHL shipping label, if the country is Switzerland? (Code Snippet)
Next How to Add an Order number on shipment label in ELEX WooCommerce DHL Shipping plugin? (Code Snippet)
You must be logged in to post a comment.