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

Last updated by John 1 year, 8 months ago.

Assigned support staff: Raja Mohammed.

Author Posts
February 26, 2019 at 1:07 pm #3244983

John

Hello there,
So i'm using the above plugin to display price based on customer country; but I would like to know if it is possible this scenario: so a client visit my shop and automatically detects the shipping country to US is there any way to also change the language based on shipping in this ex to English.
Other example will be: user from France access the website PCB auto sets the shipping country to France and the language if it is available should be also FR or fall back to EN.
So to sum up in a way if it possible WPML to depend somehow to PCB. (or WPML to depend on WOOcommerce geolocating shipping country to be more precise).

Waiting for your answer. Hope this is possible.
All the best.

{code}
add_action( 'wc_price_based_country_before_frontend_init', 'customer_country' );
function customer_country(){
if( ICL_LANGUAGE_CODE == 'fr' )
{
wcpbc_set_woocommerce_country('FR');
}
}{/code}

Ps. I don.t know what happened to actual ticket
https://wpml.org/forums/topic/price-based-on-country-switcher-plugin-wpml/

February 27, 2019 at 9:46 am #3249309

Raja Mohammed
Supporter

Languages: English (English )

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

Hi There,
Welcome to our forum,

To switch the language based on user shipping country you might have to add the bit of code hooked up to the function which changes the shipping county based on user country.

global $sitepress;
 /* $lang  language code based on the country, it's up to you to decide how to get language code based on country */
    $lang = 'nb';
    $sitepress->switch_lang($lang, true);

I'm really not sure what happened to your previous ticket, we don't close any unanswered tickets up to 10days. However, I will try to resolve your issue in this ticket.

Kind regards
Raja

February 27, 2019 at 9:52 am #3249403

John

Hey Raja, thanks for your reply.
I have added the code but nothing happens.
I only have 2 languages on my website: Eng and Romanian, so the only thing i need to do is when the Shipping Country is set to Romanian wpml to auto change to that language.
The rest shipping country should go to eng vers. 😀
Hope it is more clear.
Waiting for your answer. Thanks in advance.

February 27, 2019 at 9:55 am #3249425

Raja Mohammed
Supporter

Languages: English (English )

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

Hi John,

Please send me a screenshot of the code or paste the entire code here where it is placed. it can give me a better idea.

Kind regards,
Raja

February 27, 2019 at 9:57 am #3249430

John

Hope it is ok thanks.
hidden link

February 27, 2019 at 11:56 am #3249970

Raja Mohammed
Supporter

Languages: English (English )

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

HI John

Your code is checking whether the language code is "RO" and sets the language to "RO" which doesn't make any change. The switch language code should be placed in country switch hook please check with the PCB plugin author for the correct hook to add the language switch.You can ask them about the action hook that triggers after county change.

Kind regards
Raja

March 4, 2019 at 3:22 pm #3269548

John

Hello there. I have contacted the plugin author but he doesn't respond yet...any chance to find those hooks by myself?

March 5, 2019 at 5:25 am #3271725

Raja Mohammed
Supporter

Languages: English (English )

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

HI John,

Please check if you can find any documentation related to the plugin. We can help you with the appropriate code and its placement. Unfortunately, we cannot find any place to look out for available hooks for you. only your plugin author can advise on this.

Kind regards
Raja

March 8, 2019 at 3:13 pm #3293514

John

Hello Raja,
Here is the response from OScar from PCB.
Please tell me what i need to do to achieve language switching by shipping country.
Thanks again for your support, all the best.

--------
You can get the customer country with the function wcpbc_get_woocommerce_country(). You have to get the customer country after WooCommerce has been initiated, use the hook woocommerce_init.
I hope this helps you.

March 11, 2019 at 5:20 am #3307781

Raja Mohammed
Supporter

Languages: English (English )

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

Hi there,

Try the below code, add in the theme functions.php file.

add_action('wp_loaded','customer_country_base'); 

function customer_country_base() {
	
	$user_country = wcpbc_get_woocommerce_country(); 

	$lang = ICL_LANGUAGE_CODE;


	/*
	* map languages for country
	*/
	

	switch ( $user_country ) {
		case "US" : // if country is US
		$lang = 'en'; //  language is set to english
		break;

		/*
		* Add more checks
		*/

	}

	global $sitepress; 
    $sitepress->switch_lang($lang, true);


}

I hope this helps you better

Kind regards
Raja

March 11, 2019 at 4:09 pm #3314639

John

Raja you are the man. Thanks for your support really appreciate. The code works flawless and i'm using different domains per language.
Really appreciate for everything. This help me a lot.
You the best.

March 11, 2019 at 4:09 pm #3314641

John

My issue is resolved now. Thank you!