Skip to content
Advertisement

WooCommerce Order Export: Get custom data per line item

I’m using the “WooCommerce Customer / Order / Coupon Export” plugin to export my orders as a CSV file.

I’m exporting the orders in a one row per item format. This means that every order has multiple lines. One line per order item.

Now I want to add some extra data to every line item. For example the author (in our case vendor) of the item. I found a good way to add exta data to the export. It could be found in the documentation of the plugin: https://docs.woocommerce.com/document/ordercustomer-csv-export-developer-documentation/#section-14

It works but it’s not exactly what I need. I could add some data to the whole order. So every line item gets the same data.

So I tried to change the code and go through every line item. Unfortunatly with the same result. Every line item gets the same data. In my case the data from the last item of the order.

Here’s how I’ve changed the step 2 of the example from the official docs:

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

        // Example showing how to extract order metadata into it's own column
        //$meta_key_example = is_callable( array( $order, 'get_meta' ) ) ? $order->get_meta( 'meta_key_example' ) : $order->meta_key_example;
        //$meta_key_example = is_callable( array( $order, 'get_meta' ) ) ? $order->get_meta( 'meta_key_example' ) : $order->meta_key_example;


        // Loop through order line items

        $allItems = $order->get_items();

        foreach( $allItems as $item_line ){

            $item_vendor_id     = get_post_field( 'post_author', $item_line->get_product_id() );

            $custom_data = array(


                // User/Vendor
                'item_vendor_id'            => $item_vendor_id,
                'vendor_username'           => get_the_author_meta( 'username', $item_vendor_id ),
                'vendor_user_email'         => get_the_author_meta( 'user_email', $item_vendor_id ),

                // Address
                'vendor_company'            => get_the_author_meta( '_wcv_custom_settings_company_name', $item_vendor_id ),
                'vendor_street'             => get_the_author_meta( '_wcv_store_address1', $item_vendor_id ),
                'vendor_housenumber'        => get_the_author_meta( '_wcv_store_address2', $item_vendor_id ),
                'vendor_zip'                => get_the_author_meta( '_wcv_store_postcode', $item_vendor_id ),
                'vendor_city'               => get_the_author_meta( '_wcv_store_city', $item_vendor_id ),
                'vendor_country'            => get_the_author_meta( '_wcv_store_country', $item_vendor_id ),

                // Bank
                'vendor_bank_name'          => get_the_author_meta( 'wcv_bank_name', $item_vendor_id ),
                'vendor_bank_account_name'  => get_the_author_meta( 'wcv_bank_account_name', $item_vendor_id ),
                'vendor_iban'               => get_the_author_meta( 'wcv_bank_iban', $item_vendor_id ),
                'vendor_bic'                => get_the_author_meta( 'wcv_bank_bic_swift', $item_vendor_id ),

                );
        }


        return sv_wc_csv_export_add_custom_order_data( $order_data, $custom_data, $csv_generator );
}
add_filter( 'wc_customer_order_export_csv_order_row', 'sv_wc_csv_export_modify_row_data_example', 10, 3 );

I guess that I’m adding the data at the wrong position?! But I couldn’t figure out where the problem is.

EDIT: After the comment from @CBroe I tried to use wc_customer_order_csv_export_order_line_item.

I found an example here but that’s destroying my CSV file:

/**
 * Add weight as line item meta in CSV export Default format
 */
function sv_add_weight_to_csv_export_line_item( $line_item, $item, $product, $order ) {

    $line_item['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 );

/**
 * Add weight as line item meta in CSV export CSV Import format
 */
function sv_add_weight_to_csv_export_import_format( $order_data, $order ) {

    $count = 1;

    // add line items
    foreach ( $order->get_items() as $item ) {

        $product = $order->get_product_from_item( $item );

        if ( ! is_object( $product ) ) {
            $product = new WC_Product( 0 );
        }

        if ( $weight = $product->get_weight() ) {
            $order_data[ "order_item_{$count}" ] .= '|weight: ' . $weight;
        }

        $count++;
    }

    return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row', 'sv_add_weight_to_csv_export_import_format', 20, 2 );

But I’m still trying…

Advertisement

Answer

I found an answer. It was an example hidden in the official docs: https://github.com/skyverge/wc-plugins-snippets/blob/master/woocommerce-customer-order-coupon-export/csv/add-item-meta-to-order-export.php

/**
 * Add line item meta to the Order CSV Export in Default format
 * Example: add weight to the item meta data
 */


/**
 * Step 1. Add weight to line item data
 *
 * @param array $line_item the original line item data
 * @param array $item the item's order data
 * @param object $product the WC_Product object for the line
 * @param object $order the WC_Order object being exported
 * @return array the updated line item data
 */
function sv_wc_csv_export_add_weight_to_line_item( $line_item, $item, $product, $order ) {

    $new_item_data = array();

    foreach ( $line_item as $key => $data ) {

        $new_item_data[ $key ] = $data;

        if ( 'sku' === $key && $product ) {
            $new_item_data['weight'] = wc_format_decimal( $product->get_weight(), 2 );
        }
    }

    return $new_item_data;
}
add_filter( 'wc_customer_order_export_csv_order_line_item', 'sv_wc_csv_export_add_weight_to_line_item', 10, 4 );


/**
 * (optional) Step 2. Add a separate `item_weight` column to the default and custom formats
 *
 * @param array $column_headers the original column headers
 * @param CSV_Export_Generator $csv_generator the generator instance
 * @return array - the updated column headers
 */
function sv_wc_csv_export_modify_column_headers_item_price( $column_headers, $csv_generator ) {

    $new_headers = array();

    if ( sv_wc_csv_export_is_one_row( $csv_generator ) ) {

        foreach( $column_headers as $key => $column ) {

            $new_headers[ $key ] = $column;

            // add the item_price after the SKU column
            if ( 'item_sku' === $key ) {
                $new_headers['item_weight'] = 'item_weight';
            }
        }

    } else {

        return $column_headers;
    }

    return $new_headers;
}
add_filter( 'wc_customer_order_export_csv_order_headers', 'sv_wc_csv_export_modify_column_headers_item_price', 10, 2 );


/**
 * (optional) Step 3. Add the item weight data (step 1) to the new item_weight column (step 2)
 * 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 - the updated order data
 */
function sv_wc_csv_export_order_row_one_row_per_item_weight( $order_data, $item ) {

    $order_data['item_weight'] = $item['weight'];
    return $order_data;
}
add_filter( 'wc_customer_order_export_csv_order_row_one_row_per_item', 'sv_wc_csv_export_order_row_one_row_per_item_weight', 10, 2 );


if ( ! function_exists( 'sv_wc_csv_export_is_one_row' ) ) :

/**
 * Helper function to check the export format
 *
 * @param WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance
 * @return bool - true if this is a one row per item format
 */
function sv_wc_csv_export_is_one_row( $csv_generator ) {

    $one_row_per_item = false;

    if ( version_compare( wc_customer_order_csv_export()->get_version(), '4.0.0', '<' ) ) {

        // pre 4.0 compatibility
        $one_row_per_item = ( 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format );

    } elseif ( isset( $csv_generator->format_definition ) ) {

        // post 4.0 (requires 4.0.3+)
        $one_row_per_item = 'item' === $csv_generator->format_definition['row_type'];
    }

    return $one_row_per_item;
}

endif;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement