Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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 );
@greguly
Copy link

greguly commented Feb 6, 2014

Cool, how about product meta data?

@trewknowledge
Copy link

Does this work for the User export as well? or just the Orders export?

@philfrasty
Copy link

I'm on WC 2.0.20 and WP 3.8.1. It'll only insert the columns for me in the csv but not any contents. Using the correct meta keys. Any hints what I might be doing wrong?

@rockymountainhigh1943
Copy link

So I just did this successfully with WC 2.1.2 and WP 3.8.1, but I'm not sure there is much difference between the filters on WC 2.1.2 and WC 2.0.2.

The important thing is that the names in the array's are consistent -- so if you have 'column_1' or in my case 'attendee_names' as the name of the header -- then you must have the same column name in the other filter for 'attendee_names'.

Good luck!

@philfrasty
Copy link

@rocky
Thank you very much for your reply. For me the functions to create the row-headers as well as remove columns work fine, just inserting content does not work. What meta-key did you use in your example?

Thanks again & best regards

@philfrasty
Copy link

Just an example what I have: 'column_1' => get_post_meta( $order->id, 'billing_first_name', true ),
So billing_first_name would be the meta-key here.

@trewknowledge
Copy link

To answer my own question, to get to this to work on the customer csv export the correct filters are:

wc_customer_order_csv_export_customer_headers and wc_customer_order_csv_export_customer_row

@Kelowna
Copy link

Kelowna commented Mar 4, 2014

Trying to insert a custom field with a meta key that is Credit Card Type. Using:
'column_1' => get_post_meta( $order->id, 'Credit Card Type', true ),

All I am getting is a row full of the first letter of the value repeated in almost every column below the order row.

Any thoughts?

Had no problem in the previous version of this with the same key. Now its failing even tho i've updated the function.

@schelli
Copy link

schelli commented Mar 6, 2014

It is creating the column but not filling in the content?

@Kelowna
Copy link

Kelowna commented Mar 6, 2014

It is creating the column and putting the header in the column but then it will out put 'V' in that column and multiple columns thereafter wrapping and creating a new row of just V in each column for 'Visa' or 'M' for Mastercard. The credit card type is a meta field. I had no problem including it in the previous version of this plugin. It also shows up in the email without any problem after product purchase (both in the past and currently). I know it isn't ideal that the meta key is 'Credit Card Type' rather than 'credit_card_type' but that hasn't proved to be an issue in the past.

@Kelowna
Copy link

Kelowna commented Mar 14, 2014

Really need this to work for legacy exports. It isn't working for legacy exports. I tried keeping the legacy code in the functions.php and it no longer processes. The new code just messes up the whole CSV. Any ideas? Could it be an issue with the array if I only have one column I'm attempting to add?

@justincone
Copy link

Having the same issue as Kelowna. With Legacy export (one item per row), including meta is borking the CSV in exactly the way she describes: a single letter sprinkled in rows throughout the sheet.

I even tested with a string, rather than an actual call to a meta key:

'start_date' => 'test'

(My custom column is "start_date." The header shows up fine, btw.)

The fact that even a string is breaking the CSV creation leads me to believe there something wrong with class-wc-customer-order-csv-export-compatibility.php, not class-wc-customer-order-csv-export-generator.php.

But I'm a novice coder. Any help would be appreciated.

@smakman
Copy link

smakman commented Mar 31, 2014

The legacy export puts each ordered item on a new row, making $order_data an array of items. So for each item you will need to add the extra columns:

<?php
// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order ) {

    foreach ( $order_data as $key => $item ) {

        $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
        );

        $order_data[$key] = array_merge( $item, $custom_data );
    }

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

@ChromeOrange
Copy link

I've used the original code supplied and these settings http://cld.wthms.co/6wir and I get the correct output, shown here http://cld.wthms.co/cTh8

@traummmaschine
Copy link

Following on here, inspired by smakman's post - I too needed to add a custom field column when using the 'Legacy - One Row per Item' format.

This works for me:

 // set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order ) {

    foreach ( $order_data as $key => $item ) {

        $orderProduct = get_page_by_title( $item[line_item_name], 'OBJECT', 'product' );

        $custom_data = array(
            'column_1' => get_post_meta( $orderProduct->ID, 'YOUR_CUSTOM_FIELD', true )
        );

        $order_data[$key] = array_merge( $item, $custom_data );
    }

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

There doesn't appear to be a product ID stored in the $item array, hence using 'get_page_by_title' to grab it.

The only flaw here is if there are foreign or special characters in the product title. Retrieving product ID by other means (rather than 'get_page_by_title') would be more reliable.

@Kelowna
Copy link

Kelowna commented Apr 3, 2014

Thank you Smakman that worked!

@HewittBrown
Copy link

The concept above sound almost what I need. Would it not make more sense to call the product using the SKU?

What would the code be to get the meta data ( custom fields) using the SKU? Any help would be greatly appreciated as I am a bit dense when it comes to coding.

@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