Skip to content

Instantly share code, notes, and snippets.

@rayflores
Last active March 16, 2021 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayflores/7b428963d726d4303a83 to your computer and use it in GitHub Desktop.
Save rayflores/7b428963d726d4303a83 to your computer and use it in GitHub Desktop.
need custom counter per line (row)
<?php
/**
* Plugin Name: WooCommerce Modify CSV output
* Plugin URI: http://rayflores.com/plugins/csv-modifier/
* Description: Dan Colucci modifier
* Version: 1.0.1
* Author: Ray Flores
* Author URI: http://rayflores.com
*/
/**
* Add the product price to the individual line item entry
*
* @param array $line_item the original line item data
* @param array $item WC order item data
* @param WC_Product $product the product
* @return string
*/
$GLOBALS['wc_csv_export_index'] = 0;
function wc_csv_export_order_line_line_number( $line_item, $item, $product) {
$line_item['num'] = $GLOBALS['wc_csv_export_index']+1; //$product->get_price();
$line_item['price'] = $product->get_price();
$line_item['sku'] = $product->get_sku();
$line_item['item_qty'] = $item['qty'];
$GLOBALS['wc_csv_export_index']++;
return $line_item;
}
add_filter( 'wc_customer_order_csv_export_order_line_item', 'wc_csv_export_order_line_line_number', 10, 3 );
/**
* Add the item_price column data for the Default - One Row per Item format
*
* @param array $order_data the original order data
* @param array $item the item for this row
* @return array
*/
function wc_csv_export_order_row_one_row_per_line_number( $order_data, $item ) {
$order_data['line_number'] = $item['num'];
$order_data['item_price'] = $item['price'];
$order_data['item_sku'] = $item['sku'];
$order_data["item_qty"] = $item['item_qty'];
return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', 'wc_csv_export_order_row_one_row_per_line_number', 10, 2 );
// set order columns
function rf_wc_csv_export_custom_order_columns( $column_headers, $csv_generator ){
if ( 'default_one_row_per_item' === $csv_generator->order_format ) {
$column_headers = array(
'cust_num' => '', // custom numbering
'order_number' => '', // 'Order Number',
'order_date' => '', // 'Order Date',
'billing_email' => '', // 'E-Mail Address',
'billing_full_name' => '', // 'Bill-to Name',
'billing_address_1' => '', // 'Bill-to Address',
'billing_address_2' => '', // 'Bill-to Address2',
'billing_city' => '', // 'Bill-to City',
'billing_state' => '', // 'Bill-to State',
'billing_postcode' => '', // 'Bill-to Zip Code',
'billing_country' => '', // 'Bill-to Country',
'shipping_full_name' => '', // 'Ship-to Name',
'shipping_address_1' => '', // 'Ship-to Address',//shipping address line 1
'shipping_address_2' => '', // 'Ship-to Address2',//shipping address line 2
'shipping_city' => '', // 'Ship-to City',//shipping cite
'shipping_state' => '', // 'Ship-to State',//shipping state
'shipping_postcode' => '', // 'Ship-to Zip Code',//shipping postal code
'shipping_country' => '', // 'Ship-to Country',//shipping country
'shipping_method' => '', // 'Carrier',
'shipping_method_service_level' => '', // 'Carrier Service Level',
'shipping_total' => '', // 'Shipping Price',
'tax_total' => '', // 'Sales Tax',
'order_discount' => '', // 'Order Discount',
'line_number' => '', // 'Line Number', // line number
'item_sku' => '', // 'Item Number (SKU)',
'item_qty' => '', // 'Quantity', // item quantity
'item_price' => '', // 'Unit Price',
);
}
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'rf_wc_csv_export_custom_order_columns', 10, 4 );
function rf_wc_csv_export_modify_row_data( $order_data, $order, $csv_generator) {
foreach ($order as $cust_data) {
$i = 0;
foreach ($order->get_items() as $item ) {
$custom_data['cust_num'] = ++$i;
}
$custom_data['billing_full_name'] = $order->billing_first_name . ' ' . $order->billing_last_name;
$custom_data['shipping_full_name'] = $order->shipping_first_name . ' ' . $order->shipping_last_name;
}
if ( 'default_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', 'rf_wc_csv_export_modify_row_data', 10, 3 );
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);
@greguly
Copy link

greguly commented Jun 12, 2015

function rf_wc_csv_export_modify_row_data( $order_data, $order, $csv_generator) {

$custom_data['billing_full_name'] = $order->billing_first_name . ' ' . $order->billing_last_name;
$custom_data['shipping_full_name'] = $order->shipping_first_name . ' ' . $order->shipping_last_name;

if ( 'default_one_row_per_item' === $csv_generator->order_format ) {

    $i = 0;
    foreach ( $order_data as $data ) {
            ++$i; 
            $custom_data['cust_num'] = $i;

        $new_order_data[] = array_merge( (array) $data, $custom_data );
    }

} else {


    $custom_data['cust_num'] = 1;

    $new_order_data = array_merge( $order_data, $custom_data );
}
return $new_order_data;

}

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