Skip to content
Advertisement

Change product prices via a hook in WooCommerce 3+

IN WooCommerce, I need to multiply all product prices by a number. So I have used the following (via a plugin):

JavaScript

But, that doesn’t work for variation products. I have tried the following hooks with no luck:

JavaScript

The only one that works half way is this one:

JavaScript

But that just changed the overall price, not the selected variation price. See the image below, price is BsF. 200 and the overall price is right, 200 x 2 = 400, but the variation price when selected still shows 200:

Note: I need it to actually change, so display html hooks wont work.

Variation Price

Is there anything I’m missing, or something wrong?

Advertisement

Answer

Update (December 2020)

  • 2 code versions for themes and plugins (works in Woocommerce 3.3.x too)
  • Cached variations prices in Woocommerce 3 (Update and addition):
    Now using woocommerce_get_variation_prices_hash filter hook much more efficient, instead of wc_delete_product_transients()… See this related thread
  • Added product price filter widget hooks (see at the end).

1) Plugin version with a constructor function:

The hooks that you are using are deprecated in WooCommerce 3+

To make it work for all products prices, including variations prices, you should use this:

JavaScript

The code tested and perfectly works (only) in WooCommerce 3+.


2) For theme version: functions.php file on active child theme (or active theme):

JavaScript

Tested and works on woocommerce 3+


For products in sale you have those hooks:

  • woocommerce_product_get_sale_price (Simple, grouped and external products)
  • woocommerce_variation_prices_sale_price (Variable products (min-max))
  • woocommerce_product_variation_get_sale_price (Products variations)

Cached prices and woocommerce 3:

The 3 filters hooks involved in variations cached prices are:

  • woocommerce_variation_prices_price
  • woocommerce_variation_prices_regular_price
  • woocommerce_variation_prices_sale_price

Introduced in 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 this hooks are executed.

So performances will stay boosted (Thanks to Matthew Clark that pointed this better way)

See: Caching and dynamic pricing – upcoming changes to the get_variation_prices method


For filtering product prices with a widget (min and max price), use the following hooks:

  • woocommerce_price_filter_widget_min_amount that has one argument $price
  • woocommerce_price_filter_widget_max_amount that has one argument $price
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement