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 6 replies, has 2 voices.

Last updated by Bigul 1 year ago.

Assigned support staff: Bigul.

Author Posts
October 10, 2018 at 12:43 pm #2804268

andersF-6

Hi there!
I'm doing some custom development and trying to use the wpml_language_has_switched hook, but it's not being triggered when the language is switched.

For language switching I'm using the language menu bar.

Thanks!

October 11, 2018 at 8:08 am #2807096

Bigul
Supporter

Languages: English (English )

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

Hello,

Welcome to the WPML support forum. I will do my best to help you to resolve these issues.

Please share me more details about the requirement for better understanding. So we can try to reproduce the issue on our test installations.

It will be great if you can share the custom code you are using in the *wpml_language_has_switched* hook.

--
Thanks!

Bigul

October 11, 2018 at 1:10 pm #2808309

andersF-6

Hi!

Here's our code 🙂 we're trying to clear the cart contents when the language is switched. I've tried the built-in prompt but that works very poorly.

add_action("wpml_language_has_switched","clear_cart");
function clear_cart(){
if( function_exists('WC') ){
WC()->cart->empty_cart();
}
}

October 12, 2018 at 9:00 am #2811305

Bigul
Supporter

Languages: English (English )

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

Hello,

Thank you for the details. I have tried it my test installation with Storefront(from WooComemrce) and 2017 theme, the cart is emptying while switching between the pages in both cases. Please try with the following code after a site backup and check the issue is existing or not.

add_action("wpml_language_has_switched","clear_cart");
function clear_cart(){

global $woocommerce;

$woocommerce->cart->empty_cart(); 

}

If possible, please check the issue is existing(after a site backup) with the default theme(for an example 2017) or not.

--
Thanks!

Bigul

October 12, 2018 at 1:31 pm #2812162

andersF-6

Hi!

This hook isn't working as we intent to use it. It will clear the cart when the page is refreshed, but we'd like to empty the cart when the language is switched.

Thanks

October 13, 2018 at 4:50 pm #2814355

Bigul
Supporter

Languages: English (English )

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

Hello,

Thank you for the feedback and details. I am escalating the ticket to our second tier team for further debugging. We will get back to you as soon as possible. Please wait.

--
Thanks!

Bigul

October 16, 2018 at 2:18 pm #2821360

Bigul
Supporter

Languages: English (English )

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

Hello,

For your kind information, this issue is happening because WPML or WooCommerce Multilingual or other extensions may switch the language multiple times when loading page. For an example, may it will switch to secondary language and get the results then switch back again to the default language.

So as a workaround you can use wcml_user_switch_language hook. But you have to create a custom plugin for this purpose and use the following code. Because maybe this hook is called even when the theme is not loaded.

add_action('wcml_user_switch_language', function(){
    add_action('wp_loaded', function (){
    global $woocommerce;
    $woocommerce->cart->empty_cart();
    });
});

Please try it after a site backup and let me know your feedback.

--
Thanks!

Bigul