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

Last updated by Raja Mohammed 4 months ago.

Assigned support staff: Raja Mohammed.

Author Posts
June 11, 2019 at 7:36 pm #3998301

stefanS-8

Hi,
I need a function, that changes wpml language, when I change woocommerce country delivery on checkout page.

I found JQuery funkction when get current country delivery, but I have also change website language with wpml.

This code show alert with current language when switch country delivery:

add_action('wp_footer', 'billing_country_update_checkout', 50);
function billing_country_update_checkout() {
if ( ! is_checkout() ) return;
?>
<script type="text/javascript">
jQuery(function($){
$('select#billing_country, select#shipping_country').on( 'change', function (){
var t = { updateTimer: !1, dirtyInput: !1,
reset_update_checkout_timer: function() {
clearTimeout(t.updateTimer)
},
trigger_update_checkout: function() {
t.reset_update_checkout_timer(), t.dirtyInput = !1,
$(document.body).trigger("update_checkout")
}
};
$(document.body).trigger('update_checkout');
console.log('Event: update_checkout');

var language=$(this).val();
alert(language);
});
});
</script>
<?php
}

I need to switch page language via wpml by variable "language".

Web have two language:
1. Slovak (currency EUR / language code SK)
2. Czech (currency KC / language code CZ).

If I switch country delivery to "Czech", wpml switch web to "CZ" language code. When I switch to "Slovak" country, web automaticly switch to "SK" language.

Regards,
Steve

PS: The opposite funkctions works very well - if I change wpml language in menu, it changes country delivery. I use this code (only for your information - I don't need help with this):

add_filter( 'default_checkout_country', 'change_default_checkout_country' );
function change_default_checkout_country() {
if(ICL_LANGUAGE_CODE=='sk'){
return 'SK';
}else{
return 'CZ';
}
}
add_filter( 'default_checkout_state', 'change_default_checkout_state' );
function change_default_checkout_state() {
if(ICL_LANGUAGE_CODE=='sk'){
return 'SK';
}else{
return 'CZ';
}
}

June 12, 2019 at 7:39 am #4001185

Raja Mohammed
Supporter

Languages: English (English )

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

Hi there,

I am not sure I could be of much help in this case Since your request is more of a custom coding which is beyond our support capability. However, I can help you with a suggestion to one of our WPML API

You can use the hook "wpml_switch_language" which could switch language, The action hook must be passed to the pre_get_posts WordPress hook. Detailed documentation can be obtained from the link below.
https://wpml.org/wpml-hook/wpml_switch_language/

kindly refer to the full list of WPML API
https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference

PS: In order to change the language, the checkout page will refresh and the user needs to enter the details again.

Kind regards
Raja

June 12, 2019 at 4:22 pm #4006099

stefanS-8

Hi,

I don't need development, I need info how to switch WPML to another language via JavaScript. Only simply information.

Or I need any WPML multi-currency hook, to switch price to country delivery.

I use this code bellow to switch currency symbol when I change country delivery. It is works fine, but this switch only currency symbol, not price. Does WPML have any hooks to change prices?

add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
  if ( ! is_admin() ) { 
    global $woocommerce;
    $my_country = $woocommerce->customer->get_shipping_country();
	if( $my_country == 'SK') {
		$currency_symbol = '€'; 
	} else {
		$currency_symbol = 'Kč'; 
	}
	return $currency_symbol;
  }
}

Regards,
Stieranka

June 13, 2019 at 6:57 am #4009731

Raja Mohammed
Supporter

Languages: English (English )

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

Hi Stieranka,

I'm not sure whether changing the language using a JS script is possible since language switcher requires updating several records including cookies and needs to trigger the WP_Quey this would need a page refresh to complete the operation. We don't have any JS API for language switcher functionality.

As mentioned in my previous reply you can make use of the action hook "wpml_switch_language"

Exchange rates are applied to the cart based on the client currency, Updating currency symbol has no effect since this action requires updating WooCommerce sessions there is no hook available for this action.
As an alternative, you can try the Ajax action "wcml_switch_currency" and use parameter currency with the currency code.

Since this requires in-depth knowledge about how WCML works you can either inspect our the plugin code or considered hiring one of our certified contractors for a paid work
https://wpml.org/contractors

Kind regards
Raja