Get Admin Order Notes
February 20, 2025 ยท AI Conversations
User
get attached woocommerce order notes add by admin woocommerces admin ,for all orders of a users
Assistant
To fetch all WooCommerce order notes added by the admin for all orders of a specific user, you can use the following query in your WordPress setup:
### Method 1: Using SQL Query
If you need to fetch this data directly from the database, you can run this SQL query:
SELECT c.comment_ID, c.comment_post_ID, c.comment_content, c.comment_date, c.comment_author
FROM wp_comments c
JOIN wp_posts p ON c.comment_post_ID = p.ID
WHERE p.post_type = 'shop_order'
AND p.post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-pending')
AND c.comment_type = 'order_note'
AND p.post_author = (SELECT ID FROM wp_users WHERE user_login = 'USER_LOGIN')
AND c.comment_author != 'WooCommerce';
Replace `'USER_LOGIN'` with the username of the customer whose order notes you want to fetch.
### Method 2: Using PHP Code (WooCommerce Function)
If you prefer using PHP, you can get the order notes with this function:
function get_admin_order_notes_by_user($user_id) {
$args = array(
'post_type' => 'shop_order',
'post_status' => array('wc-completed', 'wc-processing', 'wc-on-hold', 'wc-pending'),
'author' => $user_id,
'numberposts' => -1
);
$orders = get_posts($args);
$order_notes = [];
foreach ($orders as $order) {
$notes = wc_get_order_notes(['order_id' => $order->ID]);
foreach ($notes as $note) {
if ($note->added_by === 'admin') {
$order_notes[] = [
'order_id' => $order->ID,
'note' => $note->content,
'date' => $note->date_created->date('Y-m-d H:i:s'),
'added_by' => $note->added_by
];
}
}
}
return $order_notes;
}
// Usage Example
$user_id = 123; // Replace with actual user ID
$order_notes = get_admin_order_notes_by_user($user_id);
print_r($order_notes);
### Explanation:
1. It fetches all orders for a specific user.
2. Retrieves order notes attached to those orders.
3. Filters notes added by the admin.
Let me know if you need modifications! ๐