Skip Navigation

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.

https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/#language-selector-with-flags-only

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

Assisted by: Itamar.

Author Posts
December 6, 2019 at 1:11 pm #5084001

FortunyShop

I am trying to:
Sync cart in different languages with the custom language switcher as explained here:
https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/

This is the custom code I used:
functions.php

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:
hidden link
Any product page after the item has been added to cart

I expected to see:
Cart keeping the items after language is switched through the custom button "US STORE" at the top right of the page.
The link should bring the user here:
hidden link
...and show them they have 1 item in the cart.

Instead, I got:
Empty cart after switched language from European site to US site (from fortuny.shop to fortuny.us)

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,
Itamar.

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.
So how can I get the same result without using a whole lang switcher, but just using a simple link to a single language instead?

Thanks,
Matteo

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.
Itamar.

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.
Can you please also explain to me where I can find this code in your theme's files?

If you need further help with this, please share the access details to your site with me.
I'm enabling a private message for the next reply.

Privacy and Security Policy

We have strict policies regarding privacy and access to your information. Please see:
https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

**IMPORTANT**

- - Please make a backup of site files and database before providing us access. --

Thanks,
Itamar.

December 15, 2019 at 12:51 pm #5133225

FortunyShop

Ok so I whitelisted Israel in the firewall.
Thanks

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,
Itamar.

2019-12-15_17-30-45.jpg
December 16, 2019 at 9:32 am #5137041

FortunyShop

HI,
I tried both solutions, GET and POST but cart is not synced anyway, even though in this article it says that it should work:
https://wpml.org/documentation/getting-started-guide/language-setup/passing-session-data-between-languages-in-domains/

What else could be?
Thanks

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:
A. Something is wrong with your code.
B. The cache plugin on your site is preventing the cart content from being synced.

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,
Itamar.

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:
- switched to default code as you suggested - WORKED
- went back to my code with only the US flag - WORKED

In the live site (languages divided in different domains):
- switched to default code for a moment, displaying all flags - DIDN'T WORK (cart is still empty in the other domain)
- went back to my code with only the US flag - DIDN'T WORK
- activated default language switcher for a moment - WORKED and passed the items in the other language cart

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?
Thanks

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.
Can you also see this?
But I think that it is happening only when I'm logged in as a user. The cart content is not syncing if I try it on another browser. All this still leads me to believe that it is a cache issue.

Do you have the option to clear and disable the cache on the live site and test this?

2019-12-17_20-39-28.jpg
December 18, 2019 at 9:55 am #5153983

FortunyShop

I did this in live site:
- cleared all cache and disabled WProcket
- set Cloudflare in Dev Mode in all domains (so cache is disabled there too)
- added item to cart in fortuny.shop (€)
- switched with custom button to US store (to fortuny.us in $), cart is empy

So cache must not be the problem here, then:
- activated back all caches
- enabled "Show language switcher in footer" option
- switched languages (fortuny.shop > fortuny.us and back) -> cart is always maintaining items and changing currency correctly

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.
https://wpml.org/documentation/getting-started-guide/language-setup/custom-language-switcher/#language-selector-with-flags-only
When the Language URL format is set for a domain per language, the cart content is not being synced if you are not logged in as a user. But if you are using WPML's builtin Language Switcher, the cart's content is being synchronized.

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.
https://wpml.org/purchase/support-policy/

If needed, I would suggest you consider contacting one of our certified partners from this link: https://wpml.org/contractors/
Before doing so, please have a look also here:
https://wpml.org/documentation/about-wpml-contractors/what-type-of-work-you-can-expect-from-contractors/
With our contractors, you might get the custom assistance you need to get on with your project.

Best Regards,
Itamar.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.