2017-04-18 5 views
1

私はオーダーノート(プライベートノート)を追加することができます。Woocommerce Order/WC_Orderオブジェクトから注文ノートを取得しますか?

$order->add_order_note($info_for_order); 

しかし、私はいくつかのページで値を取得しようとしたとき:

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

これは単に、空の配列を返します。私はこれをgoogledと私はそれを行う方法を見つけることができません。

+0

試し '$ order-> 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; 
} 
関連する問題