WooCommerce 2.4 price cache – Pros and cons
If you wrote a WooCommerce plugin that works with product prices, and found out that it returns incorrect results when you process variable products, the cause could be the new price caching logic added in WooCommerce 2.4. This new logic, which is not extensively documented, was introduced to increase performance of sites with complex variable products (i.e. products with 20 variations or more).
To reduce processing and calculations, when WooCommerce retrieves the prices of a variable product , it stores them in a dedicated cache, and returns the stored data from that moment on, until the product is modified. When this happens, none of the hooks associated with product price calculations runs, and your code is will probably not run as it should. For example, if your plugin returns different prices depending on custom criteria, you will notice that this no longer happens, and that you will always see the same prices. Those prices come from the cache.
Two of our plugins were affected by this new caching mechanism: our multi-currency solution, the Currency Switcher, and our Prices by Country plugin. In both cases, we had to find a way to work around the limitation of having static prices shown to the customer, while still trying to keep the performance at a good level. Our plugins are now up to date, as we announced earlier, and we thought of sharing our approach, so that it can benefit other developers.
Based on our tests, there are two solutions to this issue. The first is the best compromise between flexibility and performance, while the second can come useful if your code absolutely needs to get “raw”, live data.
1. Change the cache key for the prices, depending on your criteria
This solution allows to keep the price caching in place, while still keeping the data dynamic, choosing the correct one depending on arbitrary criteria. For example, suppose that a variable product should show two different set of prices:
- Prices for wholesalers
- Prices for the public
Your criteria, in this case, would be “customer is wholesaler“. You can then use it to ensure that the correct prices are loaded for the product. You can find an example showing how you can do this here: WooCommerce 2.4 – Price cache workaround – Dynamic key.
2. Disable the price cache entirely
This solution is no longer applicable as of WooCommerce 2.5
This solution will always give you access to live product prices, but it will disable price caching entirely. We would recommend not to use it, unless necessary. You can find the code here: WooCommerce 2.4 – Price cache workaround – Disable cache.
The new price caching system was unexpected and, although we understand why it was introduced, it can cause quite a bit of confusion. We hope that our examples will help you getting your product back on track, and add full compatibility with WooCommerce 2.4. If you have any questions, please feel free to contact us. You can also leave your feedback in the comments section, below.
The Aelia Team