Magento GeoIP Extension

·

·

Magento has a nice feature to allow checkout for visitors only from a pre-defined list of countries. But what if one wants to allow access to entire Magento or just some Magento parts to those countries? Magento is determining visitors country by looking into customers address. But visitor can be also not logged in so the only way to get visitor’s country is to look at IP address. Determining country by IP address is possible with GeoIP Lite database by MaxMind. Here I’m presenting a bridge-extension which will add this database to your Magento installation.

Magento GeoIP extension can be installed either through MagentoConnect or manually from GitHub. The extension itself will not add any new functionality but will give you tools to connect MaxMind GeoIP Lite database to your Magento installation and use it to check the country of your visitors.

Before installing the extension make sure the `var` directory of your Magento installation is writable by web-server user (by default it should be). If you want GeoIP database to be updated automatically make sure your Magento cron-job is set up and running. Re-logging into administrator interface after extension installation is not required.

After installation of the extension you will see a new field under `System\Configuration\General\General\Countries Options` group indicating the date of last database update and the button to force synchronization.

Note that frequent synchronizations will result you being banned from acessing MaxMind server for several hours. MaxMind GeoIP Lite database is updated on first Thursday of the month so if you have have Magento cron-job running you will not need to force synchronization anymore but on the first run.

As I told extension itself is not doing anything (yet) to limit users access to your Magento installation. However if comes with a set of methods you can implement in your template or extension:

    $geoIP = Mage::getSingleton('geoip/country');

    /**
     * Returns you the ISO 3166-1 code of visitors country.
     */
    echo $geoIP->getCountry();

    /**
     * Returns true or false depending if current visitors country is among allowed countries.
     * If there are no allowed countries in the system returns true.
     */
    if ($geoIP->isCountryAllowed()) {
        // ...
    }

    /**
     * Adds country (or array of countries) to list of allowed countries.
     */
    $geoIP->addAllowedCountry('DE');
    $geoIP->addAllowedCountry(array(
        'US',
        'CA'
    ));

    /**
     * Or just get a country code of a specific IP
     */
    echo $geoIP->getCountryByIp('94.230.212.77');

Please note that `geoip/country` is initialized on each Magento load so in order to save system resources please always use it as a singleton.

Allowed countries are initially popped from `System\Configuration\General\General\Countries Options\Alowed Countries` multi-select field of your Magento configuration. The `addAllowedCountry` method is not adding country to system configuration but adds it to internal list used at `isCountryAllowed` method.

That’s it. Any ideas about improving this extension, bug reports and feature requests are highly appreciated.

(Photo courtesy of photos.icons8.com)