Change Currency in WPML Based on Geolocation

Have you ever faced a situation where you needed to change currency in WPML based on geolocation? If so then you must have looked into WPML settings only to find out that WPML doesn’t have any such built-in feature.

Following is a piece of code which will change currency in WPML based on geolocation. This piece of code works with WooCommerce as well.




// Set currency based on user's country
if ( ! is_admin() ) { // This helps to avoid 500 error in WPML admin page
    add_filter('wcml_client_currency', 'geo_client_currency');
    function geo_client_currency($client_currency) {
         $country = WC()->customer->get_shipping_country();
         switch ($country) {
            // EU
            case "AT": return "EUR"; break;
            case "BE": return "EUR"; break;
            case "CY": return "EUR"; break;
            case "EE": return "EUR"; break;
            case "FI": return "EUR"; break;
            case "FR": return "EUR"; break;
            case "DE": return "EUR"; break;
            case "EL": return "EUR"; break;
            case "IT": return "EUR"; break;
            case "LT": return "EUR"; break;
            case "LV": return "EUR"; break;
            case "LU": return "EUR"; break;
            case "MT": return "EUR"; break;
            case "NL": return "EUR"; break;
            case "PT": return "EUR"; break;
            case "SP": return "EUR"; break;
            case "SI": return "EUR"; break;
            case "SK": return "EUR"; break;
            // Others
            case "CH": return "CHF"; break;
            case "GB": return "GBP"; break;
            case "DK": return "DKK"; break;
            case "CZ": return "CZK"; break;
            case "PL": return "PLN"; break;
            case "CA": return "CAD"; break;
            default: return "USD"; break;
        }
    }
}

Note: If you use this piece of code with WooCommerce and you have visual currency switcher on the front end then the currency switcher will not work because this code will always change currency in WPML based on geolocation. In order to this code along with visual currency switcher on front-end, you will have to modify it further.



Comment

There is no comment on this post. Be the first one.

Leave a comment