Caching and dynamic pricing – upcoming changes to the get_variation_prices method

get_variation_prices was a method added in 2.4 to make it easy to retrieve an array of variation prices for a variable product. With this array, you can output pricing information in many different ways; core itself uses for example $100 – $200 format for variable products (min price to max price). These prices are used for display only.

Since gathering this data requires looping over all variations in a variable product, this method utilises transient caching to keep things running smoothly. The name of the transient varies (using a hash) based on things like taxes to ensure cached prices stay valid regardless of the customer viewing the prices.

Unfortunately this does has implications for plugins which dynamically change pricing, because they change pricing, have it cached, and then all users see the cached prices. This leads to plugin conflicts.

You can read more about the problem here.

In an attempt to make these plugins work better together and with core, we’re going to be changing the way the hash gets generated, and change the filters used to modify the prices in the array.

Instead of relying on for example $product->get_price(), we’re going to store the RAW meta values ($product->price), but run them through some new filters specific to variable products. These being:

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

If you manipulate these values via custom functions, you must change the hash if your function does it conditionally. For example, lets say we have a function which changes the price based on user ID. Different User IDs would need to see different prices, and thus must be cached separately. So you should add User ID to the hash:

https://gist.github.com/mikejolley/4cb504a486db70177e39

As long as you do this, prices will be cached correctly.

Invalidating caches

Caches will invalidate when the product transient version increases. To force this to occur use:

https://gist.github.com/mikejolley/00f416f393accd8b56a8

This will invalidate ALL product caches.

Filtering prices and avoiding cache completely

If you want to change prices without touching caches, you can use the woocommerce_variation_prices filter. This passes through the full array of prices before returning to the user. Just be aware, this will be ran on every call to get_variation_prices so don’t do any performance expensive processing.

When will this change go live?

This change will be pushed live during the week of Monday 21st September in WooCommerce 2.4.7. Please post any feedback on Github.


Keep yourself in the loop!

This field is hidden when viewing the form
This field is hidden when viewing the form
This field is hidden when viewing the form


One response to “Caching and dynamic pricing – upcoming changes to the get_variation_prices method”

  1. […] WooThemes just announced a change in the logic that WooCommerce uses to load the prices for variable products. The change, whose purpose is to fix possible issues related to the caching of incorrect product prices, can break compatibility with other plugin that rely on the existing logic. These changes are due to be added to of WooCommerce 2.4.7, which is due to be released on the 21st September 2015. […]

Leave a Reply

Your email address will not be published. Required fields are marked *