I have a multi-currency wordpress website (using WPML plugin), I need a way to make a link to a specific product in a specified currency. I found this "hook" which can be used for that purpose: wcml_client_currency. As part of that, I added this code to my child functions.php file:
add_action( 'wcml_client_currency', 'currency' );
function currency( $current_currency ) {
$currency = isset( $_GET['currency'] ) ? esc_attr( $_GET['currency'] ) : $current_currency;
return strtoupper( $currency );
}
This works partially. I can use the URL mysite.com/?currency=EUR and arrive to the page with the currency selected to Euro. However, this code disables the currency selector widget which I have on my site. So landing on this page now does not change the currency anymore when you use the normal currency selector.
It seems to be a problem with two contradicting URL informations. i.e. with the above code, I add this "?currency=EUR" but the currency selector automatically adds this "wcmlc=GBP" to the URL. When both appear in the URL, the latter does not work.
When I use the "?currency=EUR" URL link and then browse to another page on my site, the currency stays the one I selected and the string disappears from the URL, now the normal currency switcher can be used.
Is there any code fix I can do to the above code which will display the currency requested but removes the string from the URL? or some other workaround to this problem?
I actually figured out the answer myself. In case anyone else is interested, I will post the solution:
instead of using "currency=EUR" in the URL, use "wcmlc=EUR in the URL, this will change the actual currency selector and will not overrule it. With that everything works.
The code I used:
add_action( 'wcml_client_currency', 'wcmlc' );
function wcmlc( $current_currency ) {
$wcmlc = isset( $_GET['wcmlc'] ) ? esc_attr( $_GET['wcmlc'] ) : $current_currency;
return strtoupper( $wcmlc );
}