Magento – Hide content based on IP address

Photo of author
Written By Ryan Rana





This post will be useful to anybody who has been asked to hide or display Magento content (specifically prices and add to cart stuff) based on visitor IP. As a kick-off you can read my initial Stackoverflow question on it.

I can’t say who the client was (NDA) in this case but here’s the synopsis;

They are a UK based manufacturer and they don’t want to compete with their worldwide supply chain, so they want;

1. All users with IPs originating in North America get redirected to a totally different website (non-Magento, nothing to do with me) ideally with a redirect message.
2. All user with IPs originating anywhere other than North America, The UK and Ireland get to see the site but with the pricing and cart disabled so they don’t see prices and can’t make a purchase.
3. UK and Ireland operate as normal.

Based on Tim’s SO answer I used his module. It basically uses the Maxmind IP list, which gets downloaded to the server as a dat file


So the IP list is not hardcoded and can be updated, and the database doesn’t needed to be amended. It can be setup to periodically download the updated list from MaxMind using the Magento CRON.

The IPs are mapped to the two letter ISO 3166-1 code country codes which Magento uses. It is tied into the Magento list of Allowed Countries (in my case it’s UK and Ireland). By default Magento allows you to control which countries you sell to, so you just use the methods supplied in the module to ensure that only those countries see the cart, prices and configurable options (ie anything relating to e-commerce). This includes the top cart in the masthead. So anyone visiting on an IP that isn’t on that list won’t see the prices or add to cart stuff. And to change that list you would just add that country to the list in the Magento backend. Easy, all tied into the native functionality.

            <?php $geoIP = Mage::getSingleton('geoip/country'); ?>
            <?php if($geoIP->isCountryAllowed()) : ?>

                <?php echo $this->getChildHtml('product_type_data') ?>

			    <?php echo $this->getTierPriceHtml() ?>
            <?php endif ?>  

There is a performance overhead to checking these IPs but it uses Magento Singletons to keep that as low as practically possible.

You can check this out using proxies like Hide My Ass (there’s a Chrome extension) or I can temporarily remove the UK from the list so that it can been seen by the client when he’s checking.

Leave a Comment