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

如何在WordPress(Woocommerce)订单表中展示产品分类名称

你需要在现有代码中order-cat判断的空分支中填入订单商品分类读取、去重、拼接的逻辑,修改后的对应代码段如下:

<?php elseif ( 'order-cat' === $column_id ) : ?>
    <?php
    $categories = [];
    // 遍历订单所有商品项
    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        // 获取商品所属的产品分类(默认WooCommerce分类法为product_cat,若为自定义分类自行替换参数)
        $terms = get_the_terms( $product_id, 'product_cat' );
        if ( $terms && ! is_wp_error( $terms ) ) {
            foreach ( $terms as $term ) {
                // 存入数组自动去重
                $categories[ $term->term_id ] = $term->name;
            }
        }
    }
    // 输出结果,无分类则显示占位符
    echo esc_html( $categories ? implode( '、', $categories ) : '-' );
    ?>

补充说明:

  • 如果你的产品分类是自定义创建的分类法,把代码里get_the_terms的第二个参数product_cat替换为你自己的分类法别名即可
  • 代码自动对重复分类做了去重处理,同一订单内多个同分类商品不会重复输出分类名称
  • 没有匹配到分类的订单会显示-作为占位,你可以自行替换为需要的提示文本

内容的提问来源于stack exchange,提问作者Павел Лукашевич

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 17:54:08