Automattic\WooCommerce\Admin\API
AnalyticsImports::retry_failed_imports
Re-schedule imports for orders that previously failed.
Order IDs whose orders no longer exist are pruned (they can never import successfully). Orders with an import already pending are skipped and reported separately, so repeated requests don't claim to schedule new work. The remaining IDs stay recorded until their import succeeds, so a retry that fails again remains visible.
Method of the class: AnalyticsImports{}
No Hooks.
Returns
\WP_REST_Response|WP_Error. Response object on success, or WP_Error object on failure.
Usage
$AnalyticsImports = new AnalyticsImports(); $AnalyticsImports->retry_failed_imports( $request );
- $request(required)
- .
AnalyticsImports::retry_failed_imports() AnalyticsImports::retry failed imports code WC 11.0.1
public function retry_failed_imports( $request ) {
$failed = OrdersScheduler::get_failed_order_imports();
if ( empty( $failed['ids'] ) ) {
return new WP_Error(
'woocommerce_rest_analytics_no_failed_imports',
__( 'There are no failed order imports to retry.', 'woocommerce' ),
array( 'status' => 400 )
);
}
$retried_count = 0;
$pruned_count = 0;
$already_scheduled_count = 0;
$error_count = 0;
foreach ( $failed['ids'] as $order_id ) {
if ( ! wc_get_order( $order_id ) ) {
OrdersScheduler::clear_failed_order_import( $order_id );
++$pruned_count;
continue;
}
// schedule_action() silently no-ops when the same import is
// already pending, so check first to report an accurate count.
if ( OrdersScheduler::has_existing_jobs( 'import', array( $order_id ) ) ) {
++$already_scheduled_count;
continue;
}
try {
OrdersScheduler::schedule_action( 'import', array( $order_id ) );
++$retried_count;
} catch ( \Throwable $e ) {
// schedule_action() may run the import synchronously (e.g. when
// Action Scheduler is unavailable); a failing order must not
// abort the whole retry request.
++$error_count;
wc_get_logger()->error(
sprintf( 'Failed to schedule analytics re-import for order %d: %s', $order_id, $e->getMessage() ),
array( 'source' => 'wc-analytics-order-import' )
);
}
}
// Nothing was scheduled and nothing is pending: surface the failure
// instead of reporting success for work that didn't happen.
if ( 0 === $retried_count && 0 === $already_scheduled_count && $error_count > 0 ) {
return new WP_Error(
'woocommerce_rest_analytics_retry_failed',
__( 'The failed orders could not be scheduled for re-import. Check the order import log for details.', 'woocommerce' ),
array( 'status' => 500 )
);
}
if ( $retried_count > 0 ) {
$message = sprintf(
/* translators: %d: number of orders scheduled for re-import */
_n( 'Re-import scheduled for %d order.', 'Re-import scheduled for %d orders.', $retried_count, 'woocommerce' ),
$retried_count
);
} elseif ( $already_scheduled_count > 0 ) {
$message = __( 'Re-import is already scheduled for the previously failed orders.', 'woocommerce' );
} else {
$message = __( 'No orders were scheduled for re-import. The previously failed orders no longer exist.', 'woocommerce' );
}
if ( $error_count > 0 ) {
$message .= ' ' . sprintf(
/* translators: %d: number of orders that could not be scheduled for re-import */
_n( '%d order could not be scheduled. Check the order import log for details.', '%d orders could not be scheduled. Check the order import log for details.', $error_count, 'woocommerce' ),
$error_count
);
}
return rest_ensure_response(
array(
'success' => true,
'message' => $message,
'retried_count' => $retried_count,
'pruned_count' => $pruned_count,
'already_scheduled_count' => $already_scheduled_count,
'error_count' => $error_count,
)
);
}