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 7 months, 4 weeks ago.

Assisted by: Sumit.

Author Posts
March 12, 2024 at 12:25 pm #15399401

louisN-4

we took our session code for ?currency=XXX
this code:
https://wpml.org/forums/topic/wpml-latest-4-6-9-litespeed-issue/page/4/#post-15392012

and changed it to cookie mode of wcml - do you have any comment before we test?

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

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

function modify_client_currency( $current_currency ) {
if( !is_admin() ){
global $woocommerce;
$cookie_set = isset($_COOKIE['wcml_client_currency']);
if(!$cookie_set && isset($_GET['currency']) && !isset($_COOKIE['wcml_client_currency'])){
$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( "wcml_client_currency", $currency, 0, '/' );
$cookie_set = true;
} else {
return $current_currency;
}
}else{
if(isset($_COOKIE['wcml_client_currency'])){
$currency = $_COOKIE['wcml_client_currency'];
}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);
}
if(!isset($_COOKIE['wcml_client_currency'])){
setcookie("wcml_client_currency", $new_currency,0,'/');
}
}

March 13, 2024 at 12:10 pm #15403806

Sumit
Supporter

Languages: English (English )

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

Hi,

Sorry for the delay I was away.

Ok I do understand now. They need query string all the time in the URL.

But your above code has so many flaws.

1. The code is setting both session and cookie.
2. Cookie name is incorrect "client_currency_cookie" this should be "wcml_client_currency"
3. You are checking if currency is valid against woocommce currency that mean even if the currency does not exist in WCML multicurrency that can be set in cookie.
4. Again in 2nd hook "wcml_switch_currency" we are setting the same cookie and session while none of them is needed.

What you can do instead add these both the code

add_filter('wcml_user_store_strategy', function ($strategy, $key) {
	return 'cookie';
}, 10, 2);
add_filter('wcml_is_cache_enabled_for_switching_currency', '__return_true');

This will create a cookie and parameter both.

Now this code will not change the currency based on the parameter as I stated before but will change the parameter based on currency change from swticher.

To fix this we can add this code which I modified.

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

function modify_client_currency($current_currency) {
	if (!is_admin() && !empty($_GET['wcmlc'])) { //Check if not admin and wcmlc parameter exist.
		global $woocommerce_wpml;
		$currency = strtoupper(esc_attr($_GET['wcmlc'])); //Make in uppercase and escape the bad values.
		if (array_key_exists($currency, (array) $woocommerce_wpml->settings['currency_options'])) {
			// Check if currency exists in WCML settings and reutrn.
			return $currency;
		}
	}
	return $current_currency; //Return the original currency if nothing matches.
}

I have added comments in code so you can understand what is happening.

Now if a request come we will change the currency based on parameter and cookie will be set by WCML based on currency change.

We don't need session so there will be no change in session data.
We are checking value from URL against the WCML setting so no other currency can be set apart from currencies define in WCML settings.

I hope this will solve your issue.

Thanks

March 13, 2024 at 12:20 pm #15403830

Sumit
Supporter

Languages: English (English )

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

Please note after these changes new parameter will be wcmlc not currency e.g. /product/simple-product/?wcmlc=USD

March 13, 2024 at 2:32 pm #15404526

louisN-4

1. can we change your code to be ?currency instead of ?wcmlc
so we won't loose the seo traffic?

2. we were using client_currency_cookie as marker for the currency, before wpml had their own cookie strategy, so you are right we don't need it anymore

3. wpml currency will be stored in wcml_client_currency, am i correct?
* asking the above, so we can test with or without caching that cookie

March 13, 2024 at 6:44 pm #15405813

louisN-4

i see we also have to change in this JS from ?wcmlc to ?currency

am i correct?

Untitled.png
March 13, 2024 at 8:04 pm #15405986

louisN-4

we made the JS change and we added your snippet

we can see everything is working on cookie

but site still not cached - can you take a pick please at things you see different in

CACHED - hidden link

NOT CACHED - hidden link

March 14, 2024 at 6:29 am #15406567

Sumit
Supporter

Languages: English (English )

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

Hi,

1. can we change your code to be ?currency instead of ?wcmlc
so we won't loose the seo traffic?

I would not suggest changing in core files. Instead I would suggest a redirect from currency to wcmlc.

Your dev can easily write a simple redirect code to change this query string. With the 301 redirect Google will update the URLs.

3. wpml currency will be stored in wcml_client_currency, am i correct?
* asking the above, so we can test with or without caching that cookie

Yes the new cookie name will be "wcml_client_currency".

