Many website owners will use a page cache to reduce load on their webserver and reduce delivery time for the HTML of their site pages to visitors’ browsers. Some website owners will use more than one – a WordPress page cache to reduce the time that PHP takes to produce the final HTML of the page, and then a hosting-level cache to avoid having to contact the webserver at all.
Complications arise if your website has content that differs for visitors in different situations. The general aim of a cache is to quickly return identical content, avoiding the need to re-generate the content from the beginning. But if visitors have variations that means that identical content is not suitable, then the cache will need to keep multiple copies of the content. In the case of selling from WooCommerce, examples of such situations are a need to display prices to a visitor based upon their country (i.e. the country of their IP address) as soon as they first visit the site, and/or to remember which country they chose as their taxation country from a drop-down on the site and adjust prices or tax display based upon that.
Configuring third-party products, including caches, is beyond the scope of support for our plugin – instead, you should ask for support from the providers of those products. But what we can do is give the general information needed to help with the specific implementation.
Firstly, any WordPress page cacheing plugin needs to support a per-country cache, i.e. supporting storing one version of a page per country. Without this, you cannot cache any pages on your site on which content varies according to the visitor’s country – i.e. you will receive none of the benefits of caching, because there will be no caching. Some very popular plugins do not support this. We recommend WP Optimize Premium, which supports this and several customers have told us they are using successfully.
You may wish to cause the VAT plugin to consider the visitor’s country to automatically be the same as that which the caching plugin has detected. Otherwise, your caches may contain anomalous content in which the cached page for a particular country in the caching plugin contains content for a different country, because the two plugins used different detection methods. (Detecting a visitor’s country upon their visit to the site is not an exact science, and VPNs, databases that aren’t fully up-to-date, proxies in other countries, and other factors can play a part). For WP Optimize Premium, here is the relevant code to add as a snippet in your site:
add_filter('wc_eu_vat_compliance_get_visitor_country_info', function($country_info) { if (function_exists('wpo_cache_get_visitor_country_code')) { $country = wpo_cache_get_visitor_country_code(); if ($country) { $country_info['data'] = $country; $country_info['meta'] = 'wpo_cache_get_visitor_country_code'; } } return $country_info; });
If you are using a different caching plugin, then you will need to ask the supplier of that plugin to help you develop an equivalent piece of code; the code for each caching plugin will be different.
If you have an external cache (i.e. a cacheing proxy) in front of your website, then you will also want to configure this cache to store separate versions depending upon the country selected upon the site (otherwise again, all pages with country-specific content will be undesirable for serving to visitors from different countries).
Again, how this is done will depend upon what product the external cache is, and you should speak to the people who are running it and/or product support for it. One approach that a customer has used successfully is to automatically set a cookie for the visitor’s country when they first visit the website, and then to configure the cache to store different cached versions for different values of that key. With a LiteSpeed cache, they configured the cache with this rule:
<IfModule Litespeed> RewriteRule .* - [E=cache-vary:vat_preselect_country] </IfModule>
Then, on the website, this code fragment was used to issue the header:
add_action('init', function() { if (defined('WP_CLI') && WP_CLI) return; if (!function_exists('WooCommerce_EU_VAT_Compliance')) return; $country = WooCommerce_EU_VAT_Compliance('WC_VAT_Compliance_Preselect_Country')->get_preselect_country(); if (is_string($country)) setcookie( 'vat_preselect_country', $country, ['path' => '/']); }, 11);
Please note that we cannot produce code for you as part of standard support, since our plugins have cheap/commodity prices. Trusted and reviewed PHP/WooCommerce developers, if you need one, can be found at places like Codeable.
Posted in: WooCommerce European (EU/UK/Norway/Switzerland) VAT Compliance