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

如何修改WooCommerce自定义列代码以适配HPOS高性能订单存储

适配WooCommerce HPOS的后台自定义列代码修改方案

启用High-Performance Order Storage (HPOS)后,WooCommerce订单不再以WordPress自定义帖子类型存储,原有订单列表钩子失效。以下是针对需求的代码修改:

1. 订单页面添加「订单备注」列

替换原有的列注册和内容填充钩子,使用HPOS兼容的钩子:

// 注册订单备注列
add_filter( 'woocommerce_admin_order_list_columns', 'woocommerce_add_order_notes_column' );
function woocommerce_add_order_notes_column( $columns ) {
    // 在第8个位置插入列
    return array_slice( $columns, 0, 8, true )
        + array( 'order_notes' => __( 'Order Notes', 'woocommerce' ) )
        + array_slice( $columns, 8, null, true );
}

// 填充订单备注列内容
add_action( 'woocommerce_admin_order_list_column_order_notes', 'woocommerce_show_order_notes_column', 10, 1 );
function woocommerce_show_order_notes_column( $order ) {
    $note = $order->get_customer_note();
    if ( !empty($note) ) {
        echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
    } else {
        echo '<span class="na">&ndash;</span>';
    }
}

2. 订单页面添加「预售产品」列

替换钩子并移除对全局$the_order的依赖,直接通过参数获取订单对象:

// 注册预售产品列
add_filter( 'woocommerce_admin_order_list_columns', 'woocommerce_add_preorder_products_tick_column' );
function woocommerce_add_preorder_products_tick_column( $columns ) {
    $ordered_columns = array();
    
    foreach( $columns as $key => $column ){
        $ordered_columns[$key] = $column;
        if( 'wc_actions' == $key ){
            $ordered_columns['preorder_products_tick'] = __( 'Pre-orders', 'woocommerce');
        }
    }

    return $ordered_columns;
}

// 填充预售产品列内容
add_action( 'woocommerce_admin_order_list_column_preorder_products_tick', 'add_items_to_preorder_products_tick_column', 10, 1 );
function add_items_to_preorder_products_tick_column( $order ) {
    $order_items = $order->get_items();

    if ( !is_wp_error( $order_items ) ) {
        foreach( $order_items as $order_item ) {
            $product_id = $order_item->get_product_id();  
            if ( has_term( 
                array( 'comic-book-pre-orders', 'dc-comics-pre-orders', 'image-comics-pre-orders', 'marvel-comics-pre-orders', 'other-publisher-pre-orders' ), 
                'product_cat', 
                $product_id 
            ) ) {
                echo "✔";
            }
        }   
    }
}

3. 产品列表「总销量」列

这部分代码不受HPOS影响(产品仍以WordPress帖子类型存储),优化后代码如下:

// 注册总销量列
add_filter( 'manage_edit-product_columns', 'column_total_sales_1', 20 );
// 填充总销量列内容
add_action( 'manage_posts_custom_column', 'column_total_sales_2' );
// 设置列可排序
add_filter('manage_edit-product_sortable_columns', 'column_total_sales_3');
// 处理排序逻辑
add_action( 'pre_get_posts', 'column_total_sales_4' );

function column_total_sales_1( $columns_array ) {
    return array_slice( $columns_array, 0, 8, true )
        + array( 'total_sales' => 'Total Sales' )
        + array_slice( $columns_array, 8, null, true );
}

function column_total_sales_2( $column_id ) {
    if( $column_id  == 'total_sales' ){
        $product = wc_get_product( get_the_ID() );
        echo $product ? $product->get_total_sales() : 0;
    }
}

function column_total_sales_3( $a ){
    return wp_parse_args( array( 'total_sales' => 'by_total_sales' ), $a );
}

function column_total_sales_4( $query ) {
    if( !is_admin() || empty( $_GET['orderby']) || empty( $_GET['order'] ) )
        return;
    if( $_GET['orderby'] == 'by_total_sales' ) {
        $query->set('meta_key', 'total_sales' );
        $query->set('orderby', 'meta_value_num');
        $query->set('order', $_GET['order'] );
    }
    return $query;
}

修改说明

  • 订单列表部分:将原钩子manage_woocommerce_page_wc-orders_columns替换为HPOS专用的woocommerce_admin_order_list_columns;列内容钩子从manage_woocommerce_page_wc-orders_custom_column替换为woocommerce_admin_order_list_column_{column_key},直接传入$order对象而非依赖ID或全局变量。
  • 产品列表部分:原代码兼容HPOS,优化了销量获取方式,使用WooCommerce官方方法get_total_sales()更规范。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 04:30:57