Italian (Italy)
Knowledge Base  >  Vik Rent Car  >  Per Programmatori  >  Currency Converter customization

It is possible to define specific hooks to overwrite or extend the default currency conversion functionalities. The hooks available allow to integrate custom conversion/exchange rates calculation, and to define the symbol to use for a specific currency, the number of decimals, the decimals/thousands separator symbols etc..

It is necessary to create a custom WordPress plugin to declare these hooks compatible with Vik Rent Car.

<?php
/*
Plugin Name:  VRC Custom Currency Conversion
Plugin URI:   https://vikwp.com/
Description:  Helper plugin to override the currency conversion functions in Vik Rent Car.
Version:      1.0
Author:       E4J s.r.l.
Author URI:   https://vikwp.com
License:      GPL2
License URI:  https://www.gnu.org/licenses/gpl-2.0.html
*/

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

/**
 * Filter to override a conversion rate between two currencies.
 * 
 * @param 	float 	$conv_rate 		the conversion rate processed by other plugins, null by default.
 * @param   string 	$from_currency 	the currency to convert from (i.e. default currency in Vik Rent Car).
 * @param 	string 	$to_currency 	the currency to convert to (ISO 4217 Alpha-3 code).
 * 
 * @return 	float|int 				the conversion rate between the two currencies, or null.
 */
add_filter('vikrentcar_get_conversion_rate', function($conv_rate, $from_currency, $to_currency) {
	if (!is_null($conv_rate)) {
		// another plugin must have set a custom conversion rate already
		return $conv_rate;
	}

	if ($from_currency === 'EUR' && $to_currency === 'USD') {
		// return a static conversion rate between EUR and USD
		return 1.50;
	}

	if ($to_currency === 'XXX') {
		// return a static conversion rate for the custom currency code "XXX"
		return 2;
	}

	// return null to not interfere with the default conversion value
	return null;
}, 10, 3);

/**
 * Filter to override the symbol, number of decimals, decimals and thousands separators of a currency.
 * 
 * @param 	array 	$currency_info 	the information array processed by other plugins, null by default.
 * @param 	string 	$currency_code 	the currency ISO 4217 Alpha-3 code to parse.
 * 
 * @return 	array 					associative list of currency information, or null.
 */
add_filter('vikrentcar_get_currency_data', function($currency_info, $currency_code) {
	if (!is_null($currency_info)) {
		// another plugin must have set custom information already
		return $currency_info;
	}

	if ($currency_code === 'USD') {
		// return custom information for the currency code USD
		return [
			'symbol' 			  => '&#128561;',
			'decimals' 			  => 3,
			'decimals_separator'  => '.',
			'thousands_separator' => ',',
		];
	}

	if ($currency_code === 'XXX') {
		// return custom information for the dummy currency code "XXX"
		return [
			'symbol' 			  => '&#128513;',
			'decimals' 			  => 0,
			'decimals_separator'  => '',
			'thousands_separator' => '',
		];
	}

	// return null to not interfere with the default currency information
	return null;
}, 10, 2);

/**
 * Filter to add a custom currency code to the available list.
 * 
 * @param 	bool 	$exists 		true if exists, false if it doesn't, null by default.
 * @param 	string 	$currency_code 	the currency ISO 4217 Alpha-3 code to parse.
 * 
 * @return 	bool 					true if exists, false if it doesn't, or null.
 */
add_filter('vikrentcar_check_currency_exists', function($exists, $currency_code) {
	if (!is_null($exists)) {
		// another plugin must have processed this currency already
		return $exists;
	}

	if ($currency_code === 'XXX') {
		// add the dummy currency code "XXX" to the list of available currencies
		return true;
	}

	// return null to not interfere with the default currency validation
	return null;
}, 10, 2);

By creating a PHP file with a name like "vrc_custom_currency_conversion.php" and with the above code inside it, you will have your own custom-coded WordPress plugin to manipulate the default currency exchange functionalities.

Do not forget to install and activate the plugin on your WordPress website, or the hooks won't be registered by WordPress itself.

Ultimo aggiornamento: 2024-04-22
Utile?