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:

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:

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.