Skip to content

Instantly share code, notes, and snippets.

@rayflores
Last active December 10, 2015 22:46
Show Gist options
  • Save rayflores/1b5765885cde38505d64 to your computer and use it in GitHub Desktop.
Save rayflores/1b5765885cde38505d64 to your computer and use it in GitHub Desktop.
Need to be able to use "Default - One Row Per Item" setting but not getting "admin custom fields" and not getting concatenated shipping_firstname + shipping_last_name ---- seems to work just fine with "Default"
<?php
/**
* Plugin Name: WooCommerce Modify CSV Exported file
* Description: Adds and removes desired and undesired fields respectively
* Version: 1.0.0
* Author: Ray Flores
* Author URI: http://www.rayflores.com
* Requires at least: 3.9
* Tested up to: 3.9
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// set the data for each for custom columns
function rf_wc_csv_export_modify_row_data( $order_data, $order, $generator ) {
$new_order_data = array();
$order_fields = $GLOBALS['wc_admin_custom_order_fields']->get_order_fields( $order->id );
$custom_data = array(
'cust_num' => is_object( $order_fields[3] ) ? $order_fields[3]->get_value_formatted() : '',
'shipping_full_name' => $order->shipping_first_name . ' ' . $order->shipping_last_name,
'shipping_address_1' => $order->shipping_address_1,
'shipping_address_2' => $order->shipping_address_2,
'shipping_postcode' => $order->shipping_postcode,
'customer_message' => $order->customer_message,
'custom_tracking_provider' => '', // Add meta here
);
if ( 'default_one_row_per_item' === $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', 'rf_wc_csv_export_modify_row_data', 10, 3 );
// set order columns
function rf_wc_csv_export_custom_order_columns( $column_headers, $generator ){
if ( 'default_one_row_per_item' === $generator->order_format ) {
$column_headers = array(
'cust_num' => 'cust#',
'order_date' => 'date',
'date_shipped' => 'shipment_date',
'blank' => 'blank',
'custom_tracking_provider' => 'ship_via',
'order_id' => 'order_id',
'item_sku' => 'item #',
'item_quantity' => 'quantity',
'shipping_full_name' => 'First and Last name',
'shipping_last_name' => 'Last name',
'shipping_address_1' => 'Address1',
'shipping_address_2' => 'address2',
'shipping_city' => 'city',
'shipping_state' => 'st',
'shipping_postcode' => 'zip',
'customer_note' => 'customer notes',
);
}
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'rf_wc_csv_export_custom_order_columns', 10, 4 );
<?php
/**
* Plugin Name: WooCommerce Modify CSV Exported file
* Description: Adds and removes desired and undesired fields respectively
* Version: 1.0.0
* Author: Ray Flores
* Author URI: http://www.rayflores.com
* Requires at least: 3.9
* Tested up to: 3.9
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// 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['order_id']);
unset( $column_headers['order_number'] );
//unset( $column_headers['order_date']);
unset( $column_headers['status'] );
unset( $column_headers['shipping_total'] );
unset( $column_headers['shipping_tax_total'] );
unset( $column_headers['tax_total'] );
unset( $column_headers['cart_discount'] );
unset( $column_headers['order_discount'] );
unset( $column_headers['discount_total'] );
unset( $column_headers['order_total'] );
unset( $column_headers['payment_method'] );
unset( $column_headers['shipping_method'] );
unset( $column_headers['customer_id'] );
unset( $column_headers['billing_first_name'] );
unset( $column_headers['billing_last_name'] );
unset( $column_headers['billing_company'] );
unset( $column_headers['billing_email'] );
unset( $column_headers['billing_phone'] );
unset( $column_headers['billing_address_1'] );
unset( $column_headers['billing_address_2'] );
unset( $column_headers['billing_postcode'] );
unset( $column_headers['billing_city'] );
unset( $column_headers['billing_state'] );
unset( $column_headers['billing_country'] );
unset( $column_headers['shipping_company'] );
unset( $column_headers['billing_country'] );
unset( $column_headers['shipping_country']);
unset( $column_headers['_custom_tracking_link']);
unset( $column_headers['tracking_provider']);
unset( $column_headers['_date_shipped']);
unset( $column_headers['tracking_number']);
unset( $column_headers['customer_note'] );
unset( $column_headers['shipping_first_name'] );
unset( $column_headers['shipping_address_1'] );
unset( $column_headers['shipping_address_2'] );
unset( $column_headers['shipping_postcode'] );
unset( $column_headers['shipping_city'] );
unset( $column_headers['shipping_state'] );
unset( $column_headers['shipping_country'] );
unset( $column_headers['shipping_last_name'] );
unset( $column_headers['shipping_items']);
unset( $column_headers['tax_items']);
unset( $column_headers['coupon_items']);
unset( $column_headers['order_notes']);
unset( $column_headers['order_currency']);
unset( $column_headers['item_name']);
unset( $column_headers['item_sku']);
unset( $column_headers['item_quantity']);
unset( $column_headers['item_total']);
unset( $column_headers['item_meta']);
unset( $column_headers['line_items']);
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_remove_column' );
function remove_filters_from_shipping_class( ) {
if (class_exists('WC_Shipment_Tracking')) {
global $WC_Shipment_Tracking; //globalize class from another plugin
// filters from WC_Shipment_Tracking class - which add the tracking info into the exporter class - needed to remove entire filters
remove_filter( 'wc_customer_order_csv_export_order_headers', array( &$WC_Shipment_Tracking, 'add_tracking_info_to_csv_export_column_headers' ) );
// remove_filter( 'wc_customer_order_csv_export_order_row', array( &$WC_Shipment_Tracking, 'add_tracking_info_to_csv_export_column_data' ), 10, 3 );
}
}
add_action('plugins_loaded','remove_filters_from_shipping_class'); // after shipment-tracking plugin has loaded
function sv_wc_customer_order_csv_export_item_multi_column_headers( $column_headers, $generator ) {
$new_column_headers = array();
if ( 'default_one_row_per_item' === $generator->order_format ) {
foreach ( $column_headers as $column_key => $column_header ) {
$new_column_headers['cust_num'] = 'cust_num';
$new_column_headers[ $column_key ] = $column_header;
}
$column_headers = $new_column_headers;
}
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_customer_order_csv_export_item_multi_column_headers', 10, 2 );
// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order ) {
$order_fields = $GLOBALS['wc_admin_custom_order_fields']->get_order_fields( $order->id );
foreach( $order->get_items() as $_ => $item ) {
$product = $order->get_product_from_item( $item ); }
$custom_data = array(
'cust_num' => is_object( $order_fields[3] ) ? $order_fields[3]->get_value_formatted() : '',
'blank' => is_object( $order_fields[99999] ) ? $order_fields[99999]->get_value_formatted() : '',
'item_sku' => $product->get_sku(),
'item_quantity' => $item['qty'],
'shipping_full_name' => $order->shipping_first_name . ' ' . $order->shipping_last_name,
'shipping_address_1' => $order->shipping_address_1,
'shipping_address_2' => $order->shipping_address_2,
'shipping_postcode' => $order->shipping_postcode,
'customer_message' => $order->customer_message
// 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 );
// reorder columns
function wc_csv_export_custom_order_columns( $column_headers, $generator ){
$new_column_headers = array();
if ( 'default_one_row_per_item' === $generator->order_format ) {
foreach ( $column_headers as $column_key => $column_name ) {
$new_column_headers[ $column_key ] = $column_name;
$new_column_headers['cust_num'] = 'cust#';
$new_column_headers['order_date'] = 'date';
$new_column_headers['date_shipped'] = 'shipment_date';
$new_column_headers['blank'] = 'blank';
$new_column_headers['custom_tracking_provider'] = 'ship_via';
$new_column_headers['order_id'] = 'order_id';
$new_column_headers['item_sku'] = 'item #';
$new_column_headers['item_quantity'] = 'quantity';
$new_column_headers['shipping_full_name'] = 'First and Last name';
$new_column_headers['shipping_last_name'] = 'Last name';
$new_column_headers['shipping_address_1'] = 'Address1';
$new_column_headers['shipping_address_2'] = 'address2';
$new_column_headers['shipping_city'] = 'city';
$new_column_headers['shipping_state'] = 'st';
$new_column_headers['shipping_postcode'] = 'zip';
$new_column_headers['customer_note'] = 'customer notes';
unset( $column_headers['order_date']);
}
}
return $new_column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_custom_order_columns', 10, 4 );
@tamarazuk
Copy link

@rayflores Here's an updated and simplified Gist: https://gist.github.com/tamarazuk/c5a4caaef8c5f450d81d

Rather than unsetting the columns you do not want, I overwrote the column headings in rf_wc_csv_export_custom_order_columns(). This also means you don't need wc_csv_export_remove_column() or remove_filters_from_shipping_class().

I made bunch of other changes which are outlined here: https://gist.github.com/tamarazuk/c5a4caaef8c5f450d81d/revisions

@rayflores
Copy link
Author

thank you so much! this works... client wished to use shipping-tracking on the site, so, i used the remove filters from that plugin 👍

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