Skip to content

Instantly share code, notes, and snippets.

@rayflores
Created December 19, 2014 20:33
Show Gist options
  • Save rayflores/dc843b57d04253777398 to your computer and use it in GitHub Desktop.
Save rayflores/dc843b57d04253777398 to your computer and use it in GitHub Desktop.
add globals for indexer :) works now...
<?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
*/
$GLOBALS['wc_csv_export_index'] = 0;
// add reverse order to orders by order number, as they are exported.
function rf_wc_csv_add_query_args( $query_args) {
$query_args += array(
'orderby' => 'date_query',
'order' => 'ASC',
);
return $query_args;
}
add_filter( 'wc_customer_order_csv_export_admin_query_args', 'rf_wc_csv_add_query_args', 10,2);
// set the data for each for custom columns
function rf_wc_csv_export_modify_row_data( $order_data, $order, $generator) {
foreach ($order as $cust_data) {
$custom_data['cust_num'] = $GLOBALS['wc_csv_export_index']+1;
}
foreach ( $order->get_shipping_methods() as $_ => $shipping_item ) {
if ($shipping_item['name'] == 'International') {
$custom_data['shipping_method'] = "INT";
} else {
$custom_data['shipping_method'] = "RG1";
} //endif
}
$i=1;
foreach ( $order->get_items() as $item_key => $item ) {
$product = $order->get_product_from_item( $item );
$sku = $product->get_sku();
$custom_data["item_{$i}"] = $sku;
$custom_data["item_{$i}_qty"] = $item['qty'];
$custom_data['order_number'] = preg_replace('/[^\p{L}\p{N}\s]/u', '', $order->get_order_number());
$custom_data['source_code'] = 3;
$custom_data['payment_method'] = "I";
$i++;
}
while ( $i < 6 ) {
$custom_data["item_{$i}"] = '';
$custom_data["item_{$i}_qty"] = '';
$i++;
}
/*
'item_1' => $item['name'],
'item_1_qty' => $custom_data["item_{$i}_qty"] ,
'item_2' => $item['name'];
'item_2_qty' => $custom_data["item_{$i}_qty"] ,
'item_3' => $item['name'];
'item_3_qty' => $custom_data["item_{$i}_qty"] ,
'item_4' => $item['name'];
'item_4_qty' => $custom_data["item_{$i}_qty"] ,
'item_5' => $item['name'];
'item_5_qty' => $custom_data["item_{$i}_qty"] ,
'cust_num' => '',//$this_num,//$cust_num,
'payment_method' => 'I',
'source_code' => 3,
'billing_first_name' => $order->billing_first_name,
'billing_last_name' => $order->billing_last_name,
'billing_email' => $order->billing_email,
'billing_phone' => $order->billing_phone,
'billing_address_1' => $order->billing_address_1,
'billing_address_2' => $order->billing_address_2,
'billing_postcode' => $order->billing_postcode,
'billing_city' => $order->billing_city,
'billing_state' => $order->billing_state,
'billing_country' => $order->billing_country,
'shipping_first_name' => $order->shipping_first_name,
'shipping_last_name' => $order->shipping_last_name,
'shipping_company' => $order->shipping_company,
'shipping_address_1' => $order->shipping_address_1,
'shipping_address_2' => $order->shipping_address_2,
'shipping_postcode' => $order->shipping_postcode,
'shipping_city' => $order->shipping_city,
'shipping_state' => $order->shipping_state,
'shipping_country' => $order->shipping_country,
'order_number' => preg_replace('/[^\p{L}\p{N}\s]/u', '', $order->get_order_number()),
);
*/
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 );
}
$GLOBALS['wc_csv_export_index']++;
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' === $generator->order_format ) {
$column_headers = array(
'cust_num' => '',//CUSTNUM',
'billing_last_name' => '',//CUSTOMER LAST NAME',
'billing_first_name' => '',//CUSTOMER FIRST NAME',
'blank' => '',//BLANK',
'billing_address_1' => '',//CUSTOMER ADDRESS 1',
'billing_address_2' => '',//CUSTOMER ADDRESS 2',
'billing_city' => '',//CUSTOMER CITY',
'billing_state' => '',//CUSTOMER STATE',
'billing_postcode' => '',//CUSTOMER POSTAL CODE',
'billing_phone' => '',//CUSTOMER PHONE #1',
'keycode' => '',//KEYCODE',
'po_number' => '',//PO #',
'shipping_method' => '',//SHIP VIA',
'blank2' => '',//BLANK',
'blank3' => '',//BLANK',
'order_date' => '',//ORDER DATE',
'order_number' => '',//ORDER NUMBER',
'item_1' => '',//ITEM 1',
'item_1_qty' => '',//ITEM 1 QTY',
'item_2' => '',//ITEM 2',
'item_2_qty' => '',//ITEM 2 QTY',
'item_3' => '',//ITEM 3',
'item_3_qty' => '',//ITEM 3 QTY',
'item_4' => '',//ITEM 4',
'item_4_qty' => '',//ITEM 4 QTY',
'item_5' => '',//ITEM 5',
'item_5_qty' => '',//ITEM 5 QTY',
'shipping_last_name' => '',//SHIP TO LAST NAME',
'shipping_first_name' => '',//SHIP TO FIRST NAME',
'blank4' => '',//BLANK',
'shipping_address_1' => '',//SHIP TO ADDRESS 1',
'shipping_address_2' => '',//SHIP TO ADDRESS 2',
'shipping_city' => '',//SHIP TO CITY',
'shipping_state' => '',//SHIP TO STATE',
'shipping_postcode' => '',//SHIP TO POSTAL CODE',
'payment_method' => '',//PAYMENT METHOD CODE',
'blank5' => '',//BLANK',
'blank6' => '',//BLANK',
'blank7' => '',//BLANK',
'blank8' => '',//BLANK',
'blank9' => '',//BLANK',
'blank10' => '',//BLANK',
'blank11' => '',//BLANK',
'blank12' => '',//BLANK',
'billing_email' => '',//CUSTOMER EMAIL',
'billing_country' => '',//CUSTOMER COUNTRY',
'shipping_country' => '',//SHIP TO COUNTRY',
'billing_phone_2' => '',//CUSTOMER PHONE #2',
'shipping_phone' => '',//SHIP TO PHONE #1',
'shipping_phone_2' => '',//SHIP TO PHONE #2',
'blank13' => '',//BLANK',
'blank14' => '',//BLANK',
'blank15' => '',//BLANK',
'blank16' => '',//BLANK',
'blank17' => '',//BLANK',
'source_code' => '',//SOURCE CODE',
);
}
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'rf_wc_csv_export_custom_order_columns', 10, 4 );
function rf_wc_remove_column_headers($csv){
$first_line = strpos($csv,"\n");
$csv = substr($csv,$first_line+1);
return $csv;
}
add_filter('wc_customer_order_csv_export_generated_csv','rf_wc_remove_column_headers', 10, 4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment