If you run a Woocommerce shop and you use WPML not only to translate products but also to have different currencies for you users this trick may be for you.
Let’s say for example that your shop currency is EUR but you also sell products for Switzerland where their currency is CHF. So if you have a user than comes from Switzerland you want to show them their default currency. You can accomplish that easily using a PHP filter and the Geotargeting Pro plugin by adding the following code:
This file contains 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
<?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'); |
You can create a lot more with Geolocation API provided in the plugin and WooCommerce like for example hiding products for certain countries or redirect users based on their location .So be sure to take a look.
This tip was shared by Andras Guseo from Divi Magazine
Leave a Reply