Skip to content

Instantly share code, notes, and snippets.

@tamarazuk
Last active August 31, 2018 04:42
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tamarazuk/46e66064a40c4a611397 to your computer and use it in GitHub Desktop.
Save tamarazuk/46e66064a40c4a611397 to your computer and use it in GitHub Desktop.
Customer/Order CSV Export: Separate item meta into multiple columns in the Default – One Row per Item format
<?php // only copy this line if needed
/**
* The use of this snippet requires at least WooCommerce 3.2
*/
/**
* Alter the column headers for the orders CSV to split item_meta into separate columns
*
* Note that this change is only applied to the Default - One Row per Item format
*
* @param array $column_headers {
* column headers in key => name format
* to modify the column headers, ensure the keys match these and set your own values
* }
* @param WC_Customer_Order_CSV_Export_Generator $csv_generator, generator instance
* @return array column headers in column_key => column_name format
*/
function sv_wc_csv_export_order_headers_split_item_meta( $column_headers, $csv_generator ) {
if ( sv_wc_csv_export_is_one_row( $csv_generator ) ) {
// remove item_meta column
unset( $column_headers['item_meta'] );
$item_meta_headers = array();
// loop over order ids
foreach ( $csv_generator->ids as $order_id ) {
$order = wc_get_order( $order_id );
// get line items
foreach ( $order->get_items() as $item ) {
// get item meta
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
$item_meta_headers[ $meta->key ] = 'item_meta: ' . $meta->key;
}
}
}
$column_headers = sv_wc_array_insert_after( $column_headers, 'item_total', $item_meta_headers );
}
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_order_headers_split_item_meta', 10, 2 );
/**
* CSV Order Export Row for One Row per Item.
*
* Filter the individual row data for the order export to add data for each item meta key
*
* @param array $order_data {
* order data in key => value format
* to modify the row data, ensure the key matches any of the header keys and set your own value
* }
* @return array the modified order data
*/
function sv_wc_csv_export_order_row_one_row_per_item_split_item_meta( $order_data ) {
$item = new WC_Order_Item_Product( $order_data['item_id'] );
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
$order_data[ $meta->key ] = $meta->value;
}
return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', 'sv_wc_csv_export_order_row_one_row_per_item_split_item_meta', 10 );
/** Helper Functions **********************************************************/
if ( ! function_exists( 'sv_wc_array_insert_after' ) ) :
/**
* Insert the given element after the given key in the array
*
* @param array $array array to insert the given element into
* @param string $insert_key key to insert given element after
* @param array $element element to insert into array
* @return array
*/
function sv_wc_array_insert_after( Array $array, $insert_key, Array $element ) {
$new_array = array();
foreach ( $array as $key => $value ) {
$new_array[ $key ] = $value;
if ( $insert_key == $key ) {
foreach ( $element as $k => $v ) {
$new_array[ $k ] = $v;
}
}
}
return $new_array;
}
endif;
if ( ! function_exists( 'sv_wc_csv_export_is_one_row' ) ) :
/**
* Helper function to check the export format
*
* @param \WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance
* @return bool - true if this is a one row per item format
*/
function sv_wc_csv_export_is_one_row( $csv_generator ) {
$one_row_per_item = false;
if ( version_compare( wc_customer_order_csv_export()->get_version(), '4.0.0', '<' ) ) {
// pre 4.0 compatibility
$one_row_per_item = ( 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format );
} elseif ( isset( $csv_generator->format_definition ) ) {
// post 4.0 (requires 4.0.3+)
$one_row_per_item = 'item' === $csv_generator->format_definition['row_type'];
}
return $one_row_per_item;
}
endif;
@Pransh20
Copy link

Pransh20 commented Dec 7, 2017

@dcolumbus
I am facing the same issue you pointed with the function WC_Order_Item_Meta.
It is deprecated now in woocommerce update

Were you able to change this script accordingly?

@bradholmes-studio
Copy link

really need this has anyone got this working

@tamarazuk
Copy link
Author

Sorry everyone, GitHub doesn't notify people about comments on gists. I have updated this to work with the latest Customer/Order/Coupon CSV Export version and WooCommerce 3.2

@frikinelmo
Copy link

THANK YOU THANK YOU THANK YOU!

@davidallenlewis
Copy link

davidallenlewis commented Feb 27, 2018

This only sort of works for me. It does separate the item_meta into separate columns but it creates a whole new set of columns for every order instead of just using one set of item_meta columns for all the orders. This results in so many columns that my spreadsheet software can't even create enough columns to import the CSV (Apple Numbers seems to be limited to 255 columns)

screen shot 2018-02-27 at 9 03 57 am

@davidallenlewis
Copy link

Replying to my post above... Tamara fixed this an updated the GIST. The issue was that the array keys being generated for the injected data were just too specific. Removing the meta_id from lines 37 and 67 fixed it!

@plastisound
Copy link

almost all my fields are empty!!! but I have a lot of attributes!!! any reason?

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