2

I really wonder if there is a way to regenerate all the native transients for a given product or variation object.

I'm in a case where I display dynamic prices, and everything is fine. But I need to update transients like wc_prices_{id} in order for it to take into account this new custom price. If not, the first page load is horrible (especially on the shop page displaying several products at a time).

I'm hooking into woocommerce_product_variation_get_price, woocommerce_product_variation_get_regular_price, woocommerce_variation_prices_price and woocommerce_variation_prices_regular_price (in short, this modify on the fly EVERY SINGLE PRICE of a page, and if it's a parent containing children, it change prices for children too in order to calculate the range prices (min/max) of the parent).

When digging into Woocommerce code, I have found which method generate the prices transient : it's read_price_data() located in wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-variable-data-store-cpt.php. But I can't find a way to call it my Cron Job script to call it on a product or variation object.

Are you guys aware of solution for this task?

Kind regards.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

The 3 filters hooks involved in variations cached prices are:

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

To regenerate cached prices, you could use on those hooks:

wc_delete_product_transients( $variation->get_id() );

But it will slow down your web site performances.

Introduced since Woocommerce 3, woocommerce_get_variation_prices_hash filter hook will allow to refresh variations cached prices in a much more efficient way, without deleting related transients anytime that those hooks are triggered.

Documentation and related thread:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399