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.


/**
 * 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 );

4 thoughts on “Woocommerce: Adding Total Order Weight To Order .CSV Export

  1. Hi, thanks for taking the time to post this! Do you know if it is still working with the latest version of Woocommerce (2.4.7)? I’ve added the code but it isn’t creating the extra column.

    Seems strange that this functionality isn’t in the plugin already.

  2. Hi, thanks for this piece of code. Just a quick question… Should this calculate the total weight of the order? It only appears to be calculating the weight of 1 item per order currently on my dev site.

    Eg. If 2 products, weighing 1Kg are purchased, the export produces an order showing just 1Kg instead of 2Kg.

    Kind regards!

Leave a Reply

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