Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvega/b1dcb5e878dd7fb8ebd06d524e3ef4ce to your computer and use it in GitHub Desktop.
Save wvega/b1dcb5e878dd7fb8ebd06d524e3ef4ce to your computer and use it in GitHub Desktop.
Add order note date to order_notes column in CSV exports generated with WooCommerce Customers Orders Coupons Export
<?php // only copy this line if needed
/**
* Adds the order note date to the notes in the order notes column of CSV exports.
*/
/**
* Adds the order note date to the notes in the order notes column of CSV exports.
*
* @param array $order_data order data
* @param \WC_Order $order order object
* @return array
*/
function sv_wc_csv_export_add_order_note_dates( $order_data, $order ) {
$args = [
'post_id' => $order->get_id(),
'approve' => 'approve',
'type' => 'order_note',
];
remove_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] );
$notes = get_comments( $args );
add_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] );
$order_notes = [];
foreach ( $notes as $note ) {
$date_created = wc_string_to_datetime( $note->comment_date );
if ( $date_created instanceof \WC_DateTime ) {
$note_date = sprintf( __( '%1$s at %2$s', 'woocommerce' ), $date_created->date_i18n( wc_date_format() ), $date_created->date_i18n( wc_time_format() ) );
$note_content = $note->comment_content . ' (' . $note_date . ')';
} else {
$note_content = $note->comment_content;
}
$order_notes[] = str_replace( [ "\r", "\n" ], ' ', $note_content );
}
$order_data['order_notes'] = implode( '|', $order_notes );
return $order_data;
}
add_filter( 'wc_customer_order_export_csv_order_row', 'sv_wc_csv_export_add_order_note_dates', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment