Woocommerce挂钩函数的第二个参数返回NULL而不是Object
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?
在Woocommerce文档中,位于 但是当我尝试访问该产品时,我得到一个 这是 我的测试代码: p>
我错过了什么吗? p>
div> get_price_html()中的
woocommerce_get_price_html code>过滤器钩子 code>方法应该接受最多接受两个参数 strong>,价格和产品的回调。 p>
NULL code>而不是WC_Product对象。 p>
add_filter('woocommerce_get_price_html',函数($ price,$ item){
echo var_dump($ item); // NULL
返回$ price ;
});
code> pre>
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.