I use the Customer/Order CSV Export plugin from WooThemes to export orders from my shop. However I want to add additional data to the CSV output per line item (do some calculations etc.) and I found below information on the official WooThemes page.
However I want to access the line item data, how can I achieve this? For example: How can I get the ‘item_total’ which is not stored in the postmeta database (the postmeta database is listed in the example below) ?
Is the line_item data included in the $order_data? How can I access it?
function wc_csv_export_modify_row_data( $order_data, $order, $csv_generator ) {
$custom_data = array(
'column_1' => get_post_meta( $order->id, 'some_meta_key', true ),
'column_2' => get_post_meta( $order->id, 'some_other_meta_key', true ),
// add other row data here in the format column_key => data
);
$new_order_data = array();
if ( isset( $csv_generator->order_format ) && ( 'default_one_row_per_item' == $csv_generator->order_format || 'legacy_one_row_per_item' == $csv_generator->order_format ) ) {
foreach ( $order_data as $data ) {
$new_order_data[] = array_merge( (array) $data, $custom_data );
}
} else {
$new_order_data = array_merge( $order_data, $custom_data );
}
return $new_order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 3 );
you can create an object of WC_Order class by passing order id to it. Using get_items() you will get all items array which are present in that order. After getting an array you may use get_item_meta() for getting each items meta values pass order_item_id to it.