Automattic\WooCommerce\Admin\API

AnalyticsImports::trigger_importpublicWC 1.0

Trigger a manual import.

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->trigger_import( $request );
$request(required)
.

AnalyticsImports::trigger_import() code WC 10.7.0

public function trigger_import( $request ) {
	$is_scheduled_mode = $this->is_scheduled_import_enabled();

	// Return error if in immediate mode.
	if ( ! $is_scheduled_mode ) {
		return new WP_Error(
			'woocommerce_rest_analytics_import_immediate_mode',
			__( 'Manual import is not available in immediate mode. Imports happen automatically.', 'woocommerce' ),
			array( 'status' => 400 )
		);
	}

	// Check if an import is already in progress or due to run soon.
	if ( $this->is_import_in_progress_or_due() ) {
		return new WP_Error(
			'woocommerce_rest_analytics_import_in_progress',
			__( 'A batch import is already in progress or scheduled to run soon. Please wait for it to complete before triggering a new import.', 'woocommerce' ),
			array( 'status' => 400 )
		);
	}

	// Trigger the batch import immediately by rescheduling the recurring processor.
	// This unschedules the current recurring action and reschedules it to run now.
	$action_hook = OrdersScheduler::get_action( OrdersScheduler::PROCESS_PENDING_ORDERS_BATCH_ACTION );
	if ( ! is_string( $action_hook ) ) {
		return new WP_Error(
			'woocommerce_rest_analytics_import_invalid_action',
			__( 'Invalid action hook for batch import.', 'woocommerce' ),
			array( 'status' => 500 )
		);
	}
	WC()->queue()->cancel_all( $action_hook, array(), (string) OrdersScheduler::$group );
	OrdersScheduler::schedule_recurring_batch_processor();

	return rest_ensure_response(
		array(
			'success' => true,
			'message' => __( 'Batch import triggered successfully.', 'woocommerce' ),
		)
	);
}