Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bekarice/bbb3566b8f2cfd7957fe to your computer and use it in GitHub Desktop.
Save bekarice/bbb3566b8f2cfd7957fe to your computer and use it in GitHub Desktop.
Rename, Re-order, or Remove columns from Customer CSV with WooCommerce Customer/Order CSV Export
<?php
/**
* These samples do not have to be used together
* They show how to rename, re-order, or remove columns
* from the exported customer CSV
*/
/**
* Rename Customer CSV Columns
* Sample: rename the customer_id column to User ID
* make sure to not change the key (`customer_id`) as this matches the column to the relevant data
* change the value of the array to change the column header that's exported
*/
function sv_wc_csv_export_rename_customer_column( $column_headers ) {
$column_headers['customer_id'] = 'User ID';
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_customer_headers', 'sv_wc_csv_export_rename_customer_column' );
/**
* Re-order Customer CSV Columns
* Sample: move customer_id after last_name
* unset the column, then reset it in the desired location
*/
function sv_wc_csv_export_reorder_customer_columns( $column_headers ) {
// remove customer_id from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['customer_id'] );
$new_column_headers = array();
foreach ( $column_headers as $key => $name ) {
$new_column_headers[ $key ] = $name;
if ( 'last_name' == $key ) {
// re-add customer_id immediately after last_name
$new_column_headers['customer_id'] = 'customer_id';
}
}
return $new_column_headers;
}
add_filter( 'wc_customer_order_csv_export_customer_headers', 'sv_wc_csv_export_reorder_customer_columns' );
/**
* Remove Customer CSV Columns
* Sample: remove shipping_country column
* Unset the column you'd like to remove
* the list of column keys can be found in class-wc-customer-order-csv-export-generator.php
*/
function sv_wc_csv_export_remove_customer_column( $column_headers ) {
unset( $column_headers['shipping_country'] );
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_customer_headers', 'sv_wc_csv_export_remove_customer_column' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment