结合使用WooCoomerce和WooCommerce Bookings插件。在他们的API Reference中,有一个列出的用于修改预订成本的过滤器:woocommerce_bookings_calculated_booking_cost。简而言之,下面是它在代码中的应用:
return apply_filters( 'woocommerce_bookings_calculated_booking_cost', $booking_cost, $product, $data );现在,我添加了以下代码,以尝试更改价格:
function foobar_price_changer( $booking_cost, $product, $data ) {
return $booking_cost;
}
add_filter( 'woocommerce_bookings_calculated_booking_cost', 'foobar_price_changer' );现在,当我使用该代码时,它会在我的日志中抛出一个错误:
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function sbnb_modify_wc_bookings_price(), 1 passed in /mywppath/wp-includes/class-wp-hook.php on line 290 and exactly 3 expected in /mywppath/wp-content/themes/enfold-child/functions.php:155据我所知,有3个参数被传递给add_filter回调函数,但在我的例子中,它只传递了一个。这里的问题可能是什么?
发布于 2020-09-08 21:36:54
如果在调用add_filter函数时没有指定$accepted_args或第四个参数,默认情况下它只向回调函数传递一个参数。因此,只要有多个参数要传递给回调函数,就必须指定预期参数的数量。来自wp-includes/plugin.php:
* @global array $wp_filter A multidimensional array of all hooks and the callbacks hooked to them.
*
* @param string $tag The name of the filter to hook the $function_to_add callback to.
* @param callable $function_to_add The callback to be run when the filter is applied.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action. Default 10.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
* @return true
*/
function add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
global $wp_filter;
if ( ! isset( $wp_filter[ $tag ] ) ) {
$wp_filter[ $tag ] = new WP_Hook();
}
$wp_filter[ $tag ]->add_filter( $tag, $function_to_add, $priority, $accepted_args );
return true;
}发布于 2020-02-19 21:45:52
这样试一试
function foobar_price_changer( $booking_cost, $product, $data ) {
return $booking_cost;
}
add_filter( 'woocommerce_bookings_calculated_booking_cost', 'foobar_price_changer', 10, 3 ); // Where $priority is 10, $args is 3.https://stackoverflow.com/questions/60301396
复制相似问题