Php 从商业订单/WC\U订单对象获取订单注释?

Php 从商业订单/WC\U订单对象获取订单注释?,php,sql,wordpress,woocommerce,orders,Php,Sql,Wordpress,Woocommerce,Orders,我可以添加带有以下内容的订单通知(私人通知): 但当我尝试在某个页面中获取值时: get_comments(['post_id' => $order_id]) // or $order_object->get_customer_order_notes() 它只返回一个空数组。我在谷歌上搜索了这个,但我找不到做这个的方法 订单注释(私人注释)仅在使用get_comments()函数时可用于后端若您查看该方法,您将看到前端查询是针对私人订单注释进行过滤的 因此,我们要做的是构建一个自定

我可以添加带有以下内容的订单通知(私人通知):

但当我尝试在某个页面中获取值时:

get_comments(['post_id' => $order_id])
// or
$order_object->get_customer_order_notes()
它只返回一个空数组。我在谷歌上搜索了这个,但我找不到做这个的方法

订单注释(私人注释)仅在使用
get_comments()
函数时可用于后端
若您查看该方法,您将看到前端查询是针对私人订单注释进行过滤的

因此,我们要做的是构建一个自定义函数来获取私人订单注释:

function get_private_order_notes( $order_id){
    global $wpdb;

    $table_perfixed = $wpdb->prefix . 'comments';
    $results = $wpdb->get_results("
        SELECT *
        FROM $table_perfixed
        WHERE  `comment_post_ID` = $order_id
        AND  `comment_type` LIKE  'order_note'
    ");

    foreach($results as $note){
        $order_note[]  = array(
            'note_id'      => $note->comment_ID,
            'note_date'    => $note->comment_date,
            'note_author'  => $note->comment_author,
            'note_content' => $note->comment_content,
        );
    }
    return $order_note;
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

此代码经过测试并正常工作


用法(例如
$order\u id=6238
):

$order\u id=6238;
$order\u notes=get\u private\u order\u notes($order\u id);
foreach($order\u票据为$note){
$note_id=$note['note_id'];
$note_date=$note['note_date'];
$note_author=$note['note_author'];
$note_content=$note['note_content'];
//输出订单的每个注释内容
回显“”.$note_content.“

”; }
获取订单备忘还有其他最佳方法

/**
 * Get all approved WooCommerce order notes.
 *
 * @param  int|string $order_id The order ID.
 * @return array      $notes    The order notes, or an empty array if none.
 */
function custom_get_order_notes( $order_id ) {
    remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
    $comments = get_comments( array(
        'post_id' => $order_id,
        'orderby' => 'comment_ID',
        'order'   => 'DESC',
        'approve' => 'approve',
        'type'    => 'order_note',
    ) );
    $notes = wp_list_pluck( $comments, 'comment_content' );
    add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
    return $notes;
}
有很多文档

例如:

wc\u获取订单\u注释([
'order\u id'=>$order->get\u id(),
'类型'=>'客户',
]);
结果

Array
(
    [0] => stdClass Object
        (
            [id] => 11
            [date_created] => WC_DateTime Object
                (
                    [utc_offset:protected] => 3600
                    [date] => 2019-03-21 11:38:51.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [content] => Hi, blah blah blah
            [customer_note] => 1
            [added_by] => admin
        )

)

尝试
$order->add\u order\u note($info\u for\u order,1)很好,我甚至不知道你怎么能得到这些信息。haha@RaymondGoldmanSeger我是一个商业数据库间谍…这就是为什么。我认为也可以用新方法扩展WC_Order类,但更复杂,必须在插件上完成。
/**
 * Get all approved WooCommerce order notes.
 *
 * @param  int|string $order_id The order ID.
 * @return array      $notes    The order notes, or an empty array if none.
 */
function custom_get_order_notes( $order_id ) {
    remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
    $comments = get_comments( array(
        'post_id' => $order_id,
        'orderby' => 'comment_ID',
        'order'   => 'DESC',
        'approve' => 'approve',
        'type'    => 'order_note',
    ) );
    $notes = wp_list_pluck( $comments, 'comment_content' );
    add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
    return $notes;
}
Array
(
    [0] => stdClass Object
        (
            [id] => 11
            [date_created] => WC_DateTime Object
                (
                    [utc_offset:protected] => 3600
                    [date] => 2019-03-21 11:38:51.000000
                    [timezone_type] => 1
                    [timezone] => +00:00
                )

            [content] => Hi, blah blah blah
            [customer_note] => 1
            [added_by] => admin
        )

)