but site still not cached - can you take a pick please at things you see different in

As I can see both of the URLs are not cached. I am testing by seeing the page source and the time at the bottom it always changes with each refresh. Please see the screenshot

For this issue, I am really not sure what is wrong. Maybe LiteSpeed can help?
I have suggested changes based on their suggestion. Did you contact them? What they are saying now?

Thanks

Screenshot 2024-03-14 115840.png
March 14, 2024 at 10:33 am #15409015

louisN-4

i am speaking to them constantly this morning (before this morning they were not available for 2 days)

we upgraded to litespeed 6.1 this morning, since they updated something related to WCML
this file:
hidden link

they suggested we add in line 16

"in this file , after line 16 , add
return;

this will stop plugin to set cookie vary , see how it goes

so now we are seeing cache hit
hidden link

while i think its a progress, i find it facinating that litespeed can't cache the cookie of

wcml_client_currency an wcml_client_language

and lastly, we have seen this behaviour on hebrew pages before we moved to cookie strategy, when you entered hebrew product page, you didn't have any cookie downloaded so it was a cache hit

and now all product pages don't have cookie on first entrance, so they are cache hit as well

March 14, 2024 at 2:08 pm #15410442

Sumit
Supporter

Languages: English (English )

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

Hi,

I don't know, how do you determine if the cache was hit or not but I see none of the pages is cached based on the time stamp in source code of the page.

About the cookie, I see it is properly set in the response header for the first request. Please see the attached screenshot.

I think this is something from LiteSpeed, why none of the pages are cached, they should fix it.

Thanks

Screenshot 2024-03-14 193748.png
March 14, 2024 at 2:36 pm #15410617

louisN-4

OK their are several ways you can check if its cached

1. open incognito , open dev tools, go to this url - hidden link
A. it loads in 1 second = CACHE
B. dev tools - look in the document headers, you will see "X-Litespeed-Cache:hit"

SECONDLY, now that the site is cached again finally, i can tell you what this meant

"hidden link

they suggested we add in line 16

"in this file , after line 16 , add
return;"

this means litespeed now bypassed their integration with WCML, and since the site is cached, is not known that litespeed has issues with WCML, that they plan to work on.

but its cached due to the fact that we basically cache per URL.

March 14, 2024 at 4:16 pm #15411503

Sumit
Supporter

Languages: English (English )

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

It was strange I had to clear the cache on my browser to see the hit.

Now I see the hit and also time stamp is not updated as per my traditional method. So yes I can see it works.

About the changes, I understand they are suppressing the varying headers.

I will update this information in our internal records. And I suggest that if they introduce a filter in that file e.g.

if (!defined('WCML_VERSION') || apply_filters('litespeed_wcml_remove_vary', false)) {
	return;
}

Then clients can disable it without modifying the core file. They just need to add

add_filter('litespeed_wcml_remove_vary', '__return_true');

If I do remember last time I also modified this file for one of our clients https://wpml.org/forums/topic/follow-up-to-site-language-resetting-the-cache/page/2/#post-12922031

Then we contacted LiteSpeed and they said they would fix it in the future. So far we don't have any update from them.

Anyway, I think there is nothing else needed from our side. Please mark it closed if you don't have any further questions.

Thanks

March 14, 2024 at 4:32 pm #15411546

louisN-4

i have emailed Ruikai from their team about it

i hope they introduce a fix so everyone can have it cached.

what i know that up to version 5.8 of litespeed, and wpml 4.68 things worked well

I thank you for being patient and providing a solution - any update re-when you will release the category indexing on version 4.7? so we can close this ticket?

March 19, 2024 at 3:33 pm #15427331

Sumit
Supporter

Languages: English (English )

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

Hi,

Thank you 🙂

Could you provide ticket about category indexing issue? So I can share the exact details.

WPML 4.7 is a major release it is not just WPML we will be adding more features to the entire system.
We may release it next month if everything goes as planned. But please share the ticket details we reschedule tickets based on the severity so something may have been released already.

Thanks

March 19, 2024 at 4:28 pm #15427628

louisN-4

this is where we spoke about categorization speed, and i was told that it will be in 4.7
https://wpml.org/forums/topic/options-autoload-slow-ttfb/page/5/#post-15309080

March 20, 2024 at 7:37 am #15429680

Sumit
Supporter

Languages: English (English )

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

Hi,

Yes, this is a known ticket to me. Most of the issues required for this are fixed and waiting for release.
If there will be a beta we will let you know the same ticket that is still open.

Thanks