{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
get_customer_order_notes() only returns public order notes.
How can I get all order notes including private notes?
The robot generated the following code:
You can get all order notes including private notes by modifying the function `nm_get_customer_order_notes()`. You'd have to adjust the 'post_status' argument, setting it to 'any' which includes both public and private notes.
Here's an example of how that could look:
“`php
function nm_get_customer_order_notes($order_id) {
remove_filter('comments_clauses', array('WC_Comments', 'exclude_order_comments'), 10, 1);
$args = array(
'post_id' => $order_id,
'approve' => 'approve',
'type' => 'order_note',
'post_status' => 'any' //This line makes sure it includes both public and private notes
);
add_filter('comments_clauses', array('WC_Comments', 'exclude_order_comments'), 10, 1);
return get_comments($args);
}
“`
This function uses the get_comments() function to fetch all comments (order notes in this case) irrespective of their status. The argument array specifies that we want comments for the particular order_id and that we want both approved comments of type 'order_note', but now includes both public and private notes. Then it restores the original filter after fetching the comments.
You can utilize it by passing the Order ID to this function as shown below:
“`php
$order_id = 123; // replace with your order id
$notes = nm_get_customer_order_notes($order_id);
“`