Senior PHP Engineer | Former E-commerce Website Manager | Full Stack Web Developer

Servers & networking equipment

Optimizing WordPress Caching for E-commerce

Caching is an important aspect of website optimization, but it can also cause unexpected issues if not implemented correctly. In one instance, a company based in the UK was using WordPress with the WooCommerce plugin to host their e-commerce website. They were displaying their prices with tax/VAT included, which led to problems with caching store pages. If a cached page was from a visitor in Germany, their VAT is 19%, so it would store that value, and if you visited that page while it was still stored in the cache, you would view their prices.

The ‘Work around’

To work around this issue, I came up with a plan to have a different cache for each country. However, having 195 different cache groups would cause performance issues and take up too much storage on the server. Therefore, I decided to have 29 cache groups, with the 28 countries that pay VAT having their own cache group, and the rest of the world using the 29th group. This reduced the impact on performance, as the most used cache group was “rest of world”.

CountryCache group
United KingdomUnited Kingdom
CanadaRest of world (Group 29)
United StatesRest of world (Group 29)
FranceFrance
GermanyGermany
Example of cache groups and how they would work

Falling into more issues

Another issue that arose was displaying tax for a shipping address. Prices shown by WooCommerce were first based on the visitor’s location, but if they changed their shipping address to another country, they would get the prices with tax from that country instead. This meant that visitors could view cached prices that were meant for a different country. To prevent this from happening, I added code to detect if a user has changed their shipping address to another country from a cookie that was created when it’s changed. Visitors would then start seeing cached pages for that country, as the only difference was the prices.

To ensure that visitors have a cached version of the page, I set up a cache preloader. However, caching all pages for 29 different groups would cause storage issues. To prevent this, I set up a preloader that would cache the top visited pages. Although there was manual involvement in setting up the preloader, I plan to automate it in the future.

Conclusion

In conclusion, caching can be a powerful tool for website optimization, but it requires careful planning and implementation. By optimizing WordPress caching for e-commerce, we can enhance website performance and provide a better experience for visitors.

Facebook
Twitter
LinkedIn
Pinterest
Email