This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 14 replies, has 11 voices.

Last updated by davidV-73 2 years, 5 months ago.

Assigned support staff: Bernat Torras.

Author Posts
May 14, 2017 at 5:03 pm #1275575

jonasL-7

I am trying to: Get automatic change of currency by geo location.
After reading the post #575973 I understand that this feature doesnt work out of the box. So my question would be if you could send me code for the following:

I have 4 currencies, EUR (default), SEK, USD, GBP
I want SEK for only Swedish visitors
USD for Americans
GBP for British
Rest of the world EUR.

(Even if its not supported I would like to give it a try)

Is there a good way to solve this by adding some code to functions.php? It would be lovely. I hope it will be built in shortly since its a must.

URL of (my) website where problem appears:
hidden link
I expected to see:
N/A

Instead, I got:
N/A

Steps to duplicate the issue:
N/A

May 15, 2017 at 10:37 am #1275958

Bernat Torras

Hi @jonasl-7,

Thanks for contacting support, I'll do my best to help you with this! 🙂

As you say, this feature is not supported by default by WooCommerce Multilingual and would require custom code workaround. I'm afraid that these kind of customizations are out of our support scope but I'd suggest you to take a look at WooCommerce Multilingual hooks guide (https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/) and try to build your own script to change the currency based on the user geo-location. I'd suggest you to take a look at 'wcml_client_currency' hook to try to change the current currency. Of course you'll need to make use of some other libraries to geo-locate the client IP before deciding the new currency.

add_filter('wcml_client_currency', 'wcml_custom_currency');
function wcml_custom_currency($currency){
    // 1- Geo-locate the client IP 
    // 2- Assign a new currency to the client based on its location
    // 3- Return this new currency
    return $currency; 
}

If you need further help with this, I'd suggest you to contact our certified contractors (https://wpml.org/documentation/support/wpml-contractors/) and explain them your request. I'm sure they'll be able to help you in a better way.

Thanks,
Bernat

May 17, 2017 at 8:24 am #1277902

jonasL-7

Thanks Bernat.
I think your information will be useful for the developer who will make it. Dont think I can do this myself. Im only a "copy and paste" kind of guy.

How does the different currencies affect the reports? In Woocommerce Orders I see my orders in the currency that the customer paid with. Is the sales reports showing in the default currency?

Thanks.
Jonas

May 17, 2017 at 12:33 pm #1278180

Bernat Torras

Hi Jonas,

WooCommerce Multilingual automatically adds the language and currency information to each order that is placed in your site. Then, it filters the orders and reports based on the used currency.

So, in orders or in reports page, you're able to switch the currency and see a list of the orders placed in each currency.

Here you can read more information about this:
https://wpml.org/documentation/related-projects/woocommerce-multilingual/multi-currency-support-woocommerce/#attachment_325264

Hope it helps.

Thanks,
Bernat

May 22, 2017 at 9:11 am #1281328

jonasL-7

Thank you Bernat. So I need to run one report for each currency, convert currencies to the default currency and sum it manually in order to get my total sales report. I got it. Its not the best solution but manageable.

May 22, 2017 at 1:13 pm #1281653

Bernat Torras

Yes, this is how it works (it would be much more difficult the other way). Hope it helps you! 🙂

If there's nothing else we can do here to help you, please consider marking this ticket as resolved. Otherwise, let me know and I'll do my best to help!

Thanks,
Bernat

March 8, 2018 at 11:43 am #1540410

jlorquin

Hi Jonas, were you able to implement the solution? I need to achieve the same result as yours. Thanks

May 10, 2018 at 5:19 pm #2077717

thibaultG

Since this is a topic that we can found a lot here, I will share my snippet that is currently working with latest version of Woocommerce and WPML as of today:

- Geolocate support must be enabled in Woocommerce General settings.

// Set currency based on visitor country
if ( ! is_admin() ) { // to avoid ERROR 500 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;
            }
     
		}
}
September 14, 2018 at 9:24 am #2734832

niklasT

Hi,

I am also facing the same situation where it would be great if currency can be changed according to customers' location. The code above works fine to geolocate and match the currency but there is no way to change the currency manually on website anymore after using that code.

Is there anyway to change the code so it will change the currency based on the visitor's location when landing on the site but also lets a visitor to change the currency manual by using currency switcher? In other words, only fire the code once when landing on the page.

Thanks for your help!

October 17, 2018 at 7:08 pm #2825711

stamatiosM

I am searching the same question with niklasT. Actually what I was thinking is to check in wcml_custom_currency function if user has used currency switcher. In such a case the set_client_currency($currency) is called (is it? I am not sure) and the $woocommerce->session->client_currency is set. So we can set if this session value is set and change the currency according to IP or not.

April 9, 2019 at 8:43 am #3564381

Nont Pongpairoj

Hi thanks for the tip. I followed the code and understand the logic. This is probably a stupid questions but where do I actually put these codes? There seems to be a few places I can insert these codes? Could you let me know where I should enter the codes?

Many thanks

August 5, 2019 at 2:38 am #4337319

karenB-6

Hi ThibaultG,

You provided code is working fine with me.

But The currency switcher is no more working.

That means if someone wants to switch the currency they can't. the system forces them to use their country currency I assigned.

People from New Zeland must be see the NZD price, they can't switch the currency by Currency switcher widget to USD anymore, But I want the currency switcher still works.

YOu got my point?

Thanks
Ash

August 23, 2019 at 7:40 pm #4449611

jaimeG-2

Thibault's snippet is spot on for me, I'm just setting 'GB' to GBP then 'default' to EUR.

I don't use the currency switcher, however, I am using billing_country rather than shipping (as virtual products in my shop so there's nothing to ship) - then customers can update their billing country at checkout or in their account, and that would switch the currency.

Only problem at the moment is the order totals don't update when the billing country dropdown is changed unless the page is refreshed. So I need to workout how to get it to update automatically...

September 13, 2019 at 9:43 am #4567135

rangsonT

I have the same problem as karenB-6. Cannot change currency. Is there any update on this code or official support?`
Thanks.

September 16, 2019 at 2:21 pm #4581245

davidV-73

@thibaultg does the snippet still work with the latest stable Woo version? And, for us not so savvy admins, where do I insert the snippet?

Thanks very much,
David