如何仅通过订单ID获取客户公开说明

时间:2018-05-17 12:49:22

标签: wordpress woocommerce

我向客户编写插件可以使用order id跟踪订单状态/详细信息。

我还想向客户显示公共订单备注

但我找不到任何功能或方法来做到这一点。

这是我的代码,但此代码显示所有注释包括私人和公共注释:

*/
function woohez_get_all_order_notes( $order_id ){
    $order_notes    =   array();
    $args = array (
            'post_id'   => $order_id,
            'orderby'   => 'comment_ID',
            'order'     => 'DESC',
            'approve'   => 'approve',
            'type'      => 'order_note'
    );
    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;
}


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

2 个答案:

答案 0 :(得分:0)

我解决了这个问题, 您应该添加meta keymeta value 就像这样:

function woohez_get_all_order_notes( $orderNumber ){
            $order_notes    =   array();
            $args = array (
                    'post_id'   => $orderNumber,
                    'orderby'   => 'comment_ID',
                    'order'     => 'DESC',
                    'approve'   => 'approve',
                    'type'      => 'order_note',
                    'meta_query' => array(
                        array(
                          'key' => 'is_customer_note',
                          'value' => 1,
                          'compare' => 'EXISTS',
                        )
                      )
            );
            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;
        }

答案 1 :(得分:0)

我认为下面的代码会获取私人笔记ID并将其从循环中排除,所以现在你只会得到非私有的笔记

*/
function woohez_get_all_order_notes( $order_id ){
    $private_order_notes = get_private_order_notes( $order_id );
    private_note_ids = array();
    foreach($private_order_notes as $private_note)
    {
           $private_note_ids[] = $private_note['note_id'];
    }
    $order_notes    =   array();
    $args = array (
            'post_id'   => $order_id,
            'orderby'   => 'comment_ID',
            'order'     => 'DESC',
            'approve'   => 'approve',
            'type'      => 'order_note',
            'post__not_in' => $private_note_ids
    );
    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;
}


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

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;
}