Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Order Notes from a Woocommerce Order / WC_Order object?

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.

like image 244
Raymond Seger Avatar asked Apr 18 '17 03:04

Raymond Seger


People also ask

How would you obtain a customer's order details from WooCommerce?

One way is to simply go to the “Customers” page in your WordPress admin panel. From there, you can click on any customer to view their complete details. Another way to get customer details is through the WooCommerce REST API. This allows you to programmatically get information about customers, orders, etc.

Where are WooCommerce notes stored?

WooCommerce notes are stored in the database. When a customer places an order, a note is generated and stored in the database. The customer's name, email address, and order ID are also stored in the note. Notes can be viewed and edited from the WooCommerce > Orders > Edit Order > Notes section.

How do I order item details in WooCommerce?

$order->get_id(); // Returns the unique ID for this object. $order->get_meta(); // Get Meta Data by Key. $order->get_meta_cache_key(); // Helper method to compute meta cache key. Different from WP Meta cache key in that meta data cached using this key also contains meta_id column.


2 Answers

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>';
}
like image 122
LoicTheAztec Avatar answered Oct 07 '22 20:10

LoicTheAztec


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
        )

)
like image 34
yaroslawww Avatar answered Oct 07 '22 20:10

yaroslawww