Created
December 17, 2014 20:14
-
-
Save WillBrubaker/d42c6a0d54bb09332554 to your computer and use it in GitHub Desktop.
Custom CSV output for the WooCommerce Customer/Order CSV Exporter
This file contains hidden or 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 to CSV Export. | |
| function wc_csv_export_modify_column_headers( $column_headers ) { | |
| $new_headers = array( | |
| 'shipping_country_name' => 'shipping_country_name' | |
| // 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, $csv_generator ) { | |
| // Get the array of country names by country code via the Woocommerce function. | |
| $countries = WC()->countries->get_countries(); | |
| $shipping_country = get_post_meta($order->id, '_shipping_country', true); | |
| $shipping_country_name = html_entity_decode($countries[$shipping_country]); | |
| $custom_data = array( | |
| 'shipping_country_name' => $shipping_country_name | |
| // add other row data here in the format column_key => data | |
| ); | |
| $new_order_data = array(); | |
| if ( isset( $csv_generator->order_format ) && ( 'default_one_row_per_item' == $csv_generator->order_format || 'legacy_one_row_per_item' == $csv_generator->order_format ) ) { | |
| foreach ( $order_data as $data ) { | |
| $new_order_data[] = array_merge( (array) $data, $custom_data ); | |
| } | |
| } else { | |
| $new_order_data = array_merge( $order_data, $custom_data ); | |
| } | |
| return $new_order_data; | |
| } | |
| add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 3 ); | |
| // reorder columns | |
| function wc_csv_export_reorder_columns( $column_headers ) { | |
| unset( $column_headers['shipping_country_name'] ); | |
| $new_column_headers = array(); | |
| foreach ( $column_headers as $column_key => $column_name ) { | |
| $new_column_headers[ $column_key ] = $column_name; | |
| if ( 'shipping_country' == $column_key ) { | |
| $new_column_headers['shipping_country_name'] = 'shipping_country_name'; | |
| } | |
| } | |
| return $new_column_headers; | |
| } | |
| add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_reorder_columns' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment