Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client wants to change the currency based on the URL parameter without needing a page reload.

Solution:
1. Set up a Currency Switcher as detailed in our documentation: Multi-currency Support for WooCommerce.
2. If using multiple languages, set currency per language as explained in the same guide.
3. For a custom solution, follow these steps:
- Retrieve the "currency" parameter from the URL.
- Sanitize the parameter to prevent switching to a non-existent currency.
- Use the

wcml_client_currency

hook to force the currency switch. For details, see: wcml_client_currency.
- It is recommended to force a page reload after the currency change.
4. Consider additional hooks for post-switch actions:

If this solution does not seem relevant or if you need further custom work, please open a new support ticket.

100% of people find this useful.

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 3 replies, has 2 voices.

Last updated by Waqas Bin Hasan 1 year, 6 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
November 1, 2023 at 1:24 pm #14707827

artjanv

We want to change the currency based on the url.

So default for our EN shop is currency EUR but if someone visit the url like this:
hidden link
It has to be in GPB. We can change the cookie but then it is available after a reload an not directly. Any hook we can use? We already checked this hook "wcml_set_client_currency" but that's not working.

November 2, 2023 at 11:11 am #14715611

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting the support.

I checked your site and it looks like is only one language is used. So as per my understanding, in this case you can setup a Currency Switcher. Please see https://wpml.org/documentation/related-projects/woocommerce-multilingual/multi-currency-support-woocommerce/ for details.

If you are using multiple languages (translations), you can also set currency per language (details are in the same guide).

On the other hand, you may also check WCML's Hooks Reference (https://wpml.org/documentation/related-projects/woocommerce-multilingual/wcml-hooks-reference/), in case if it interests you more.

Please let me know if this helps or I'll try my best to help you further on the matter.

Regards.

November 2, 2023 at 5:31 pm #14720093

artjanv

HI,

We know the currency switcher but needs a method to change the currency by url. We have searched for the correct hook but didn't found any that gives the ability to set the correct currnecy directly.

For example:
domain.nl?currency=EUR = SHow all prices in EUR
domain.nl?currency=GPB = SHow all prices in GPB

Default is EUR.

We can use a function for this. We already test with chaning the cookie, that's working only after the visitor refresh the page.

November 3, 2023 at 7:49 am #14723093

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for the details.

I think this leads to a custom coding solution which is unfortunately out of the scope of this forum.

However, as per my understanding, you can adopt the following workaround via custom coding:

- Grab the value of "currency" parameter from URL.
- Sanitize the parameter properly, because a user can switch to a non existing currency if done improperly.
- Force switching the currency by using "wcml_client_currency" WCML hook (https://wpml.org/wcml-hook/wcml_client_currency/).
- If the currency is changed in the filter, we strongly recommend you force the page to reload. See details on the mentioned guide.

Additionally, you may also be interested in following hooks, in case you want to handle things after currency has been switched:

- https://wpml.org/wcml-hook/wcml_switch_currency/
- https://wpml.org/wcml-hook/wcml_set_client_currency/

If you need additional custom work, we recommend you contact WPML Contractors at https://wpml.org/contractors/.