In the Woocommerce documentation, the woocommerce_get_price_html
filter hook located in get_price_html()
method is supposed to take a callback that accepts up to two parameters, a price and a product.
But when I try to access the product, I get a NULL
instead of the WC_Product Object.
Here is my testing code:
add_filter( 'woocommerce_get_price_html', function( $price, $item ) {
echo var_dump ($item); // NULL
return $price;
});
Am I missing something?
You need to declare the 2 parameters that you are using for this hook, in your hooked function, just after the hook priority, this way:
add_filter( 'woocommerce_get_price_html', function( $price, $product ) {
echo var_dump ($product); // The WC_Product object instance
return $price;
}, 10, 2 );
And it's better to name your function, like:
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 );
function filter_woocommerce_get_price_html( $price, $product ) {
echo var_dump ($product); // The WC_Product object instance
return $price;
}
This time you should be able to get the variable $product
object…
See documentation for add_action() and add_filter() WordPress functions.