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

Last updated by Sumit 6 months ago.

Assisted by: Sumit.

Author Posts
February 28, 2024 at 4:44 pm #15355078

louisN-4

hey goo old friend - sumit,

i saw what bigul wrote to me the other day in
https://wpml.org/forums/topic/wpml-latest-4-6-9-litespeed-issue/page/3/#post-15336379

we are in process of change our code:
https://wpml.org/forums/topic/wpml-latest-4-6-9-litespeed-issue/page/3/#post-15338335

to have cookie instead of session implementation, however that doesn't resolve the whole thing

as WPML discussed recently with litespeed - but you wasn't involved, because i am sure if it is was you, you would have not closed the discussion - if you will see the guy who spoke on the below thread is litespeed team member, their LAST and MAIN issue is that the URL is not changing:
https://wpml.org/forums/topic/anyway-to-force-show-up-the-currency-query-string-in-every-page/

i am also trying from our end, and my dev, built this, tell me what you think, this changes currency and URL, and we still use sessions at the moment

function wprdcv_param_redirect(){
$current_currency = apply_filters('wcml_price_currency', NULL);
if(isset($_COOKIE['client_currency_cookie']) && (!$_GET['currency'] || $_GET['currency']!=$current_currency)){
$location = esc_url(add_query_arg('currency', $_COOKIE['client_currency_cookie']));
wp_redirect($location);
}
}
add_action('template_redirect', 'wprdcv_param_redirect');

February 29, 2024 at 3:11 pm #15358830

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Thank you for the details that I wasn't aware of.

For this ticket https://wpml.org/forums/topic/wpml-latest-4-6-9-litespeed-issue/page/3/#post-15338335 I am truly sorry. I am not aware of this ticket but I will make sure the relevant ticket summary and our documentation are updated.

We don't need any custom code this was the feature of WCML to add query string with currency in URL but in a later version, we switched to the session then we got issues related to caching so we introduced a new feature in the form of filter "wcml_is_cache_enabled_for_switching_currency".

We have missed documenting this filter so this may be the reason our team did not find it.

However, this filter is our recommended way to add a query string.

The code you wrote may work but we must not go to the complicated (redirecting user each time causing double request to server) way we should choose the easy way 🙂 Adding a filter will do the job. When last time we interacted you had a staging site so if you still have this, I would say to add these filters there and check you will have a query string in the URL when currency changes.

Apart from that LiteSpeed doesn't need to open a new ticket for their question they can directly reach our compatibility team. Please let them know this, they must have our email address because we interacted with them in the past.
The person's name was "Ruikai" as I can see from the records.

About your code here https://wpml.org/forums/topic/wpml-latest-4-6-9-litespeed-issue/page/3/#post-15338335
As I can see the main objective of code is to set the cookie. If this is true then this code is also not needed we can simply add filter and WCML will create the cookie automatically.

Please test these changes and let me know if you need further help.

Thanks

February 29, 2024 at 3:46 pm #15359062

louisN-4

Ruikai and Eric are the team leads in Litespeed for solving issues.

I gave them yesterday our manual code, because they told me, they need redirect, since they plan to cache URLs, and they are not ok, even with cookie feature, to have the same URL with different cookie

function wprdcv_param_redirect(){
$current_currency = apply_filters('wcml_price_currency', NULL);
if(isset($_COOKIE['client_currency_cookie']) && (!$_GET['currency'] || $_GET['currency']!=$current_currency)){
$location = esc_url(add_query_arg('currency', $_COOKIE['client_currency_cookie']));
wp_redirect($location);
}
}
add_action('template_redirect', 'wprdcv_param_redirect');

LASTLY, we do plan to also change from session to cookie, as you wrote, we just wait for final GO from litespeed all the setup works.

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

February 29, 2024 at 4:16 pm #15359362

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

and they are not ok, even with cookie feature, to have the same URL with different cookie

Ok but I wonder why. WP Rocket works perfectly with the same URL and different cookies.

Anyway, they know better about their system than me. Ok if you are going to use your redirect feature then skip

add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

This is not needed and will cause interference with your query string and WCML query string.

The only code you need is

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );

To enable cookies.

I hope for now you don't need any further help from my side. But I will keep this ticket open for a while and you can reply if you need any further help.

Other suggestions, not related to WCML but for your site. When adding a redirect code also adjust the links on the site using javascript so when the user comes with a query string there will be no redirect this way you can save extra request to server.

Thanks

February 29, 2024 at 4:59 pm #15359817

louisN-4

"When adding a redirect code also adjust the links on the site using javascript so when the user comes with a query string there will be no redirect this way you can save extra request to server."

what do you mean, users already have links like:
hidden link

and it works

we are only adding when the user changes currency

