Title: Using Fields in CSV Order Export
Last modified: August 31, 2016

---

# Using Fields in CSV Order Export

 *  [fitbox](https://wordpress.org/support/users/fitmealat/)
 * (@fitmealat)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/using-fields-in-csv-order-export/)
 * Author posted this in another thread:
 * // Loop through your order line items & display whatever custom fields you want
   //
   using wc_get_order_item_meta( $item_id, $key );
 * could you please show how this is done?!
 * i want to export the fields with the orders CSV plugins and need todo exactly
   what you have written up there but not sure how to approach.
 * any help would be appreciated!
 * [https://wordpress.org/plugins/wc-fields-factory/](https://wordpress.org/plugins/wc-fields-factory/)
 * love the plugin thanks!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Saravana Kumar K](https://wordpress.org/support/users/mycholan/)
 * (@mycholan)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/using-fields-in-csv-order-export/#post-7039616)
 * Hi, could you tell me which exporter plugin you are using.? so that I can examine
   and if possible I will give the patch update for that plugin so that it includes
   Order Item Meta as well.
 *  Thread Starter [fitbox](https://wordpress.org/support/users/fitmealat/)
 * (@fitmealat)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/using-fields-in-csv-order-export/#post-7039621)
 * Hi Kumar,
 * thx for the reply.
 * I am using [this one.](https://www.woothemes.com/products/ordercustomer-csv-export)
 * i put a snipped in my functions.php to export various extra custom fields that
   i need.
 * I also want to include some data from some fields from your plugin.
 * example for some other data:
 * //add the data headers
 *     ```
       function wc_csv_export_modify_column_headers( $column_headers ) { 
   
       	$new_headers = array(
       		'_billing_sundaytime' => 'Montag',
       		// add other column headers here in the format column_key => Column Name
       	);
       	return array_merge($new_headers, $column_headers);
       }
       add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_csv_export_modify_column_headers' );
       ```
   
 * //now fill the data”
 *     ```
       function wc_csv_export_modify_row_data( $order_data, $order, $csv_generator ) {
       $custom_data = array(
       '_billing_sundaytime' => get_post_meta( $order->id, '_billing_sundaytime', true ),
       );
   
       add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 3 );
       ```
   
 * for sku for example its alittle more complicated:
 *     ```
       global $wpo_wcpdf;
       	global $wpo_wcpdf_pro;
       	$rechnungsnummer = $wpo_wcpdf->export->get_invoice_number( $order->id );
       	//$gutschriftnummer = $wpo_wcpdf_pro->get_number('credit_note', $order->id);
   
       	$refunds = $order->get_refunds();
       			foreach ($refunds as $key => $refund) {
       				if ($credit_note_number = $wpo_wcpdf_pro->get_number('credit-note', $refund->id)) {
       					$credit_note_numbers[] = $credit_note_number;
       				}
       			}
       			if ( isset($credit_note_numbers) ) {
       				$gutschriftnummer = implode(', ', $credit_note_numbers);
       			}
   
       	global $woocommerce, $product;
       	$bestellte_produkte ="";
   
       	//$zutaten_meta = "";
   
       	$order = new WC_Order( $order->id );
   
       	$items = $order->get_items();
   
       		foreach ($items as $item ) {
   
       			if ( !empty($item['variation_id']) ) {          // Check for a product variation
       									$id = $item['variation_id'];            // Assign variation ID
       							} else {
       									$id = $item['product_id'];              // Else use base ID
       							}
   
       		$product = new WC_Product($id);	
   
       		//$zutaten_meta = wc_get_order_item_meta($item_id, $key, $single); 
   
       		//$sku = $productmeta->post->sku;
       		$sku = $product->get_sku();
       		$product_name = $item['name'];
       		$product_qty =  $item['qty'];
   
       			//echo $id;
   
       		if ( $product_qty > '1') {
       				$bestellte_produkte .= $product_qty;
       				$bestellte_produkte .= "x ";
       				}
   
       		$bestellte_produkte .= $sku;
       			//echo $product_name;
       			//echo $product_qty;
   
       		}
       ```
   
 *  Plugin Author [Saravana Kumar K](https://wordpress.org/support/users/mycholan/)
 * (@mycholan)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/using-fields-in-csv-order-export/#post-7039622)
 * Hi, you are almost there. you could use `wc_get_order_item_meta()` function to
   get any order items’s meta. Do some thing like this
 * Feel free to modify as you need.
 *     ```
       function wc_csv_export_modify_column_headers( $column_headers ) {
       	$new_headers = array(
       			'order_item_meta' => 'Order Item Meta'
       	);
       	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 ) {
   
       	$item_metas = array();
       	$order = new WC_Order( $order->id );
       	$items = $order->get_items();	
   
       	foreach ( $items as $item ) {
       		$item_metas[] = wc_get_order_item_meta( $item_id, "your_wccpf_fields_name" );
       	}
   
       	$custom_data = array(
       		'order_item_meta' => get_post_meta( $order->id, 'Item Meta', implode( "; ", $item_metas ) )
       	);
   
       	return array_merge( $order_data, $custom_data );
       }
       add_filter( 'wc_customer_order_csv_export_order_row', 'wc_csv_export_modify_row_data', 10, 2 );
       ```
   
 *  Thread Starter [fitbox](https://wordpress.org/support/users/fitmealat/)
 * (@fitmealat)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/using-fields-in-csv-order-export/#post-7039654)
 * hm its not working all i see in the excel is “array”
 * i think its because $item_id is not right. Where is it coming from?
 * `$item_metas[] = wc_get_order_item_meta( $item_id, "your_wccpf_fields_name" );`
 * here is what worked after trial and error:
 *     ```
       foreach ($items as $item ) {
       $item_metas = "";
       $order = new WC_Order( $order->id );
       	$items = $order->get_items();
       $item_metas .= implode(" -", $item['item_meta']['your_wccpf_fields_LABEL']);
       }...
       ```
   
 * **BUT you have to use the FIELD LABEL!**
 * thanks for the help anyways. maybe i did something wrong 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using Fields in CSV Order Export’ is closed to new replies.

 * ![](https://ps.w.org/wc-fields-factory/assets/icon-128x128.jpg?rev=2738843)
 * [WC Fields Factory](https://wordpress.org/plugins/wc-fields-factory/)
 * [Support Threads](https://wordpress.org/support/plugin/wc-fields-factory/)
 * [Active Topics](https://wordpress.org/support/plugin/wc-fields-factory/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wc-fields-factory/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wc-fields-factory/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [fitbox](https://wordpress.org/support/users/fitmealat/)
 * Last activity: [10 years, 3 months ago](https://wordpress.org/support/topic/using-fields-in-csv-order-export/#post-7039654)
 * Status: not resolved