Skip Navigation

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

Last updated by per-egilR 5 months, 2 weeks ago.

Assigned support staff: Bruno.

Author Posts
March 25, 2020 at 3:08 pm #5762061


I am trying to: I'm writing a plugin using the WP REST API. When I call wc_get_product() within the context of the REST API, the user-selected currency is reset to the language default currency. Reloading the page or navigation to a new page, the user-selected currency is lost, and the default currency is displayed in the currency switcher.

Link to a page where the issue can be seen: hidden link

I expected to see: The client-selected currency in the currency switcher when reloading the page.

Instead, I got: The default currency of the current language.

March 25, 2020 at 11:15 pm #5764513


Thank you for contacting us. I would be very happy to help you but I didn't fully understand what is the issue.

Are you referring to the price that is displayed just below the coin selector? If so, I cannot replicate the problem. When I change this and refresh the page, the price is displayed with the currency that was selected.

Can you please elaborate a little more so I can better understand? Sometimes screenshots illustrating what you mean may go a long way to clarify what words fail to transmit so if you like make use of "upload an image" below the comment area.

Thank you.

March 25, 2020 at 11:38 pm #5764651


I've done some more debugging and narrowed the issue down. The following is tested in a WP REST API context.

The core cause of this issue is that WCML_Multi_Currency::client_currency is set to the default WooCommerce currency (get_option( 'woocommerce_currency' )) when the WCML_Multi_Currency object is created:

099 WCML_Multi_Currency::__construct()
108 WCML_Multi_Currency::are_filters_need_loading()
161 WCML_Multi_Currency::get_client_currency()
489 $this->client_currency = $woocommerce_currency;

At this time - at least in a REST API context - the $woocommerce->session object is not yet available. On later calls to WCML_Multi_Currency::get_client_currency() - when $woocommerce->session is available - the WooCommerce session is updated with the initial default currency:

604 $woocommerce->session->set( 'client_currency', $this->client_currency );

and the user-selected currency is lost.

A quick and dirty workaround is to manually update WCML_Multi_Currency::client_currency at the beginning of your REST API code:

global $woocommerce, $woocommerce_wpml;
$currency = $woocommerce->session->get( 'client_currency' );
$woocommerce_wpml->multi_currency->set_client_currency( $currency );

I think that this is a bug that should be fixed.

March 25, 2020 at 11:49 pm #5764663


Hi Bruno, the reason why this worked is that I applied the above workaround. I have removed the workaround now, and you can try again.


April 2, 2020 at 11:13 am #5821511


The WCML client currency gets lost when using a custom REST API. Adding the below code at the beginning of each REST API function solved this issue for me:

global $woocommerce, $woocommerce_wpml;
$currency = $woocommerce->session->get( 'client_currency' );
$woocommerce_wpml->multi_currency->set_client_currency( $currency );