The order notes are saved as post comments, so you can use the WordPress function get_comments()
to get the last note:
$args = array(
'post_id' => $order_id,
'orderby' => 'comment_ID',
'order' => 'DESC',
'approve' => 'approve',
'type' => 'order_note',
'number' => 1
);
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
$notes = get_comments( $args );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
By default Woocommerce excludes order notes when calling the get_comments function. To prevent this, you have to remove the filter, call get_comments()
and add the filter again. I copied this code from the function WC_Meta_Box_Order_Notes::output()
in wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-notes.php and added “number”, so you just get the last note.