Last active
August 30, 2017 22:15
-
-
Save Garconis/618bcd07821d8e91a4ad28f66860c86d to your computer and use it in GitHub Desktop.
WooCommerce Customer / Order CSV Export | random hacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add Custom Column Headers | |
function wc_csv_export_modify_column_headers( $column_headers ) { | |
$new_headers = array( | |
'carrier' => 'Carrier', | |
'line_number' => 'Line Number', | |
// 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 ) { | |
foreach ( $order_data as $key => $item ) { | |
$custom_data = array( | |
'carrier' => 'USPS', | |
'line_number' => '1', | |
// 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 ); | |
/** | |
* Re-order Customer CSV Columns | |
* move carrier after shipping_country | |
* move line_number after discount_total | |
* unset the column, then reset it in the desired location | |
*/ | |
function wc_csv_export_reorder_columns( $column_headers ) { | |
// remove order total from the original set of column headers, otherwise it will be duplicated | |
unset( $column_headers['carrier'] ); | |
unset( $column_headers['line_number'] ); | |
$new_column_headers = array(); | |
foreach ( $column_headers as $column_key => $column_name ) { | |
$new_column_headers[ $column_key ] = $column_name; | |
if ( 'shipping_country' == $column_key ) { | |
// add order total immediately after order_number | |
$new_column_headers['carrier'] = 'Carrier'; | |
} | |
if ( 'discount_total' == $column_key ) { | |
// add order total immediately after order_number | |
$new_column_headers['line_number'] = 'Line Number'; | |
} | |
} | |
return $new_column_headers; | |
} | |
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_reorder_columns' ); | |
// Remove a Column | |
function wc_csv_export_remove_column( $column_headers ) { | |
// the list of column keys can be found in class-wc-customer-order-csv-export-generator.php | |
unset( $column_headers['shipment_tracking'] ); | |
return $column_headers; | |
} | |
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_remove_column' ); | |
// Set character length for fields, and tweak content of certain fields too | |
function wc_csv_export_trim_data( $order_data ) { | |
for( $i = 0; $i < count( $order_data ); $i++ ) { | |
if ( isset( $order_data[ $i ]['order_id'] ) ) { $order_data[ $i ]['order_id'] = mb_strimwidth($order_data[ $i ]['order_id'], 0, 20, ""); } | |
if ( isset( $order_data[ $i ]['order_date'] ) ) { $order_data[ $i ]['order_date'] = date_i18n('m/d/Y', strtotime( $order_data[ $i ]['order_date'] ) ); } | |
if ( isset( $order_data[ $i ]['billing_email'] ) ) { $order_data[ $i ]['billing_email'] = mb_strimwidth($order_data[ $i ]['billing_email'], 0, 80, ""); } | |
// Change billing_first_name to include both first and last name instead | |
if ( isset( $order_data[ $i ]['billing_first_name'] ) ) { $order_data[ $i ]['billing_first_name'] = mb_strimwidth($order_data[ $i ]['billing_first_name']." ".$order_data[ $i ]['billing_last_name'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['billing_address_1'] ) ) { $order_data[ $i ]['billing_address_1'] = mb_strimwidth($order_data[ $i ]['billing_address_1'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['billing_address_2'] ) ) { $order_data[ $i ]['billing_address_2'] = mb_strimwidth($order_data[ $i ]['billing_address_2'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['billing_city'] ) ) { $order_data[ $i ]['billing_city'] = mb_strimwidth($order_data[ $i ]['billing_city'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['billing_state'] ) ) { $order_data[ $i ]['billing_state'] = mb_strimwidth($order_data[ $i ]['billing_state'], 0, 2, ""); } | |
if ( isset( $order_data[ $i ]['billing_postcode'] ) ) { $order_data[ $i ]['billing_postcode'] = mb_strimwidth($order_data[ $i ]['billing_postcode'], 0, 10, ""); } | |
if ( isset( $order_data[ $i ]['billing_country'] ) ) { $order_data[ $i ]['billing_country'] = mb_strimwidth($order_data[ $i ]['billing_country'], 0, 2, ""); } | |
// Change shipping_first_name to include both first and last name instead | |
if ( isset( $order_data[ $i ]['shipping_first_name'] ) ) { $order_data[ $i ]['shipping_first_name'] = mb_strimwidth($order_data[ $i ]['shipping_first_name']." ".$order_data[ $i ]['shipping_last_name'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['shipping_address_1'] ) ) { $order_data[ $i ]['shipping_address_1'] = mb_strimwidth($order_data[ $i ]['shipping_address_1'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['shipping_address_2'] ) ) { $order_data[ $i ]['shipping_address_2'] = mb_strimwidth($order_data[ $i ]['shipping_address_2'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['shipping_city'] ) ) { $order_data[ $i ]['shipping_city'] = mb_strimwidth($order_data[ $i ]['shipping_city'], 0, 30, ""); } | |
if ( isset( $order_data[ $i ]['shipping_state'] ) ) { $order_data[ $i ]['shipping_state'] = mb_strimwidth($order_data[ $i ]['shipping_state'], 0, 2, ""); } | |
if ( isset( $order_data[ $i ]['shipping_postcode'] ) ) { $order_data[ $i ]['shipping_postcode'] = mb_strimwidth($order_data[ $i ]['shipping_postcode'], 0, 10, ""); } | |
if ( isset( $order_data[ $i ]['shipping_country'] ) ) { $order_data[ $i ]['shipping_country'] = mb_strimwidth($order_data[ $i ]['shipping_country'], 0, 2, ""); } | |
if ( isset( $order_data[ $i ]['carrier'] ) ) { $order_data[ $i ]['carrier'] = mb_strimwidth($order_data[ $i ]['carrier'], 0, 10, ""); } | |
if ( isset( $order_data[ $i ]['shipping_method'] ) ) { | |
$a = $order_data[ $i ]['shipping_method']; | |
//If shipping method contains USPS, Free Shipping, First, or Priority Mail(R), then force order export to show as "First", else display whatever else it may be | |
if (strpos($a, 'USPS') !== false) { | |
$order_data[ $i ]['shipping_method'] = mb_strimwidth("First", 0, 30, ""); | |
} | |
elseif (strpos($a, 'Free Shipping') !== false) { | |
$order_data[ $i ]['shipping_method'] = mb_strimwidth("First", 0, 30, ""); | |
} | |
elseif (strpos($a, 'First') !== false) { | |
$order_data[ $i ]['shipping_method'] = mb_strimwidth("First", 0, 30, ""); | |
} | |
elseif (strpos($a, 'Priority Mail®') !== false) { | |
$order_data[ $i ]['shipping_method'] = mb_strimwidth("First", 0, 30, ""); | |
} | |
else { | |
mb_strimwidth($order_data[ $i ]['shipping_method'], 0, 30, ""); | |
} | |
} | |
if ( isset( $order_data[ $i ]['shipping_total'] ) ) { $order_data[ $i ]['shipping_total'] = mb_strimwidth($order_data[ $i ]['shipping_total'], 0, 10, ""); } | |
if ( isset( $order_data[ $i ]['tax_total'] ) ) { $order_data[ $i ]['tax_total'] = mb_strimwidth($order_data[ $i ]['tax_total'], 0, 10, ""); } | |
if ( isset( $order_data[ $i ]['discount_total'] ) ) { $order_data[ $i ]['discount_total'] = mb_strimwidth($order_data[ $i ]['discount_total'], 0, 10, ""); } | |
if ( isset( $order_data[ $i ]['line_number'] ) ) { $order_data[ $i ]['line_number'] = mb_strimwidth($order_data[ $i ]['line_number'], 0, 3, ""); } | |
if ( isset( $order_data[ $i ]['item_sku'] ) ) { $order_data[ $i ]['item_sku'] = mb_strimwidth($order_data[ $i ]['item_sku'], 0, 20, ""); } | |
if ( isset( $order_data[ $i ]['item_quantity'] ) ) { $order_data[ $i ]['item_quantity'] = mb_strimwidth($order_data[ $i ]['item_quantity'], 0, 10, ""); } | |
//Divide item_total by item_quantity to get the item cost (unit price) | |
if ( isset( $order_data[ $i ]['item_total'] ) ) { $order_data[ $i ]['item_total'] = mb_strimwidth(($order_data[ $i ]['item_total'])/($order_data[ $i ]['item_quantity']), 0, 10, ""); } | |
} | |
return $order_data; | |
} | |
add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_trim_data' ); | |
// change CSV delimiter to a tab | |
function wc_csv_export_modify_delimiter() { | |
return "\t"; | |
} | |
add_filter( 'wc_customer_order_csv_export_delimiter', 'wc_csv_export_modify_delimiter' ); | |
apply_filters( 'wc_customer_order_csv_export_download_content_type', 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment