WooCommerce 2.3 introduced a neat feature which allowed you to geolocate your customers to set their initial location, showing correct taxes and shipping values without the customer needing to input their address.

Although this worked well, we discovered that it was not fully compatible with static caching plugins. Here is an example of the problem:

1. Hans from Germany visits the store and looks at a product.
2. Hans sees the German taxed price.
3. The page is then cached.
4. Charles from the UK visits the store and looks at the same product.
5. As the page is cached, he sees German prices, rather than UK prices.

Additionally, if the entire site was cached, the geolocation would never actually take place because its powered by a PHP script which won’t run on cached pages. This can be confusing to customers.

We looked at possible solutions and came to the conclusion that JavaScript would be needed, in combination with a way of making pages unique based on user.

Geolocation, with a hint of Ajax

To solve this issue, we’ve added a new option to set the user’s location called “Geolocation with page caching support”. Here is how this works:

1. When a customer visits a page on the site, an ajax call is fired in the background which geolocates that user via ajax and returns their location.
2. The returned location is compared to the users known location. If the location is not yet known, or the location has changed, the page redirects back to itself with an extra querystring. e.g. v=12345 This is a hash based on the user location.
3. The hash causes cache invalidation – the new page (with querystring) will be cached separately.

If a customer from Germany visits a page, he will get a hash based on Germany – he will only see cached versions of pages from other German customers. This works around the caching issue, without needing to disable it completely.

The negative from this is the extra ajax request per page, however, this is necessary – without this, no geolocation would take place on a cached page.

This new feature will be available in WooCommerce 2.4 and can be tested by using our master branch on Github. The issue was reported here.