Get Order Notes from a Woocommerce Order / WC_Order object?
Asked Answered
L

5

7

I can add an order note (private note) with:

$order->add_order_note($info_for_order);

But when I tried to get the values in some page with:

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

It simply returns an empty array. I googled this and i can't find the method to do it.

Lcm answered 18/4, 2017 at 3:44 Comment(1)
try $order->add_order_note($info_for_order, 1);Schmaltz
L
19

Order notes (private note) are only available for backend when using get_comments() function.
If you look at WC_Comments exclude_order_comments() method you will see that front end queries are filtered regarding private order notes…

So the turn around is to build a custom function to get the private Order notes:

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


Usage (for example the $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>';
}
Lewan answered 18/4, 2017 at 5:15 Comment(2)
nice, i don't even know how you can get that info. hahaLcm
@RaymondGoldmanSeger I am a woocommerce database spy… that's why. I think it could be also possible to extend WC_Order class with a new method, but is more complicated and has to be done on a plugin.Lewan
A
13

There woo documentation https://docs.woocommerce.com/wc-apidocs/function-wc_get_order_notes.html

Example:

wc_get_order_notes([
   'order_id' => $order->get_id(),
   'type' => 'customer',
]);

Result

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
        )

)
Anchor answered 12/2, 2020 at 10:33 Comment(0)
S
4

There is alternative best way for getting order notes.

/**
 * 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;
}
Sporogenesis answered 15/8, 2017 at 6:44 Comment(0)
T
2

Answer for 2022

Not sure about these other answers, but according to woocommerce docs the best approach is:

$order->get_customer_note();
Trichomonad answered 20/5, 2022 at 20:36 Comment(1)
Mind you that this way you get the customer note from the checkout. I think OP is asking about in-admin order notes. The ones that you can set, send and read within the administration (when editing an order), or from a specialized dashboard, in case you are using Dokan or etc.Publish
U
0

By looking into how WooCommerce does it in their own woocommerce\includes\admin\meta-boxes\class-wc-meta-box-order-notes.php all you need is:

$notes = wc_get_order_notes( ['order_id' => $order_id] );

This is documented here wc_get_order_notes()

wc_get_order_notes(array<string|int, mixed> $args) : array<string|int, stdClass>

Parameters

Query arguments { Array of query parameters.

@type string $limit         Maximum number of notes to retrieve.
                            Default empty (no limit).
@type int    $order_id      Limit results to those affiliated with a given order ID.
                            Default 0.
@type array  $order__in     Array of order IDs to include affiliated notes for.
                            Default empty.
@type array  $order__not_in Array of order IDs to exclude affiliated notes for.
                            Default empty.
@type string $orderby       Define how should sort notes.
                            Accepts 'date_created', 'date_created_gmt' or 'id'.
                            Default: 'id'.
@type string $order         How to order retrieved notes.
                            Accepts 'ASC' or 'DESC'.
                            Default: 'DESC'.
@type string $type          Define what type of note should retrieve.
                            Accepts 'customer', 'internal' or empty for both.
                            Default empty.
}
Udale answered 15/1 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.