Skip to content

Instantly share code, notes, and snippets.

@gonzomir
Last active January 31, 2018 08:22
Show Gist options
  • Save gonzomir/bf19046f0b9475364e190eee842600df to your computer and use it in GitHub Desktop.
Save gonzomir/bf19046f0b9475364e190eee842600df to your computer and use it in GitHub Desktop.
Currency switcher class for use with WordPress.

Currency switcher class

Use this class to show prices in different currencies. Use add_filter to change the output of the function that shows prices to a different currency like this (the example is for Lava RealEstate):

function gonzo_price( $price, $post_id ){
  global $post, $gonzo_cw;

  if( !$post_id )
    $post_id  = $post->ID;

  return $gonzo_cw->format( $gonzo_cw->convert( intVal( get_post_meta( $post_id, '_price', true ) ), 0 ) );

}
add_filter( 'lava_realestate_price', 'gonzo_price', 10, 2 );

The current implementation has a base currency in GBP.

License

Licensed under (WTFPL)[http://www.wtfpl.net/]

<?php
class CurrencySwitcher {
private $currencies = array (
'gbp' => array ( 'sign' => '£', 'position' => 'before', 'rate' => 1, 'name' => 'British Pound', 'short' => 'GBP' ),
'eur' => array ( 'sign' => '€', 'position' => 'before', 'rate' => 1.142530706, 'name' => 'Euro', 'short' => 'EUR' ),
'usd' => array ( 'sign' => '$', 'position' => 'before', 'rate' => 1.02771855, 'name' => 'US Dollar', 'short' => 'USD' ),
'aud' => array ( 'sign' => '$', 'position' => 'before', 'rate' => 1.730482852, 'name' => 'Australian Dollar', 'short' => 'AUD' )
);
private $currency = 'gbp';
public function __construct( $default ){
if( array_key_exists( $default, $this->currencies ) ){
$this->currency = $default;
}
add_action( 'init', array( $this, 'set_currency' ) );
add_action( 'gonzo_daily', array( $this, 'get_rates' ) );
$this->set_rates();
}
public function get_rates(){
$ratesURL = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml';
$response = wp_remote_get( $ratesURL, array( 'user-agent' => 'Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0' ) );
$ratesXML = new SimpleXMLElement( $response['body'] );
$currencies = array_keys( $this->currencies );
// We put a default EUR rate that we will later reference to GBP
$rates = array( 'eur' => 1 );
foreach( $ratesXML->Cube->Cube->Cube as $rate ){
$currency = strtolower( $rate['currency'] );
if( in_array( $currency, $currencies ) ) {
$rates[$currency] = (float)$rate['rate'];
}
}
if( $rates['gbp'] != 0 ) {
$eur2gbp = 1 / $rates['gbp'];
foreach( $currencies as $currency ){
$rates[$currency] = round( $eur2gbp * $rates[$currency], 4 );
}
set_transient( 'gonzo_currency_rates', $rates, 48 * HOUR_IN_SECONDS );
return $rates;
}
return false;
}
private function set_rates(){
if ( false === ( $rates = get_transient( 'gonzo_currency_rates' ) ) ) {
if ( true === ( $rates = @$this->get_rates() ) ) {
set_transient( 'gonzo_currency_rates', $rates, 48 * HOUR_IN_SECONDS );
}
}
if( $rates ) {
foreach ( $this->currencies as $key => $currency ) {
if( $rates[$key] != 0 ) {
$this->currencies[$key]['rate'] = $rates[$key];
}
}
}
}
public function set_currency(){
if( isset( $_GET['currency'] ) ){
if( array_key_exists( $_GET['currency'], $this->currencies ) ){
$this->currency = $_GET['currency'];
}
$expiry = strtotime('+1 year');
setcookie( 'currency', $this->currency, $expiry, COOKIEPATH, COOKIE_DOMAIN );
}
elseif ( isset( $_COOKIE['currency'] ) ) {
if( array_key_exists( $_COOKIE['currency'], $this->currencies ) ){
$this->currency = $_COOKIE['currency'];
}
}
}
public function get_currency(){
return $this->currency;
}
public function get_currencies(){
return $this->currencies;
}
public function format( $price ) {
$currency = $this->currencies[$this->currency];
if( $currency['position'] == 'before' ){
return $currency['sign'] . '' . number_format_i18n( $price );
}
else {
return number_format_i18n( $price ) . '' . $currency['sign'];
}
}
public function convert( $price, $precision ){
return round( $price * $this->currencies[$this->currency]['rate'], $precision );
}
}
$gonzo_cw = new CurrencySwitcher( 'gbp' );
?>
<?php
/**
* In plugin activation and deactivation hooks register sheduled job to execute rates update.
*/
function gonzo_activate(){
if ( ! wp_next_scheduled ( 'gonzo_daily' ) ) {
wp_schedule_event( time(), 'daily', 'gonzo_daily' );
}
}
register_activation_hook( __FILE__, 'gonzo_activate' );
function tnac_deactivate() {
wp_clear_scheduled_hook('gonzo_daily');
}
register_deactivation_hook(__FILE__, 'gonzo_deactivate');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment