我正在尝试向WooCommerce订单中的每个退款行添加一个按钮(其功能超出了这个问题的范围,足以说明它需要退款id作为参数)。我发现这些行是在woocommerce\\includes\\admin\\meta Box\\views\\html订单退款中创建的。无法重写的php。然而,有一项行动:
do_action( \'woocommerce_admin_order_item_values\', null, $refund, $refund->get_id() );
这似乎非常适合我的目的,所以我尝试将以下代码添加到函数中。php测试它是否可以工作:add_filter(\'woocommerce_admin_order_item_values\', \'test_refund_id\');
function test_refund_id($nullvar, $refund, $refund_id) {
if ( !empty($refund->refunded_by) ){
echo \'<td>Refund ID: \'.$refund_id.\'</td>\';
}
}
我用过if ( !empty($refund->refunded_by) )
因为woocommerce_admin_order_item_values
在html订单发货中也会被调用。php,html订单项。php和html订购费。php,第二个参数是“order item”对象(而不是“return”对象),因此没有refunded_by
属性和我的指示应该被忽略。但令我失望的是,我发现了以下错误:
致命错误:Uncaught ArgumentCounter错误:参数太少,无法执行函数test\\u return\\u id(),传入了1个\\wp includes\\class wp hook。php位于第290行,在\\wp content\\themes\\mytheme\\functions中正好需要3个。菲律宾比索:1087
我知道,事实上,我在上面引用的行中传递了三个参数(即null、退款对象和退款id),但我检查了290行附近错误引用的脚本(\\wp includes\\class wp hook.php),我在apply_filters()
功能:
// Avoid the array_slice if possible.
if ( $the_[\'accepted_args\'] == 0 ) {
$value = call_user_func( $the_[\'function\'] );
} elseif ( $the_[\'accepted_args\'] >= $num_args ) {
$value = call_user_func_array( $the_[\'function\'], $args );
} else {
$value = call_user_func_array( $the_[\'function\'], array_slice( $args, 0, (int) $the_[\'accepted_args\'] ) );
}
出于测试目的,我尝试临时更改最后一个“else”子句,用原始的参数数组替换“切片”参数数组,从而强制脚本保留所有参数,因此array_slice( $args, 0, (int) $the_[\'accepted_args\'] )
变得简单$args
. 惊喜,惊喜,现在一切顺利!现在,我很清楚这当然不是一个解决方案,因为我无法编辑核心WP脚本。我只想知道为什么apply_filters()
认为接受的参数小于传递的参数,因此它将参数数组裁剪为仅第一个参数。提前非常感谢任何想了解这件事的人。