Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active October 8, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timersys/9eb4e25d338f45169846aeb3afa9a05f to your computer and use it in GitHub Desktop.
Save timersys/9eb4e25d338f45169846aeb3afa9a05f to your computer and use it in GitHub Desktop.
Change default currency on woocommerce depending on user country
<?php
/**
* Change default WPML Woocommerce currency
* depending on user's country
* https://timersys.com/?p=21090&preview=true
*/
function geot_woo_default_currency( $currency ) {
if( ! function_exists( 'geot_country_code' ) || isset( $_COOKIE['geot_currency'] ) )
return $currency;
$user_country = geot_country_code();
switch( $user_country ) {
case 'CH':
$currency = 'CHF';
break;
case 'US':
$currency = 'USD';
break;
default:
$currency = 'EUR';
break;
}
// save cookie to execute this only once
setcookie( 'geot_currency', $currency, time() + (86400 * 30), "/");
return $currency;
}
add_filter('wcml_client_currency','geot_woo_default_currency');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment