Exportar pedidos WooCommerce a fichero CSV

Exporta los pedidos realizados entre 2 fechas en WooCommerce y expórtalos a un fichero CSV.

$start = isset($_POST['tocsv_start']) ? $_POST['tocsv_start'] : '';
  $end   = isset($_POST['tocsv_end']) ? $_POST['tocsv_end'] : '';

  if ( strlen($start) > 0 ) {
      $args = array(
          'date_created' => $start . '...' . $end
      );
      $orders = wc_get_orders( $args );

      if ( $orders && (sizeof($orders) > 0) ) {
          $data = array();
          foreach ( $orders as $order ) {
              $order_data = array();
              $order_data[] = $order->get_order_number();


              $data[] = $order_data;
          }
          array_to_csv_download( $data );
      }
  }
  
 function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
	    header('Content-Type: application/csv');
	    header('Content-Disposition: attachment; filename="'.$filename.'";');
	    
	    // clean output buffer
	    ob_end_clean();
	    
	    $handle = fopen( 'php://output', 'w' );
	    
	    // use keys as column titles
	    // fputcsv( $handle, array_keys( $array['0'] ) );
	    
	    foreach ( $array as $value ) {
	        fputcsv( $handle, $value , $delimiter );
	    }
	    
	    fclose( $handle );
	    
	    // flush buffer
	    ob_flush();
	    
	    // use exit to get rid of unexpected output afterward
	    exit();
	}