This thread is resolved. Here is a description of the problem and solution.
Problem:
Cart items are not synced between languages with custom lang switcher.
Solution:
The user is tweaking our code example from WPML's documentation here.
If your site is set to 'A different domain per language' Language URL format, then this code example will not sync the cart's content when you switch the language. But only by adding a class (wpml-ls-item) the element that holds the language's link, the issue is solved. This is the corrected code example.
function language_selector_flags(){ $languages = icl_get_languages('skip_missing=0&orderby=code'); if(!empty($languages)){ foreach($languages as $l){ if(!$l['active']) echo '<a class="wpml-ls-item" href="'.$l['url'].'">'; echo '<img src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" />'; if(!$l['active']) echo '</a>'; } } }
Relevant Documentation:
https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/
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 18 replies, has 2 voices.
Last updated by Itamar 4 years, 11 months ago.
Assisted by: Itamar.
Author | Posts |
---|---|
December 6, 2019 at 1:11 pm #5084001 | |
FortunyShop |
I am trying to: This is the custom code I used: function us_shop_link(){ $languages = icl_get_languages('skip_missing=0&orderby=code'); if(!empty($languages)){ foreach($languages as $l){ if(!$l['active']) { if($l['language_code'] == "en_us") { echo ' <li> <a title="Go to the US Store" href="'.$l['url'].'"> <img src="<em><u>hidden link</u></em>" class="us-store-butt glow" alt="US store button" title="Go to the US Store"> </a> </li> '; } } } } } header-main.php <?php us_shop_link(); ?> Link to a page where the issue can be seen: I expected to see: Instead, I got: |
December 9, 2019 at 5:07 pm #5094637 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Hi. I can see this issue on your site. But I'm not sure that it is being caused by your custom Language Switcher. Please check whether the option 'Store a language cookie to support language filtering for AJAX' in WPML -> Languages -> Language filtering for AJAX operations is enabled. If not then please enable it and see if it helps. If it does not help then please check if things work when you place the default Language Switcher that comes with WPML. Does it work then? Regards, |
December 10, 2019 at 4:03 pm #5101405 | |
FortunyShop |
Hi, Yes it works with the default language switcher, it maintains the items in the cart. Thanks, |
December 11, 2019 at 12:21 pm #5106571 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Hi, and thanks for trying my suggestions. I'm consulting our second tier supporters about this issue. When I have a reply from them, I'll let you know here. Thank you for your patience. |
December 11, 2019 at 1:10 pm #5106991 | |
FortunyShop |
Thank you, I'll wait for news then. |
December 12, 2019 at 6:05 pm #5117991 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Hi. Our second tier supporter says that the code is working for him finely. Could you please let me have access to your site so I can check things. If you need further help with this, please share the access details to your site with me. Privacy and Security Policy We have strict policies regarding privacy and access to your information. Please see: **IMPORTANT** - - Please make a backup of site files and database before providing us access. -- Thanks, |
December 15, 2019 at 12:51 pm #5133225 | |
FortunyShop |
Ok so I whitelisted Israel in the firewall. |
December 15, 2019 at 3:40 pm #5133541 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Thanks. When I go to WPML -> Languages -> Language URL format, I can see that the option 'Pass session arguments between domains through the language switcher' is set to 'Disable this feature'. Please see the attached screenshot. This might probably be the reason why the cart content is not syncing. Please change it to 'Pass arguments via GET (the url)' and check if then things work. Thanks, |
December 16, 2019 at 9:32 am #5137041 | |
FortunyShop |
HI, What else could be? |
December 17, 2019 at 1:40 pm #5147417 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Hi, and thanks for trying my suggestions. I've checked things both on my test site and on your site. Here are my findings. It seems that you have based your code on the following code example from our documentation. function language_selector_flags(){ $languages = icl_get_languages('skip_missing=0&orderby=code'); if(!empty($languages)){ foreach($languages as $l){ if(!$l['active']) echo '<a href="'.$l['url'].'">'; echo '<img src="'.$l['country_flag_url'].'" height="12" alt="'.$l['language_code'].'" width="18" />'; if(!$l['active']) echo '</a>'; } } } Is this correct? When I try this code on my test site, things are working. The cart content is syncronizing. Also, if I try your code but with commenting out two lines as here below, the code works. function us_shop_link(){ $languages = icl_get_languages('skip_missing=0&orderby=code'); if(!empty($languages)){ foreach($languages as $l){ if(!$l['active']) { // if($l['language_code'] == "en_us") { echo ' <li> <a title="Go to the US Store" href="'.$l['url'].'"> <img src="<em><u>hidden link</u></em>" class="us-store-butt glow" alt="US store button" title="Go to the US Store"> </a> </li> '; // } } } } } My suspections are: Can you please clear all the cache from your site and deactivate the caching plugin and see if things are working then? If they are not working with your code, can you please try our code example and tell me if they are working then (even if all the other languages will show in the Language Switcher from the code)? Also please make sure that you are adding a product to the cart that has a complete translation into the en_us langauge Thanks, |
December 17, 2019 at 4:33 pm #5148755 | |
FortunyShop |
So, we use WP rocket for cache and it's always deactivated in our staging site. In the staging site (languages divided in subfolders) I did this: In the live site (languages divided in different domains): So basically, at least using the "different domains per language" option, there's a difference between how the custom code passes the variables than the default WPML lang switcher. Can you confirm this? |
December 17, 2019 at 6:45 pm #5149359 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Yes, I can confirm this. But this is what happens on your site. I've configured my test site to be in "different domains per language," and the cart content is synced with your custom code (with the lines that are commented out.) In fact, the cart content is synced anyway. If I keep the second domain cart page open and add a product to the cart in the main domain and then refresh the second domain cart page, the content shows there. And now I can see that it is also happening on your site. Please see the attached screenshot. It happened after I re-saved the options in WooCommerce Multilingual -> Settings -> Cart. Do you have the option to clear and disable the cache on the live site and test this? |
December 18, 2019 at 9:55 am #5153983 | |
FortunyShop |
I did this in live site: So cache must not be the problem here, then: There must be something independent from cache that makes the custom button not work and language switcher work. Can you confirm this? PS. I think if you're logged the problem isn't there as the situation is different, as when you're logged the information of the cart is stored in your account, so for example if I log in with my smartphone I still find the same cart items (here the problem seems to be more in the session than in the account). Thanks |
December 18, 2019 at 10:35 pm #5158659 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Hi. I still need to look into this matter. I'll be able to reply here tomorrow. Thank you for your patience. |
December 19, 2019 at 10:03 am #5162103 | |
Itamar Supporter
Languages: English (English ) Hebrew (עברית ) Timezone: Asia/Jerusalem (GMT+02:00) |
Hi. I can replicate the issue with the custom Language Switcher code from our documentation here. I've escalated this issue to our second tier supporters. They will check it, and when I have news about it, I'll report to you here. Please note that I escalated the issue for the code example that we give in our documentation. Our support policy states that we cannot produce custom code solutions for you. So if our developers will find a fix for the custom code, but your tweakings still don't work, we will not be able to fix that for you. If needed, I would suggest you consider contacting one of our certified partners from this link: https://wpml.org/contractors/ Best Regards, |