wc_get_order_notes()
Get order notes.
No Hooks.
Returns
stdClass[]. Array of stdClass objects with order notes details.
Usage
wc_get_order_notes( $args );
- $args(array) (required)
- Query arguments {
php 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.
}.
Changelog
| Since 3.2.0 | Introduced. |
wc_get_order_notes() wc get order notes code WC 10.7.0
function wc_get_order_notes( $args ) {
$key_mapping = array(
'limit' => 'number',
'order_id' => 'post_id',
'order__in' => 'post__in',
'order__not_in' => 'post__not_in',
);
foreach ( $key_mapping as $query_key => $db_key ) {
if ( isset( $args[ $query_key ] ) ) {
$args[ $db_key ] = $args[ $query_key ];
unset( $args[ $query_key ] );
}
}
// Define orderby.
$orderby_mapping = array(
'date_created' => 'comment_date',
'date_created_gmt' => 'comment_date_gmt',
'id' => 'comment_ID',
);
$args['orderby'] = ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'date_created', 'date_created_gmt', 'id' ), true ) ? $orderby_mapping[ $args['orderby'] ] : 'comment_ID';
// Set WooCommerce order type.
if ( isset( $args['type'] ) && 'customer' === $args['type'] ) {
$args['meta_query'] = array( // WPCS: slow query ok.
array(
'key' => 'is_customer_note',
'value' => 1,
'compare' => '=',
),
);
} elseif ( isset( $args['type'] ) && 'internal' === $args['type'] ) {
$args['meta_query'] = array( // WPCS: slow query ok.
array(
'key' => 'is_customer_note',
'compare' => 'NOT EXISTS',
),
);
}
// Set correct comment type.
$args['type'] = 'order_note';
// Always approved.
$args['status'] = 'approve';
// Does not support 'count' or 'fields'.
unset( $args['count'], $args['fields'] );
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 );
return array_filter( array_map( 'wc_get_order_note', $notes ) );
}