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?

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;
}
  • @Anime your u using single website or store for all country? – Abdul Jan 18 '18 at 11:12
  • for now 1 website 1 store . but in future multuple – Purushotam Sangroula Jan 18 '18 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.

  • Okay let me check. But is there no short codes in magento or php for this purpose? – Purushotam Sangroula Jan 18 '18 at 9:43
  • please check my updated question . may be you can help. – Purushotam Sangroula Jan 18 '18 at 9:48
  • I just edit my post. Using Abdul method should also works, however still depends on GeoIp (but using remote api) – Konrad Siamro Jan 18 '18 at 10:39
  • it is also an answer and thanks for explaining allo specific. – Purushotam Sangroula Jan 18 '18 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 !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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