Respond with an error if there are no items to export.
This commit is contained in:
parent
1650794f2b
commit
916ea55ae9
|
@ -163,17 +163,24 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
|
|||
$user_id = get_current_user_id();
|
||||
$export_id = str_replace( '.', '', microtime( true ) );
|
||||
|
||||
WC_Admin_Report_Exporter::queue_report_export( $user_id, $export_id, $report_type, $report_args );
|
||||
WC_Admin_Report_Exporter::update_export_percentage_complete( $user_id, $report_type, $export_id, 0 );
|
||||
$total_rows = WC_Admin_Report_Exporter::queue_report_export( $user_id, $export_id, $report_type, $report_args );
|
||||
|
||||
// @todo: handle error case?
|
||||
$result = array(
|
||||
'status' => 'success',
|
||||
'message' => __( 'Your report file is being generated.', 'woocommerce-admin' ),
|
||||
if ( 0 === $total_rows ) {
|
||||
$response = rest_ensure_response(
|
||||
array(
|
||||
'status' => 'error',
|
||||
'message' => __( 'There is no data to export for the given request.', 'woocommerce-admin' ),
|
||||
)
|
||||
);
|
||||
|
||||
} else {
|
||||
$response = rest_ensure_response(
|
||||
array(
|
||||
'status' => 'success',
|
||||
'message' => __( 'Your report file is being generated.', 'woocommerce-admin' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Wrap the data in a response object.
|
||||
$response = rest_ensure_response( $result );
|
||||
// Include a link to the export status endpoint.
|
||||
$response->add_links(
|
||||
array(
|
||||
|
@ -183,6 +190,9 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
|
|||
)
|
||||
);
|
||||
|
||||
WC_Admin_Report_Exporter::update_export_percentage_complete( $user_id, $report_type, $export_id, 0 );
|
||||
}
|
||||
|
||||
$data = $this->prepare_response_for_collection( $response );
|
||||
|
||||
return rest_ensure_response( $data );
|
||||
|
|
|
@ -72,7 +72,7 @@ class WC_Admin_Report_Exporter {
|
|||
* @param string $export_id Unique ID for report (timestamp expected).
|
||||
* @param string $report_type Report type. E.g. 'customers'.
|
||||
* @param array $report_args Report parameters, passed to data query.
|
||||
* @return void
|
||||
* @return int Number of items to export.
|
||||
*/
|
||||
public static function queue_report_export( $user_id, $export_id, $report_type, $report_args = array() ) {
|
||||
$exporter = new WC_Admin_Report_CSV_Exporter( $report_type, $report_args );
|
||||
|
@ -99,6 +99,8 @@ class WC_Admin_Report_Exporter {
|
|||
self::QUEUE_GROUP
|
||||
);
|
||||
}
|
||||
|
||||
return $total_rows;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue