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)

12 Kommentare zu «Magento GeoIP Extension»

  1. Pingback: Unsere 5 meistgelesenen Blog Artikel | Openstream Internet Solutions

  2. Pingback: Top 15: Unsere meistgelesenen Blog Artikel im 2013 | Openstream Internet Solutions

  3. Hi Tim,
    Nice way to elaborate your blog regarding Magento GEO IP Extension.As you know online business strengthened its roots deeply across all over the world, so running an online business is itself a risk.that’s why you need some Plug-in that provide security to your store and Magento GEO IP Block Module http://bit.ly/1hLyIpu protects you in this regard. you can block any user ,city country or a region on your website. you can also block users to some pages as well.

  4. Hi,
    we initially used this on our 1.4 M version and was great we updated to 1.9 and all the problems started. index management no longer visible can not update geoip data base it just continues to load and nothing happens last update data not available on front end. And the default site is showing randomly around the world. help pls

    1. Unfortunately, we’re no longer using this for any client projects. If you don’t have a developer who can look into it, please email us with more details and we will be happy to send you a quote.

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert