WC 预订 - 过滤 - 传递的参数很少
WC Bookings - filter - to few arguments passed
将 WooCoomerce 与 WooCommerce Bookings plugin. In their 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 回调,但在我的例子中它只传递了一个。这可能是什么问题?
试试这个方法
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.
如果调用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;
}
将 WooCoomerce 与 WooCommerce Bookings plugin. In their 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 回调,但在我的例子中它只传递了一个。这可能是什么问题?
试试这个方法
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.
如果调用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;
}