0

I'm trying to customize the WooCommerce CSV Export plugin to add additional columns for two standard product attributes ("fund" and "appeal code").

I'm able to add the actual headers, but can't get any data in the rows. Here is the code I'm trying:

// add custom column headers
function wc_csv_export_modify_column_headers( $column_headers ) { 

    $new_headers = array(
        'fund' => 'fund',
        'appeal' => 'appeal_code',
        // add other column headers here in the format column_key => Column Name
    );

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

// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order, $csv_generator ) {

    $custom_data = array(
        'fund' => wc_get_product_terms( $product->id, 'pa_fund' ),
        'appeal' => wc_get_product_terms( $product->id, 'pa_appeal-code' ),
        // add other row data here in the format column_key => data
    );

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

    return $new_order_data;
}
add_filter(
    'wc_customer_order_csv_export_order_row',
    'wc_csv_export_modify_row_data',
    10,
    3 
);
  • Print $custom_data value and check if the values are there in the array – WisdmLabs Aug 19 '15 at 13:55
  • Sorry, my php knowledge is still lacking. Where/how would I print $custom_data value? – dpruth Aug 19 '15 at 20:46
0

With the help of a more knowledgeable colleague, we got it working. Here's the code:

/** Add custom column headers **/
function wc_csv_export_modify_column_headers( $column_headers ) {

    $new_headers = array(
        'fund' => 'fund',
        'appeal' => 'appeal_code',
    );

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

/** Set the data for each for custom columns **/
function wc_csv_export_modify_row_data( $order_data, $item, $order, $csv_generator ) {

    // Determine the product ID from the SKU, and use that to find the fund and appeal code
    $pid = wc_get_product_id_by_sku($item[sku]);
    $fund = wc_get_product_terms( $pid, 'pa_fund' );
    $appeal = wc_get_product_terms( $pid, 'pa_appeal-code' );

    // Add the fund & appeal code to the order_data & return each line
    $order_data["fund"] = $fund[0];
    $order_data["appeal"] = $appeal[0];
    return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item',
            'wc_csv_export_modify_row_data', 10, 4 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.