Created
May 2, 2020 01:15
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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