Sometimes clients want a store, but for various reasons can’t actually sell online. Woocommerce works well for this; with it’s pre-built layouts for categories and single items. To do this universally, or restrict price visibility to only logged in users, the WooCommerce Catalog Visibility Options plugin does a great job. I’ve used it very successfully on wholesale sites where the users expect to have to login in able to shop.
But what about retail sites where you don’t want to force a login? That gets more difficult. The use case here is a retail site that sells online to the general public, but ONLY in North America. This client is expanding his product through other distributors in Europe and wants retail clients there to read about the products, but not be able to buy directly. The challenge here was to find a way to keep the store fully functional in the US and Canada, but to turn off prices and the buy buttons everywhere else.
By adding a geolocation plugin and a couple of new functions, I was able to make this work very nicely.
- Install the GeoIP Detection plugin. This plugin does have to be connected to an GeoIP database, I kept mine simple and went with the Maxmind default. I also had caching and a reverse proxy setup, so I checked both of those boxes as well.
- Write the functions. This was the “fun” part. I was inspired by an article by Josh Pollack, that really got me started in the right direction. The rest of the function is basically disabling all the price and button hooks found in the Woocommerce templates and replacing them. This code could be it’s own plugin, but I placed it in my themes’ functions.php file.
- Test, test, test. I highly recommend implementing this on a staging site first, you never want your store to disappear on a live site. The best way to find out if it’s working is to comment out the line with the country code that you’re in and see if the pricing disappears. For final testing, I found a site where I could fake my IP for another country and see what happened. I also did some real user testing and asked friends I had in Canada and France to take a look for me. When my friend in Canada could see the buy now buttons and my friend in France could not, I was confident that I could now implement this on the live site. Once I did, I had my friends user test for me again!