Magento – Get the country code of website user

country-regionsmagento-1.9

I want to get the country code of my website user so that I can show and hide some blocks in my code. How may I do that? For example if someone is browsing our website from Australia I should get AU.

I think there must be some code in Magento to know the country's location. If this feature is not out of the box in Magento then what is the purpose of having allow specific countries in almost every configuration? And how does allow specific countries work?

Best Answer

Try this:

$ipAddress =  $_SERVER['REMOTE_ADDR'];
$json       = file_get_contents("http://ipinfo.io/{$ipAddress}/json");
$details    = json_decode($json);
if(isset($details->country)) {
    echo $details->country;
}
Related Topic