Woocommerce: Adding Total Order Weight To Order .CSV Export

So your courier asks you if you can provide a CSV of all your orders to make printing shipping labels a breeze – no problem. Thanks to SkyVerge and their awesome Customer / Order CSV Exporter plugin you can easily export a CSV with all the details you need… or can you?

Our courier – APC require that the total order weight is in the CSV in order to print out the correct shipping label (as is probably the case with many other couriers). This isn’t as simple as it sounds… Although SkyVerge provide some excellent info for developers and Nicola Mustone over at WooThemes provides some details on how to get the individual weights into the line item column they don’t tell you how to achieve this.

Anyway, the bit you’re all waiting for – the code 🙂 Add this to your themes functions.php and a new column will be added to the end of the export titled “Weight” with the orders total weight.

[php]

/**
* Add weight and full name column in CSV export
*/

function wc_csv_export_modify_column_headers( $column_headers ) {

$new_headers = array(
‘column_1’ => ‘Weight’,
);

return array_merge( $column_headers, $new_headers );
}
add_filter( ‘wc_customer_order_csv_export_order_headers’, ‘wc_csv_export_modify_column_headers’ );

$wc_csv_export_order_weight = 0;

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

global $wc_csv_export_order_weight;

$custom_data = array(
‘column_1’ => $wc_csv_export_order_weight,
);

$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 );
}

$wc_csv_export_order_weight = 0;

return $new_order_data;
}

add_filter( ‘wc_customer_order_csv_export_order_row’, ‘wc_csv_export_modify_row_data’, 10, 3 );

function sv_add_weight_to_csv_export_line_item( $line_item, $item, $product, $order ) {

global $wc_csv_export_order_weight;

if ( $product->get_weight() ) {
$wc_csv_export_order_weight += $product->get_weight();
}

return $line_item;
}

add_filter( ‘wc_customer_order_csv_export_order_line_item’, ‘sv_add_weight_to_csv_export_line_item’, 10, 4 );

[/php]

Leave a Reply

Your email address will not be published. Required fields are marked *