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

WooCommerce后台订单列表获取购买产品名称的实现方案问询

WooCommerce后台订单列表新增产品名称列实现方案

你只需修改inforder_add_new_order_admin_list_column_content函数内的逻辑即可,修改后的完整可运行代码如下:

<?php
/**
 * @snippet       给WooCommerce后台订单列表新增产品列
 */
 
add_filter( 'manage_edit-shop_order_columns', 'inforder_add_new_order_admin_list_column' );
function inforder_add_new_order_admin_list_column( $columns ) {
    $columns['product_order'] = '购买产品';
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'inforder_add_new_order_admin_list_column_content' );
function inforder_add_new_order_admin_list_column_content( $column ) {
    global $post;
    if ( 'product_order' === $column ) {
        $order = wc_get_order( $post->ID );
        $product_list = [];
        // 遍历订单内所有产品类订单项,自动过滤运费、优惠券等非产品项
        foreach ( $order->get_items() as $item ) {
            $product = $item->get_product();
            if ( $product ) {
                // 基础格式:产品名 x 购买数量
                $product_list[] = $product->get_name() . ' × ' . $item->get_quantity();
                // 可选功能:点击产品名跳转至产品编辑页,按需取消下方注释即可
                // $product_list[] = '<a href="' . get_edit_post_link( $product->get_id() ) . '" target="_blank">' . $product->get_name() . ' × ' . $item->get_quantity() . '</a>';
            }
        }
        // 多个产品用换行分隔输出,可替换为顿号、逗号等其他分隔符
        echo implode( '<br>', $product_list );
    }
}

注意事项

  • 兼容单个订单包含多件产品的场景,不会出现只显示单款产品的问题
  • 代码兼容WooCommerce 3.0及以上所有版本,无废弃方法调用
  • 可根据运营需求调整显示内容,比如追加产品SKU、产品规格等信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 22:54:00