Replacing a deprecated filter 'woocommerce_get_price' with 'woocommerce_product_get_price'
-
14-03-2021 - |
Question
I recently updated WooCommerce on a site that I didn't make myself. After doing this, then I saw the following line in the error-log:
woocommerce_get_price is deprecated since version 3.0.0!
Use woocommerce_product_get_price instead.
But when I google woocommerce_get_price
, then I can find no documentation on it, besides for this hookr-page and this page, where it just says that it's deprecated. But I can find no information about input and output for woocommerce_get_price
and woocommerce_product_get_price
. It feels like that it's simply a renaming, - but how do I figure out, if I can simply replace the name of that filter, without having to test everything page and function on the website?
It's a function in a plugin that has been custom-made for this suppliers sites. And the function looks like this:
public function woocommerce_get_price($price, $product) {
$price = $this->get_user_discounted_price($price, $product);
return $price;
}
Solution
It's hard to find because the hook name is generated dynamically.
All the get_
functions in the WooCommerce product class, like $product->get_price()
, use the get_prop()
method under the hood. This is part of the WC_Data
class that WC_Product
extends. At the end of get_prop()
the filters are applied like this:
apply_filters( $this->get_hook_prefix() . $key, $value, $this );
And get_hook_prefix()
looks like this:
protected function get_hook_prefix() {
return 'woocommerce_' . $this->object_type . '_get_';
}
So all the get_
methods like get_price()
, get_sku()
, get_description()
etc. will go through a filter with the same format: woocommerce_product_get_{property}
. In your case woocommerce_product_get_price
.
For the new filter, the 2 arguments are the value, $value
, and the product, $this
. So it doesn't look like you need to change the callback. It should be sufficient to change the hook name.