Tuesday, January 18, 2011

Magento - Auto Store switcher using GEOIP



Prerequisites:
  • GEOIP (geoip.inc, geoip.dat etc)
  • Signup for an account in www.ipinfodb.com
  • Download the php class api HERE

  • 1. put the geoip files (preferably in a folder) in root directory
  • 2. edit the index.php of the root directory and replace these code
  • /* Store or website code */
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
    
    /* Run store or run website */
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
    
    Mage::run($mageRunCode, $mageRunType);
    
  • with these
//########### GEOIP ############//
$geoipPath = 'geoip.inc';
include($geoipPath);

$gi = geoip_open("GeoIP/GeoIP.dat",GEOIP_STANDARD);

$ip = $_SERVER['REMOTE_ADDR'];
$country_code = geoip_country_code_by_addr($gi, $ip);

if(strtoupper($country_code) != "NL"){
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'en';
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

    Mage::run($mageRunCode, $mageRunType);
}else{
    $mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : 'nl';
    $mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';

    Mage::run($mageRunCode, $mageRunType);
}

4 comments:

  1. Magento has auto store switcher using GEOIP function and there are many modules available on web, I have not saw this function anywhere before. Auto Store Switch is key factor in magento module that cannot provide others.

    Alan Smith..
    Magento Block IP address

    ReplyDelete
  2. Great tutorial! I also saw many modules for implementing this functionality, I myself use this one https://amasty.com/magento-geoip-redirect.html

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Great Read! I am impressed on how you make your article easy to understand. I'll come back for more :D

    Japs Buidon is a Social Media Specialist and belongs to a team of Magento Developer in Florida. For more tutorial and tips you can follow him here -> alwaysopencommerce.com

    ReplyDelete