Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save woogist/8837975 to your computer and use it in GitHub Desktop.
Save woogist/8837975 to your computer and use it in GitHub Desktop.
WooCommerce Customer/Order CSV Export: Add additional columns
<?php
// add custom column headers
function wc_csv_export_modify_column_headers( $column_headers ) {
$new_headers = array(
'column_1' => 'Column 1',
'column_2' => 'Column 2',
// add other column headers here in the format column_key => Column Name
);
return array_merge( $column_headers, $new_headers );
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_modify_column_headers' );
// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order ) {
$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
);
return array_merge( $order_data, $custom_data );
}
add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 2 );
@eyarnell
Copy link

This code works great for additional radio fields but with a text field all I see is the column header but no column content. I have verified that the field name is correct. Any ideas?

@steinaan
Copy link

steinaan commented May 9, 2014

I am a novice, so can anyone tell in which file to insert the code. Thanks to anyone that can help me.

'Column 1', 'column_2' => 'Column 2', // add other column headers here in the format column_key => Column Name ); return array_merge( $column_headers, $new_headers ); } add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_modify_column_headers' ); // set the data for each for custom columns function wc_csv_export_modify_row_data( $order_data, $order ) { $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 ); return array_merge( $order_data, $custom_data ); } add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 2 );

@LucAwater
Copy link

I had the same problem as philfrasty. It created the columns, but didn't insert any content. Now i've got it working with the original code posted above and the 'order export format' setting, at 'CSV import'.

@davidmanson
Copy link

davidmanson commented May 11, 2016

For those who are looking for a way to add product meta in the CSV file using the 'Legacy - One Row per Item' format, I found a way that seems to work quite well :

function wc_csv_export_modify_row_data( $order_data, $order ) {

   $key = 0;

   foreach ( $order->get_items() as $item ) {
       $product = $order->get_product_from_item( $item );
       if ( ! is_object( $product ) ) {
           $product = new WC_Product( 0 );
       }

       $order_data[ $key ]['ean'] = get_post_meta( $product->id, '_cpf_ean', true );
       $order_data[ $key ]['mpn'] = get_post_meta( $product->id, '_cpf_mpn', true );

       $key++;
   }

   return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 2 );

@mikeeman2000
Copy link

I'm no php wiz, but I can understand that code. However, I having a hard time displaying user meta data of the customer. So instead of a custom field i'd like to display user meta of the user who made the purchase.

'column_1' => get_user_meta( $order->id, 'some_meta_key', true ),
'column_2' => get_user_meta( $order->id, 'some_other_meta_key', true ),

Doesn't work. Any insights?

Thanks

@Garconis
Copy link

Garconis commented Jan 6, 2017

How do you add custom meta keys from an ITEM that is part of the order? Example: I am using "Custom Format" where a row represents a single line item. Items within an order have _startdate set to them... but I'm having a hard time pulling in this custom meta key into the CSV Export.

davidmanson's example seems to pull in the item's CURRENT data... not the data that was captured during the order.

@Garconis
Copy link

@mikeeman2000, did you ever figure that out? I'm looking to do the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment