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

如何在WooCommerce处理中订单商品汇总后台页面添加SKU及品牌属性显示

如何在WooCommerce处理中订单商品汇总后台页面添加SKU及品牌属性显示

嗨,我来帮你搞定这个需求!要在这个后台汇总页面里显示商品SKU和品牌(自定义属性),我们需要在原代码基础上,关联获取商品的核心信息——毕竟原代码只拿到了商品名称,没对接商品本身的属性数据。下面是具体的修改方案和完整代码:

核心修改思路

  • 从订单行项中获取产品ID:每个订单行项都关联着对应的产品ID,我们需要从woocommerce_order_itemmeta表中取出_product_id的值;
  • 获取产品SKU和品牌属性:拿到产品ID后,用WooCommerce自带的wc_get_product()函数(更安全规范)获取产品对象,从中提取SKU和自定义品牌属性;
  • 调整表格结构:在表头和输出部分新增SKU、品牌列,把获取到的信息展示出来;
  • 优化去重逻辑:原代码按商品名称去重容易出错,改成按产品ID去重,避免同名称不同产品被错误合并。

修改后的完整代码

function register_pending_order_summary_page() {
    add_submenu_page( 
        'woocommerce', 
        'Pending Order Summary', 
        'Pending Order Summary', 
        'read', 
        'pending_order_summary_page', 
        'pending_order_summary_page_callback' 
    );
}

function pending_order_summary_page_callback() {  ?>
<div class="wrap">
    <h1>Pending Order Summary</h1>
    <p id="pendingordersubtitle">This is a list of all the products needed to complete all 'Processing' orders.<br /><hr /></p>
    <?php
    // 存储商品汇总数据:键为产品ID,值包含名称、SKU、品牌、总数量
    $product_summary = array();

    global $wpdb;

    // 获取所有处理中的订单ID
    $order_ids = $wpdb->get_results( 
        $wpdb->prepare(
            "SELECT ID FROM {$wpdb->prefix}posts where post_type = %s and post_status = %s;", 
            'shop_order', 
            'wc-processing' 
        )
    );

    // 遍历每个订单
    foreach($order_ids as $an_order_id) {
        // 获取当前订单的所有商品行项
        $order_line_items = $wpdb->get_results( 
            $wpdb->prepare(
                "SELECT order_item_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items where order_id = %d and order_item_type = %s;", 
                $an_order_id->ID, 
                'line_item'
            )
        );

        // 遍历每个行项
        foreach($order_line_items as $line_item) {
            // 获取当前行项对应的产品ID
            $product_id = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta where meta_key = %s and order_item_id=%d", 
                    "_product_id", 
                    $line_item->order_item_id
                )
            );

            if(!$product_id) continue; // 跳过无产品ID的行项

            // 获取当前行项的购买数量
            $item_quantity = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta where meta_key = %s and order_item_id=%d", 
                    "_qty", 
                    $line_item->order_item_id
                )
            );
            $item_quantity = (int)$item_quantity;

            // 如果该产品还没在汇总数组里,先获取它的SKU和品牌
            if(!isset($product_summary[$product_id])) {
                // 使用WooCommerce函数获取产品对象(兼容版本更新,更稳定)
                $product = wc_get_product($product_id);
                if(!$product) continue;

                // 获取SKU,无SKU时显示默认文本
                $sku = $product->get_sku() ? $product->get_sku() : '无SKU';
                
                // 获取品牌(假设品牌是自定义属性,属性slug为'brand',请根据实际情况修改)
                $brand = $product->get_attribute('brand') ? $product->get_attribute('brand') : '无品牌';

                // 初始化汇总数据
                $product_summary[$product_id] = array(
                    'name' => $line_item->order_item_name,
                    'sku' => $sku,
                    'brand' => $brand,
                    'total_qty' => $item_quantity
                );
            } else {
                // 已存在的产品,累加数量
                $product_summary[$product_id]['total_qty'] += $item_quantity;
            }
        }
    }

    // 按总数量降序排序
    usort($product_summary, function($a, $b) {
        return $b['total_qty'] - $a['total_qty'];
    });

    // 输出表格
    echo '<table><tr><td style="width: 100px;"><strong>Quantity</strong></td><td><strong>Product</strong></td><td><strong>SKU</strong></td><td><strong>Brand</strong></td></tr>';
    if(!empty($product_summary)){
        foreach ($product_summary as $product_data) {
            echo '<tr>
                <td>' . $product_data['total_qty'] . '</td>
                <td>' . $product_data['name'] . '</td>
                <td>' . $product_data['sku'] . '</td>
                <td>' . $product_data['brand'] . '</td>
            </tr>';
        }
    } else {
        echo '<tr><td colspan="4">暂无处理中的订单商品</td></tr>';
    }
    echo '</table>';

    // 清理数据库查询缓存
    $wpdb->flush();
    ?>
    <br /><a href="javascript:window.print()"><button type="button" class="orderprintbutton">Print</button></a>
</div>
<?php 
}

add_action('admin_menu', 'register_pending_order_summary_page',99);

// 打印样式调整
add_action('admin_head', 'dashboard_print_view');
function dashboard_print_view() {
    echo '<style>
    @media print {
        #adminmenu{display: none!important;}
        div#adminmenuback {display: none!important;}
        #wpcontent {margin-left: 5%!important;}
        #wpfooter {display: none!important;}
        .orderprintbutton {display: none!important;}
        #screen-meta {display: none!important;}
    }
    </style>';
}

重要提醒

如果你的品牌自定义属性slug不是brand(比如用了product_brand),一定要把代码里get_attribute('brand')中的brand改成你实际使用的属性slug,否则会显示“无品牌”。

备注:内容来源于stack exchange,提问作者Alexandru T.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 11:08:02