function wprdcv_param_redirect(){
$current_currency = apply_filters('wcml_price_currency', NULL);
if(isset($_COOKIE['client_currency_cookie']) && (!$_GET['currency'] || $_GET['currency']!=$current_currency)){
$location = esc_url(add_query_arg('currency', $_COOKIE['client_currency_cookie']));
wp_redirect($location);
}
}
add_action('template_redirect', 'wprdcv_param_redirect');

February 29, 2024 at 7:47 pm #15360417

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Yes I understand but suppose the currency is USD and the cookie is also USD and the parameter does not exist in the URL then it will redirect, so if someone visits hidden link it will redirected to hidden link this is what I meant.

However, I see no redirect on your website yet it seems code is not added yet. Please finalize the solution from LiteSpeed and if anything needed from WPML side let me know.

Thanks

March 5, 2024 at 12:14 am #15372376

louisN-4

Hi Sumit - I need your help

Litespeed disapeared, he told me he was going to China for a trip, but then disappeared for several days (maybe no internet connection)

in POST - https://wpml.org/forums/topic/wpml-latest-4-6-9-litespeed-issue/page/3/#post-15345729

i asked, what should my htaccess look like, in order to get litespeed to cache the cookie,

do you have any idea on that to help?

March 6, 2024 at 8:32 am #15377708

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Okay! As we agreed you will be using WCML cookie and your code for the query string so only the cookie name will be changed. And the cookie WCML stores is "wcml_client_currency"

Based on that only this code will be changed from "client_currency_cookie" to "wcml_client_currency" so that will be

<IfModule LiteSpeed>
RewriteRule .? - [E=Cache-Vary:wcml_client_currency]
</IfModule>

Thanks

March 7, 2024 at 11:18 am #15383891

louisN-4

hi sumit,

i need your input please

eventually we decided not to use

function wprdcv_param_redirect(){
$current_currency = apply_filters('wcml_price_currency', NULL);
if(isset($_COOKIE['client_currency_cookie']) && (!$_GET['currency'] || $_GET['currency']!=$current_currency)){
$location = esc_url(add_query_arg('currency', $_COOKIE['client_currency_cookie']));
wp_redirect($location);
}
}
add_action('template_redirect', 'wprdcv_param_redirect');

instead we edited

woocommerce-multilingual/res/js/wcml-multi-currency.js
real name of file was wcml-multi-currency.js I added -123 to neglect caching of file.
I added new code from line # 54 to line #58
if (target_location.indexOf('currency=') === -1) {
target_location += '?currency=' + currency;
} else {
target_location = target_location.replace(/([?&])currency=[^&]+/, '$1currency=' + currency);
}

now for the question, you gave us a code to implement so wcml works based on cookies and not based on session

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

then you told us not to use
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

in case we edit the js like we showed you above, do we need to keep
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

in our snippet of wcml changing to cookie or not?

March 7, 2024 at 3:55 pm #15385533

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

First of all, I would not advise you to change the files of WCML as this will prevent you from updating the plugin.
Second, the file you are changing is not even loaded on production the correct file is wp-content\plugins\woocommerce-multilingual\res\js\multi-currency.min.js

This file is loaded only when SCRIPT_DEBUG is set to true. This is not advisable to set this true on production.

Now coming to your question

in case we edit the js like we showed you above, do we need to keep
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

No, you don't need to because you are manually adding the currency query string in the core file. This code only adds the query string when currency changes so it is not needed.

My best advice would be to use both the filters and avoid any changes also your changes and custom code.
This way we can keep the neat and clean customization without losing the updates.

If you add these both filters all of your work is done

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

Now if the user goes to next page e.g. from the product page where he switched the currency to the shop page the query string will be removed. This thing we need to confirm with LiteSpeed if they still need to query string all the time or if it is just needed for once when currency is changed.

Most of the Cache plugin needs it only once and they reset the cache based on that. That's why we introduced this feature.

In case they need query string all the time then we need to work in this direction.

Do you agree?

PS:- I am bound not to recommend any customization in core files those are not a workaround but for customization. So I am truly sorry I can not assist you in that direction.

I hope you understand!

March 9, 2024 at 11:09 pm #15392012

louisN-4

Hi Sumit,

we tried your code either as

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );

or as

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

and in both cases, when we did
hidden link

we got USD currency, so that means something is not working on your code

so we returned to using sessions for now, and now ?currency=ZAR or ?currency=CAD is working

add_filter( 'wcml_client_currency', 'modify_client_currency', 10, 1 );

