Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Can Magento show different store front/products for different ip addresses? 
 
Matt Smith
Jr. Member
 
Total Posts:  4
Joined:  2008-08-06
 

With my limited Magento experience I was hoping someone might be able to answer this question.

Can Magento filter products or a store front based on someones location/ip address. For instance we want to sell certain products to Denver only and filter out the same products for other states. Can this be accomplished? If so how?

One last question is could you make separate store fronts and filter those by IP/state also?

Thank you so much for the help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
AdamV
Jr. Member
 
Avatar
Total Posts:  2
Joined:  2009-07-30
 

Matt,

Its been over a year but I think I have your answer.

Yes.

What you need to do is edit the index.php and put the logic there. Here’s some throw away code I wrote to fetch a country code for an IP and redirect to a store whose code matches the country code:

function getIP() {
$ip;
if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = “UNKNOWN”;
return $ip;
}

function countryFromIP(){
$ip = getIP();
$d = file_get_contents("http://www.ipinfodb.com/ip_query_country.php?ip=$ip&output;=xml");
//Use backup server if cannot make a connection
if (!$d){
$backup = file_get_contents("http://backup.ipinfodb.com/ip_query_country.php?ip=$ip&output;=xml");
$answer = new SimpleXMLElement($backup);
if (!$backup) return NULL; // Failed to open connection
}else{
$answer = new SimpleXMLElement($d);
}
return strtolower($answer->CountryCode);
}

function getStoreForIP()
{
$country = countryFromIP();
if (isset($country)) {
$stores = Mage::app()->getStores(false, true);
if (isset($stores[$country]) && $stores[$country]->getIsActive()) return $stores[$country];
}
Mage::app()->getStore();
}

if (preg_match("/^\/\?/",$_SERVER[’REQUEST_URI’]) && isset($_GET[’fake_ip’])) {
header(’Location: ‘.getStoreForIP()->getBaseUrl());
exit;
}

it was a throw away tests so your milage may vary.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cgfc
Sr. Member
 
Avatar
Total Posts:  92
Joined:  2008-08-29
Montevideo, Uruguay
 
AdamV - 23 September 2009 10:07 AM

Matt,

Its been over a year but I think I have your answer. .....

Thanks man! Very helpfull!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top