1

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?

5 Answers 5

2

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;
}
2
  • @Anime your u using single website or store for all country?
    – Abdul
    Jan 18, 2018 at 11:12
  • for now 1 website 1 store . but in future multuple
    – P S
    Jan 18, 2018 at 14:38
1

You can use GeoIP module like:
https://www.mageworx.com/geoip-location-magento-extension.html

This one is for free and should be enough for Your needs :)

EDIT:

Specific countries for config are mostly related to shipping country address. For example users from whole world may log in into Your shop but they can ship only for specific countries. There is no native Magento functionality that may show You current country of customer.

GeoIp use current user's IP for recognize theirs location.

However if You want manage registered customers, You can use saved customers addresses

$address = Mage::getModel('customer/address')->load($customerAddressId);
$country_id = $addressess->getCountryId();
$countryModel = Mage::getModel('directory/country')->loadByCode($country_id);
$countryName = $countryModel->getName();

Using this method You will be able to manage only logged customers.

4
  • Okay let me check. But is there no short codes in magento or php for this purpose?
    – P S
    Jan 18, 2018 at 9:43
  • please check my updated question . may be you can help.
    – P S
    Jan 18, 2018 at 9:48
  • I just edit my post. Using Abdul method should also works, however still depends on GeoIp (but using remote api) Jan 18, 2018 at 10:39
  • it is also an answer and thanks for explaining allo specific.
    – P S
    Jan 18, 2018 at 14:43
0

Simple: if the user is from the EU and has less than € 350 in the cart

<?php $countCart = $this->helper('checkout/cart')->getSummaryCount(); //get total items in cart $totalCart = $this->helper('checkout/cart')->getQuote()->getGrandTotal(); //get total price $visitorCountry = Mage::helper('geoip2/maxmind')->getGeoLocation($_SERVER['REMOTE_ADDR'])->countryCode; $eu=array('AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'SP', 'SE', 'GB'); ?> <?php if ((in_array($visitorCountry, $eu)) && $totalCart < '350') : ?> <p class="alert-box note-msg">Free shipping for orders above 350€.</p> <?php endif ?>

0

Hello you can use below code to get country code of current visitor or user

$om = \Magento\Framework\App\ObjectManager::getInstance();
$remoteAddress = $om->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$remoteAddress->getRemoteAddress()));
echo $ipdat->geoplugin_countryCode;

Hope this will help you. Happy Coding !!!

0

Execute this from your custom controller function, using ipstack, Get your API key from your ipstack account.

public function execute()
{
    $visitorIp = $this->getVisitorIp();
    // YOUR_API_KEY : Get the API key from https://ipstack.com
    $url = "http://api.ipstack.com/".$visitorIp."?access_key=YOUR_API_KEY";
    $this->_curl->get($url);
    $response = json_decode($this->_curl->getBody(), true);
    // return visitor country
    return $response['country_code'];
}

/**
 * Function to get the visitor ip
 * @return text
 */
function getVisitorIp() {
    $remoteAddress = $this->objectManager->create('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
    return $remoteAddress->getRemoteAddress();
}

complete custom module available here : https://github.com/Nayana-chandran/visitor-country-code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.