在订单明细中的订单项目表中显示产品元
问题描述:
我需要为订单商品添加自定义列,并在此列中显示特定的产品元数据. 我的意思是下面的图片, 我找不到woocommerce采取的任何操作来添加此列!
I need to add custom column for order items and show specific product meta in this column. I mean something like image below, I can't find any action from woocommerce to add this column!
答
您可以使用以下代码:
// Add custom column headers here
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
// set the column name
$column_name = 'Test Column';
// display the column name
echo '<th>' . $column_name . '</th>';
}
// Add custom column values here
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
// get the post meta value from the associated product
$value = get_post_meta($_product->post->ID, '_custom_field_name', 1);
// display the value
echo '<td>' . $value . '</td>';
}
我已经对此进行了评论,因此应该足够清楚,但是简而言之,这段代码添加了一个名为"Test Column"的自定义列,该列从产品的自定义字段中提取了值,即"_custom_field_name"
I've commented it so it should be clear enough, but in a nutshell this code adds a custom column, named "Test Column", and this column pulls the value from the custom field of the product, called "_custom_field_name".