Load Magento Store on The Basis of Client IP Detection
- Digital Engineering
Load Magento Store on The Basis of Client IP Detection
Magento let us set up multiple stores on a single instance. For a global website it is good to have multilingual and multi-currency stores for different countries. It provides a good user experience if website automatically detects user’s region and shows prices accordingly , will build confidence in user and will add a extra pie to user’s buying experience.
This is how you can do this –
If you are managing stores under a single website then write below code in your root’s index.php.
If you have multiple websites and each website has multiple stores then create a separate folder for your website and create index.php for each website.
Below is the code to run your store on the basis of IP detection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
function getCountryCodeByIp($ip) { // API call to get Country code of requested ip $url="http://api.ipinfodb.com/v3/ip-country/? key=3c1d963333567e3dc050d75f5de6ef3817ea852152644a388ef817498d6b6eec&ip=".$ip; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 400); $data = curl_exec($ch); curl_close($ch); if(curl_errno($ch)) { return 'US'; } $county_conf=explode(';',$data); if(@$county_conf[3] && !empty($county_conf[3])) { return $county_conf[3]; } else return 'US'; } |
//by default this function return US as default country if some error occurred in getting the country code.
//to work above function,curl need to be enabled on your server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
function get_client_ip() { $ipaddress = ''; if ($_SERVER['HTTP_CLIENT_IP']) $ipaddress = $_SERVER['HTTP_CLIENT_IP']; else if($_SERVER['HTTP_X_FORWARDED_FOR']) $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; else if($_SERVER['HTTP_X_FORWARDED']) $ipaddress = $_SERVER['HTTP_X_FORWARDED']; else if($_SERVER['HTTP_FORWARDED_FOR']) $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; else if($_SERVER['HTTP_FORWARDED']) $ipaddress = $_SERVER['HTTP_FORWARDED']; else if($_SERVER['REMOTE_ADDR']) $ipaddress = $_SERVER['REMOTE_ADDR']; else $ipaddress = 'UNKNOWN'; return $ipaddress; } |
//use this function to get the client IP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
if(isset($_COOKIE['countryCode'])) $c_code=$_COOKIE['countryCode']; else { $ip_address=get_client_ip(); $c_code=getCountryCodeByIp($ip_address); $cookie_domain=Mage::getStoreConfig('web/cookie/cookie_domain','any_store_code'); //you need to set cookie with domain.so just get the cookie domain of any store of the current website. setcookie('countryCode',$c_code,time()+(86400*7),'/',$cookie_domain); } if($c_code=='IN') { Mage::run('store_india_code', 'store'); //use your actual store code } else if($c_code=='US') { Mage::run('store_us_code', 'store'); //use your actual store code } |
//cookie is used so that you don’t need to hit the api again and again for getting the country code.
You can place multiple if else condition if you have few number of stores.
If you are managing a big number of store then placing multiple if-else condition is not a good idea. In that case,you just map you stores with the country by creating a module or by simply having a table which has store_code and country_code column.
Fetch the store code from the DB of the Country and put in the store run code as below.
Mage::run($store_code,’store’);
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s