Search code examples
magento

Magento - Hide prices based on IP


I am due to put a Magento site live and the client has sprung a last minute request. They are a UK based manufacturer and they don't want to compete with their worldwid 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.

I'm trying to work out the best way to approach this. I am aware of three options;

  • .HTACCESS

I've worked on a store before where the developer has set a huge list of IPs in the .htaccess (mainly blocking China and Russia as a security thing). I could do something similar, redirecting users with USA IPs and then maybe setting an environmental variable like so;

How to set a custom global from .htaccess

Which could be accessed in the templates to not display the add to cart and price markup.

Pros: Relatively Easy

Cons: Horrible. The list of IPs would be hardcoded, and the conditional code to display/not display the prices etc would be template based rather than done via a module. Plus the 'You are being transferred to our USA store' request couldn't be handled this way.

  • Store switching using commercial module

Another option could be to customise/configure the following commercial module to handle the request;

http://www.mageworx.com/store-and-currency-auto-switcher-magento-extension.html

I'm thinking this would involve setting up new websites rather than new stores (as the current installation already has 1 Website and 2 Stores each with one Store View.). The new websites would have their own theme with no cart/price info displayed.

Pros: IPs not hardcoded, uses the MaxMind GeoIP database. Module based.
Cons: Again the code whether to display the Prices is template based. Also this would add a lot of administration overhead for the user - extra websites, extra store views to complicate things. And not using 99% of the features of this module.

  • A Custom Module to handle it

I know this is the ideal solution but I am much happier in app/design than I am in app/code. I know how to build a module and have built very small modules before but.... Based on Alan Storm's blog post here;

http://magento-quickies.tumblr.com/post/32402056167/magento-startup-events

I am imagining I would use the controller_front_init_before event and create an observer which extends a class or classes relating to the isSaleable() method which is used in the product view templates anyway. Possibly??

Pros: This is the right way to do it. No admin overhead. Easy to disable/enable.

Any suggestions, pointers or links on this would be a big help. Thanks.


Solution

  • Here is a kick-off for what you called an "ideal solution".

    Get my free GeoIP extension from MagnetoConnect or GitHub. Then in any template use:

    $geoIP = Mage::getSingleton('geoip/country');
    ...
    <?php if($geoIP->isCountryAllowed()) ?>
        the block of code you want to display only for selected countries
    <?php endif ?>
    

    More info in my article here.

    If you have any question please feel free to ask.