Orders::generate_csv_file( array $formatted_data, string $filename = 'export.csv' )

Outputs the formatted data as a CSV file for download.


Parameters

$formatted_data

(array) (Required) The formatted data for CSV export.

$filename

(string) (Optional) The name of the file for download.

Default value: 'export.csv'


Top ↑

Return

(void)


Top ↑

Source

File: src/Tickets/Commerce/Admin_Tables/Orders.php

	public function generate_csv_file(
		array $formatted_data,
		string $filename = 'export.csv'
	) {
		$charset = get_option( 'blog_charset' );
		// Set headers to force download on the browser.
		header( "Content-Type: text/csv; charset=$charset" );
		header( 'Content-Disposition: attachment; filename="' . sanitize_file_name( $filename ) . '"' );

		// Open the PHP output stream to write the CSV content.
		$output = fopen(
			'php://output',
			'w'
		);

		// Iterate over each row and write to the PHP output stream.
		foreach ( $formatted_data as $row ) {
			fputcsv(
				$output,
				$row
			); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_fputcsv
		}

		// Close the output stream.
		fclose( $output );

		// Exit to prevent any additional output.
		exit;
	}

Top ↑

Changelog

Changelog
Version Description
5.8.1 Introduced.