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->get_items() as $order_item ) {
$product_sku = '';
$product_id = $order_item->get_product_id();
if( $product_id ) {
$product = wc_get_product( $product_id );
$product_sku = $product->get_sku();
}
$data[] = [
$order->get_date_created()->format( 'm/d/Y' ),
$order_id,
$order_data['shipping']['first_name'] . ' ' . $order_data['shipping']['last_name'],
$product_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;
} );
Support
Using our Google upload form:
- Describe the issue and what you’ve observed.
- Describe your expected outcome(s).
- List steps to reproduce the issue.
- Optionally attach screen-shot images or a video.
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.