This article was last updated in May 2021
Due to how WP Engine's caching system is configured, it can interfere with features like geolocation and currency selection, by storing and serving the same content to all visitors. In some cases, a visitor from the US could be seeing content for Germany, while a visitor from the UK could be seeing content for Canada. This happens because WP Engine's caching stores a single copy of each content (e.g. a product page), which is what every visitor ends up seeing. Plugins can't fix this (and shouldn't), as they are not even loaded when the incorrect content is served.
We're aware of that issue, as we encountered it, and tried to address it, for quite a while. The WP Engine Team should also be familiar with the behaviour, as many of our customers who run WooCommerce site on the WP Engine service had to implement some fixes to address the caching logic.
The possible solution can be summarised as follows:
1. Configure the caching service to take into account visitor's currency
We have an article explaining how to configure the caching service to serve the correct content (see How to add dynamic caching to your site), depending on the absence/presence of the active currency and/or active country cookies. The logic is the following:
We have been informed that some of WP Engine support staff tried to exclude the Currency Switcher cookie from the cache instead. That's not necessarily effective. The approach of "excluding the cookie from the cache" means "skip the cache when the cookie is present". That is:
This is the opposite of what our article describes, and it might not work correctly. The cache should always be used when the cookie is present (taking into account the value of the cookie), and skipped when the cookie is absent.
Skipping the cache when the cookie is present can yield wrong results. It can also have a significant impact on performance, as aelia_cs_selected_currency must be set 100% of the time to identify the content to be served. That would effectively mean always skipping the cache.
2. Use our Cache Handler, as a workaround, after configuring the cache to take into account URL arguments
Our Cache Handler (also documented in the article linked in the previous paragraph) works by running a script on each page, which generates unique URLs for each page. Those URLs have an extra argument appended to them, whose value changes depending on elements such as the active country and currency. This workaround works in most cases, but please be aware that, on WP Engine, in the past it caused infinite redirect loops on some sites (see section 2.1, below). When that happened, it turned out to be due to how WP Engine's caching was configured:
In another case, the WP Engine server even seemed to try acting "too intelligent". It noticed that visits to https://yoursite.com were redirected to a different URL (e.g. yoursite.com/?ph=2314865892346), so it decided to perform the redirection itself, on the server side. That caused another glitch:
This made it look like the Cache Handler was redirecting the users to the wrong location, whereas it was the server itself doing that, behind the scenes.
2.1 How WP Engine tried to fix the redirect loop issue
WP Engine tried to "exclude the ph argument", introduced by the Cache Handler, from the cache. Based on our understanding, that would have meant "don't serve cached content when the ph argument is present." As far as we know, that didn't always work, as visitors kept receiving cached content. We speculate that this is because the URL argument is ignored anyway by the server, like described in the "infinite loop" scenario. Besides, even if it worked, skipping the cache when the "ph" argument is present would defeat the purpose of having a dynamic URL in the first place, as the behaviour would become the following:
The result would be the same as skipping the cache when the currency cookie is present, which basically means "always".
Ideally, the issue caused by the caching layer would have to be solved at that level. The Currency Switcher can't do anything about that, as the wrong content is served by the WP Engine server before the plugin is even loaded. This gives the following options to fix the issue:
- You could try to contact WP Engine, to see if they can configure their caching service as we described in our knowledge base, but it's possible that they can't do it. We have been discussing this kind of issue for years now, and they always told us that they can't add site-specific settings to their caching layer (other hosting providers, such as Kinsta, are more flexible, from that perspective).
- If WP Engine doesn't want to configure the caching service as described, you can try using our Cache Handler, as described above, keeping in mind that there may be some glitches to address. Those issues don't depend on our plugin either, it's just how the server works.
- If the Cache Handler doesn't work in "redirect" mode, because WP Engine's servers keep serving the wrong content, you can try to enable the "Ajax" mode instead. That mode has a couple of drawbacks:
- It fires up to three Ajax calls at every page load, to fetch the correct content. These requests must be uncached, and that can increase the load on the server. WP Engine might complain about that.
- Pages would load with some prices, which would be replaced with the correct ones after a few moments. This can be noticeable, as the prices would change on the page.
- The Ajax mode can only update "standard" prices generated by WooCommerce, e.g. for products, price filters and so on. It can't "see" prices introduced by custom fields, such as product addons, options, and so on. If these prices are visible on the site, they won't be refreshed.
- As a last resort, you could disable the WP Engine page caching altogether. That would prevent all the issues caused by caching, but it would have an impact on performance.
- As an "after last" resort, you could consider switching to a hosting provider with a more flexible caching solution. Kinsta is currently our managed hosting provider of choice, and they are happy to configure the caching layer as you need. They did that for many of our customers already.
Alternatively, you could use a VPS, on which you can install Varnish, and configure that service to serve the correct cached content. The article linked earlier (How to add dynamic caching to your site) includes a section about Varnish, which points to the documentation written by one of our collaborators, Mike Andreasen, whom you can hire on Codeable. He's an expert in setting up the Varnish caching logic and can configure it for you.