在WooCommerce后端管理订单表屏幕中,我创建了一个新列来显示信息。我要显示的数据是订单中每个项目的元数据。
在订单处理过程中,会捕获产品的其他元数据,并将其保存在订单中(通过转到WoomCommerce>订单>编辑特定订单ID)。
这是我想要获取并返回的数据:
https://d.pr/free/i/HZzEam
当我检查那个区域时,它是
display_meta
要复制并添加到列中的表内容:
https://d.pr/free/i/oGsngJ
我看到了,在搜索WooCommerce文件时
这是正在创建该表的区域
:
$hidden_order_itemmeta = apply_filters(
'woocommerce_hidden_order_itemmeta', array(
'_qty',
'_tax_class',
'_product_id',
'_variation_id',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
'method_id',
'cost',
)
);
?><div class="view">
<?php if ( $meta_data = $item->get_formatted_meta_data( '' ) ) : ?>
<table cellspacing="0" class="display_meta">
<?php
foreach ( $meta_data as $meta_id => $meta ) :
if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) {
continue;
}
?>
<tr>
<th><?php echo wp_kses_post( $meta->display_key ); ?>:</th>
<td><?php echo wp_kses_post( force_balance_tags( $meta->display_value ) ); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
我意识到要添加到单个列中需要很多信息,但这正是我目前需要做的。
与WooCommerce在订单编辑区显示该信息的方式类似,我如何在我的列中显示该订单项元?
我正在使用一些PHP代码片段(hook)来创建新的列,方法是使用来自admin columns pro插件的可用操作。实际上,它允许我添加一个现有自定义字段的列,然后用我想要的任何数据覆盖它。这是我的当前代码:
<?php
/**
* Display custom Event Registration details (item meta data from the order) (which are captured via WooCommerce Add-ons form/fields) on the ORDER LIST/TABLE SCREEN via overriding Admin Columns Pro field column
*
* Filter the display value for a column
*
* @param mixed $value Custom field value
* @param int $id Object ID
* @param AC_Column $column Column instance
*/
function fs_custom_event_registration_details_column_value( $value, $id, $column ) {
if ( $column instanceof AC_Column_CustomField ) {
$order = wc_get_order( $id );
//$order_items = $order->get_items();
// get the meta key of this column
$meta_key = $column->get_meta_key();
// we've added the WooCommerceEventsTicketsPurchased as a custom column via Admin Columns plugin, and now are targetting that one to override it with different data
if ( 'WooCommerceEventsTicketsPurchased' == $meta_key ) {
$value = 'testing<br>';
// example of replacing column field value with a value from a different field
// $billingphone = get_post_meta( $id, '_billing_phone', true );
// $value .= sprintf( '<span>'. $billingphone .'</span><br>', $value );
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
//$product_id = $item->get_product_id();
//$product_variation_id = $item->get_variation_id();
$product_id = $item['product_id'];
$value .= $product_name;
}
}
}
// show me the results
return $value;
}
add_filter( 'ac/column/value', 'fs_custom_event_registration_details_column_value', 10, 3 );
但很明显,这只是抢占了订单中产品的名称。我需要获取订单中每个项目的订单元数据。
不一定要放在桌子上。项目符号列表,甚至逗号分隔都可以。
另外,如果可能的话,我只想输出事件类别中产品的item meta。