在订单详细信息的订单项目表中显示产品元 [英] Show product meta in order items table in Order Details

查看:36
本文介绍了在订单详细信息的订单项目表中显示产品元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为订单项添加自定义列并在此列中显示特定的产品元数据.我的意思是像下面的图片,我找不到 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".

这篇关于在订单详细信息的订单项目表中显示产品元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