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.



12 Comments

  • Super-Duper blog! I am loving it!! Will come back again. I am taking your feeds also. Melanie Saunders Melissa
  • Thanks so much for the blog post.Thanks Again. Great.
  • I loved your blog.Really looking forward to read more. Will read on…
  • If you are going for best contents like I do, only go to see this web page all the time because it presents feature contents, thanks
  • I loved your blog.Really looking forward to read more. Will read on…
  • I really like looking at and I believe this website got some really useful stuff on it!
  • I needed to post you that little bit of observation to say thanks once again for your personal marvelous principles you've featured in this article. It is quite seriously generous of people like you to give openly just what many individuals might have marketed as an e book to generate some dough for themselves, most notably given that you could possibly have done it if you ever decided. These principles as well served to be a fantastic way to fully grasp other people have similar fervor much like my own to know the truth many more pertaining to this issue. I am certain there are millions of more fun opportunities in the future for individuals who looked over your blog post.
  • Thanks for your own work on this website. My mum take interest in setting aside time for research and it is simple to grasp why. We notice all relating to the powerful way you provide very helpful tips via your website and therefore strongly encourage response from visitors on this subject matter while our own simple princess is undoubtedly studying so much. Take pleasure in the rest of the year. Your performing a tremendous job.
  • Very informative blog article.Much thanks again. Fantastic.
  • I must say I read a great article with pleasure

Leave a comment