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

插件开发求助:如何仅通过Order ID获取订单公开备注

解决WooCommerce仅显示公开订单备注的问题

我来帮你搞定这个问题!你当前的代码会拉取所有订单备注(包括私有),是因为没有针对WooCommerce的公开备注做过滤——WooCommerce里的**公开订单备注(Public Order Notes)**是通过评论元数据is_customer_note来标记的,当这个字段的值为1(布尔值true)时,就是面向客户的公开备注;值为0则是商家内部的私有备注。

修改后的代码

我给你调整了函数,添加了元查询来筛选仅公开的备注:

function woohez_get_public_order_notes( $order_id ){
    $order_notes = array();
    
    $args = array(
        'post_id' => $order_id,
        'orderby' => 'comment_ID',
        'order' => 'DESC',
        'approve' => 'approve',
        'type' => 'order_note',
        // 添加这部分来筛选公开备注
        'meta_query' => array(
            array(
                'key' => 'is_customer_note',
                'value' => '1',
                'compare' => '='
            )
        )
    );
    
    remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
    $notes = get_comments( $args );
    
    if ( $notes ) {
        foreach ( $notes as $note ) {
            $order_notes[] = wpautop( wptexturize( wp_kses_post( $note->comment_content ) ) );
        }
    }
    
    return $order_notes;
}

// 使用示例:传入你的订单ID
$notes_array = woohez_get_public_order_notes( 209 );

if ( count( $notes_array ) != 0 ) {
    foreach ( $notes_array as $note ) {
        echo $note;
    }
} else {
    echo "No public notes found!";
}

关键修改说明

  • 在$args里新增了meta_query数组,专门匹配is_customer_note值为1的评论,这就是WooCommerce标记公开备注的核心逻辑。
  • 我把函数名改成了woohez_get_public_order_notes,让功能更直观,你也可以根据自己的习惯改回原名。
  • 输出提示调整为No public notes found!,让客户能更清楚当前的状态。

这样调整后,你的插件就只会向客户展示那些专门标记为公开的订单备注了,商家的私有备注会被自动过滤掉~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:21:33