Reputation: 43
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?
Upvotes: 4
Views: 549
Reputation: 11841
There are 3 places where this filter is hooked. Three of them have two parameters.
Try this way if though
add_filter( 'woocommerce_get_price_html', 'alter_price', 10, 2 );
function alter_price( $price, $item ) {
echo var_dump ($item);
return $price;
}
Upvotes: 0
Reputation: 254378
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.
Upvotes: 1