function modify_client_currency( $current_currency ) {
if( !is_admin() ){
global $woocommerce;

if(isset($_GET['currency']) && !isset($_COOKIE['client_currency_cookie'])){
$currency = esc_attr( $_GET['currency'] );
$currency = strtoupper( $currency );
$currencies = get_woocommerce_currencies();
if ( array_key_exists($currency, $currencies)) {
if ( ! $woocommerce->session->has_session() ) {
$woocommerce->session->set_customer_session_cookie( true );
$woocommerce->session->set( 'client_currency', $currency );
} else {
$woocommerce->session->set( 'client_currency', $currency );
}
setcookie( "client_currency_cookie", $currency, 0, '/' );
} else {
return $current_currency;
}
}else{
if(isset($_COOKIE['client_currency_cookie'])){
$currency = $_COOKIE['client_currency_cookie'];
}else{
$currency = $current_currency;
}
}
return $currency;
}
}

add_action( 'wcml_switch_currency', 'my_custom_action_kideno', 10, 1 );
function my_custom_action_kideno( $new_currency ) {
global $woocommerce;
if ( !$woocommerce->session->has_session() ) {
$woocommerce->session->set_customer_session_cookie(true);
$woocommerce->session->set('client_currency', $new_currency);
}else{
$woocommerce->session->set('client_currency', $new_currency);
}
setcookie("client_currency_cookie", $new_currency,0,'/');
}

March 10, 2024 at 7:56 pm #15392918

louisN-4

i just read a thread related to your new cookie strategy:
https://wordpress.org/support/topic/add-cookie-for-wpml-multicurrency-feature/

could it, be that because you didn't tell us and we didn't exclude the cookie
wcml_client_currency

from cache, then this is why ?currency=XXX

did not change the currency on the pages?

March 11, 2024 at 8:35 am #15393477

Sumit
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hi,

Our hook does not work like that. It does not change the currency from URL query string value but URL query string value changes based on the currency.

For example, on your site change the currency using currency switcher and you will see the URL value changed on the currency change request.

This is what is needed for caching plugin to work as per our understanding.

To fix this issue faster I would suggest asking LiteSpeed support exactly what is needed in WCML to make this work. Then only I can suggest to you what we can do.

for now, I am not understanding what is needed from the LiteSpeed cache, is it a cookie, query string etc.

Thanks

March 11, 2024 at 12:59 pm #15395249

louisN-4

Hi Sumit

This code, allows to ?currency=XXX to function correctly:

add_filter( 'wcml_client_currency', 'modify_client_currency', 10, 1 );

function modify_client_currency( $current_currency ) {
if( !is_admin() ){
global $woocommerce;

if(isset($_GET['currency']) && !isset($_COOKIE['client_currency_cookie'])){
$currency = esc_attr( $_GET['currency'] );
$currency = strtoupper( $currency );
$currencies = get_woocommerce_currencies();
if ( array_key_exists($currency, $currencies)) {
if ( ! $woocommerce->session->has_session() ) {
$woocommerce->session->set_customer_session_cookie( true );
$woocommerce->session->set( 'client_currency', $currency );
} else {
$woocommerce->session->set( 'client_currency', $currency );
}
setcookie( "client_currency_cookie", $currency, 0, '/' );
} else {
return $current_currency;
}
}else{
if(isset($_COOKIE['client_currency_cookie'])){
$currency = $_COOKIE['client_currency_cookie'];
}else{
$currency = $current_currency;
}
}
return $currency;
}
}

add_action( 'wcml_switch_currency', 'my_custom_action_kideno', 10, 1 );
function my_custom_action_kideno( $new_currency ) {
global $woocommerce;
if ( !$woocommerce->session->has_session() ) {
$woocommerce->session->set_customer_session_cookie(true);
$woocommerce->session->set('client_currency', $new_currency);
}else{
$woocommerce->session->set('client_currency', $new_currency);
}
setcookie("client_currency_cookie", $new_currency,0,'/');
}

we need a code with this the cookie strategy so the ?currency=XXX will function also good

the main reasoning behind it (of litespeed) is that each url, will have its own set of cookies

this is why they asked us to change url when changing currency

so the only part left is to have ?currency=XXX to get it to work with this mode:

// Use Cookie instead of WCSession
add_filter( 'wcml_user_store_strategy', function ( $strategy, $key ) {
return 'cookie';
}, 10, 2 );
add_filter( 'wcml_is_cache_enabled_for_switching_currency', '__return_true' );

they and we believe this is the last part to make wcml + litespeed to work well with caching

March 11, 2024 at 1:25 pm #15395588

louisN-4

maybe you can shed some light on this topic

this is being cached perfecly
hidden link

my guess is it because it doesn't have multiple currencies so no client_currency_cookie is being downloaded at first

this however doesn't gets cached anymore
hidden link

what you see in dev console is that their is issue with client_currency_cookie, so their are several ways

1. get litespeed to develop special module for wpml/wcml session cookie

2. you manage to get the cookie strategy to work with ?currency=XXX, this way they will cache it per url and then they told me we won't need cookie-vary directive in htaccess

louisN-4 confirmed that the issue was resolved on 2024-03-20 12:12:05.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.