You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce教程:如何在后台订单页面添加产品列表列

在WooCommerce后台订单页面添加产品列表列的解决方案

嘿,我来帮你搞定这个需求!要在WooCommerce后台订单列表里新增一个显示订单产品的列,我们可以通过几个WordPress钩子来实现,下面是具体步骤和完整代码:

步骤1:给订单列表添加新列的表头

首先需要注册一个新列,用manage_edit-shop_order_columns钩子来设置列的名称和位置。

步骤2:填充新列的产品内容

接着用manage_shop_order_posts_custom_column钩子,获取每个订单里的产品数据,循环输出产品名称、数量,还能加上产品编辑页的链接方便快速查看。

步骤3:调整列宽优化显示(可选)

默认列宽可能不够用,我们可以加一点自定义CSS来调整,让产品列表显示更舒服。

完整代码示例

把下面的代码添加到你的主题functions.php文件里,或者自定义插件中(记得先备份原文件哦!):

// 给订单列表添加新列
add_filter('manage_edit-shop_order_columns', 'add_order_products_column');
function add_order_products_column($columns) {
    // 自定义新列的位置,这里选择在"订单总额"列后插入
    $new_columns = [];
    foreach ($columns as $key => $value) {
        $new_columns[$key] = $value;
        if ($key === 'order_total') {
            $new_columns['order_products'] = '订单产品'; // 可以修改这个列名
        }
    }
    return $new_columns;
}

// 填充新列的产品内容
add_action('manage_shop_order_posts_custom_column', 'populate_order_products_column', 10, 2);
function populate_order_products_column($column, $post_id) {
    if ($column === 'order_products') {
        $order = wc_get_order($post_id);
        if (!$order) return;

        $products_html = '';
        foreach ($order->get_items() as $item) {
            $product = $item->get_product();
            $product_name = $product ? $product->get_name() : $item->get_name();
            $quantity = $item->get_quantity();
            
            // 给产品名称添加编辑页链接
            $product_link = $product ? get_edit_post_link($product->get_id()) : '';
            if ($product_link) {
                $products_html .= '<a href="' . esc_url($product_link) . '" target="_blank">' . esc_html($product_name) . '</a>';
            } else {
                $products_html .= esc_html($product_name);
            }
            // 显示购买数量
            $products_html .= ' ×' . esc_html($quantity) . '<br>';
        }

        echo $products_html ? $products_html : '<em>无产品</em>';
    }
}

// 调整新列的宽度(可选)
add_action('admin_head', 'style_order_products_column');
function style_order_products_column() {
    echo '<style>
        .wp-list-table .column-order_products {
            width: 25%;
            min-width: 200px;
        }
    </style>';
}

代码说明:

  • add_order_products_column:可以修改'订单产品'来更改列名,也可以调整if ($key === 'order_total')里的参考列,改变新列在表头的位置。
  • populate_order_products_column:自动获取订单里的所有产品,输出名称(带编辑链接)和购买数量,无产品时显示提示文字。
  • style_order_products_column:自定义列的宽度,你可以根据后台页面的布局需求调整width和min-width的值。

设置完成后,刷新WooCommerce后台的订单页面,就能看到新增的产品列表列啦,效果和你预期的一致!

内容的提问来源于stack exchange,提问作者user3013821

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:17:25