2017-04-18 124 views
1

我可以添加一個訂單說明(私人注):從Woocommerce訂單/ WC_Order對象獲取訂單註釋?

$order->add_order_note($info_for_order); 

但是,當我試圖得到一些頁面的值:

get_comments(['post_id' => $order_id]) 
// or 
$order_object->get_customer_order_notes() 

它只是簡單地返回一個空數組。我GOOGLE了這一點,我找不到方法來做到這一點。

+0

嘗試'$命令 - > add_order_note($ info_for_order,1);' – Reigel

回答

2

訂單備註(私人注)使用get_comments()功能時,僅適用於後端。
如果你看看WC_Comments exclude_order_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_id = 6238

$order_id = 6238; 
$order_notes = get_private_order_notes($order_id); 
foreach($order_notes as $note){ 
    $note_id = $note['note_id']; 
    $note_date = $note['note_date']; 
    $note_author = $note['note_author']; 
    $note_content = $note['note_content']; 

    // Outputting each note content for the order 
    echo '<p>'.$note_content.'</p>'; 
} 
+2

不錯,我甚至不知道如何可以得到這些信息。 haha –

+1

@RaymondGoldmanSeger我是一個woocommerce數據庫間諜...這就是爲什麼。我認爲用一種新方法擴展WC_Order類也是可能的,但是更復雜並且必須在插件上完成。 – LoicTheAztec

1

沒有爲獲得訂單的註釋替代最好的辦法。

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