Export bulk selected orders to CSV spreadsheet file

Adds bulk action “Download spreadsheet file” to the WooCommerce administrator orders list view. To use:

  1. Set your Screen Options > Number of orders per page to 100 or your desired export size.
  2. Filter the page so it shows most of the orders you want to process (status, etc).
  3. Click the “select all” checkbox then un-check any orders on the page you don’t want in the export.
  4. Select the Bulk actions drop-down choosing “Download spreadsheet file” then click Apply.

For more advanced functionality check out the official extension:
WooCommerce Customer / Order / Coupon Export

Description

add_filter( 'bulk_actions-edit-shop_order', function( $actions ) {
	$actions['csv_download'] = 'Download spreadsheet file';
	return $actions;
}, 20 );
add_filter( 'handle_bulk_actions-edit-shop_order', function( $redirect_to, $action, $post_ids ) {
	if( $action !== 'csv_download' ) { return $redirect_to; }
	$args = [
		'csv_download' => 1,
		'post_ids' => implode( ',', $post_ids ),
	];
	return add_query_arg( $args, $redirect_to );
}, 10, 3 );
add_action( 'admin_init', function() {
	if( empty( $_REQUEST['csv_download'] ) ) { return; }
	$header = [
		'Date', 'OrderId', 'Customer', 'SKU', 'Product', 'Quantity', 'Price'
	];
	$data = [ $header ];
	$post_ids = explode( ',', $_REQUEST['post_ids'] );
	foreach( $post_ids as $order_id ) {
		$order = wc_get_order( $order_id );
		$order_data = $order->get_data();
		$order_items = $order->get_items();
		foreach( $order_items as $order_item ) {
			$product = wc_get_product( $order_item->get_product_id() );
			$data[] = [
				$order->get_date_created()->format( 'm/d/Y' ),
				$order_id,
				$order_data['shipping']['first_name'] . ' ' . $order_data['shipping']['last_name'],
				$product->get_sku(),
				$order_item['name'],
				$order_item['qty'],
				$order_item['line_total'],
			];
		}
	}

	// Output
	header( 'Content-Type: text/csv; charset=utf-8' );
	header( 'Content-Disposition: attachment; filename=orders.csv' );
	$out = fopen( 'php://output', 'w' );
	foreach( $data as $row ) { fputcsv( $out, $row ); }
	fclose( $out );
	exit;
} );

How To Use

  1. Log into a staging or development clone of your site.
  2. Install and activate the Code Snippets plugin.
  3. Navigate to WP Admin > Snippets > Add New.
  4. Copy and paste the code from the Description tab above.
  5. Check to ensure formatting came over properly and no syntax errors show up in the editor.
  6. Customize the code as desired.
  7. Add a meaningful title and description.
  8. Select whether to run on front-end or back-end (or both). Some snippets require both.
  9. Click the “Save and Activate” button.
  10. Test your site to ensure it works as expected.
  11. Disable if any problems, or recover as necessary.
  12. Repeat steps two onward on your live environment.

Support

Using our Google contact form:

  1. Describe the issue and what you’ve observed.
  2. Describe your expected outcome(s).
  3. List steps to reproduce the issue.
  4. Optionally attach screen-shot images or a video.

Free support uses a WooCommerce sandbox site provided by the Developer. Issue must be reproducible there – independently of a User’s database, theme, and plugin configurations.

Need help with your WooCommerce site? We’re available – contact us today!

Credits

We code all of our code snippets directly. Our clients provide most of the ideas and demand for the functionality provided by our code snippets.

There are several sources one can find on Google for code that has inspired or contributed to this open-source library. Here’s some main ones:

License & Disclaimer

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Disclaimer of Warranty:

EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.