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.

//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.

//use this function to get the client IP.

//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.