Automattic\WooCommerce\Internal\CLI\Migrator\Core

ProductsController::execute_migration_loopprivateWC 1.0

Execute the main cursor-based migration loop.

Method of the class: ProductsController{}

Hooks from the method

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->execute_migration_loop( $fetcher, $mapper, $progress ): void;
$fetcher(object) (required)
The platform fetcher instance.
$mapper(object) (required)
The platform mapper instance.
$progress(object) (required)
The WP_CLI progress bar instance.

ProductsController::execute_migration_loop() code WC 10.7.0

private function execute_migration_loop( $fetcher, $mapper, $progress ): void {
	$limit_remaining            = $this->parsed_args['limit'];
	$session_cursor             = $this->parsed_args['dry_run'] ? null : $this->session->get_reentrancy_cursor();
	$after_cursor               = ! empty( $session_cursor ) ? $session_cursor : null;
	$has_next_page              = true;
	$total_processed_in_session = 0;

	do {
		$batch_limit = min( $this->parsed_args['batch_size'], $limit_remaining );
		if ( $batch_limit <= 0 ) {
			break;
		}

		$batch_args = array(
			'limit'        => $batch_limit,
			'after_cursor' => $after_cursor,
		);

		if ( ! empty( $this->parsed_args['filters'] ) ) {
			$batch_args = array_merge( $batch_args, $this->parsed_args['filters'] );
		}

		try {
			$batch_data = $fetcher->fetch_batch( $batch_args );
		} catch ( Exception $e ) {
			/**
			 * Fires when an error occurs during migration.
			 *
			 * @since 10.3.0
			 *
			 * @param string $error_type The type of error (fetch, mapping, import).
			 * @param string $message    The error message.
			 * @param array  $context    Additional error context.
			 */
			do_action(
				'wc_migrator_error_occurred',
				'fetch',
				$e->getMessage(),
				array(
					'batch_args' => $batch_args,
					'platform'   => $this->parsed_args['platform'],
				)
			);

			WP_CLI::warning( "Error fetching batch: {$e->getMessage()}" );
			break;
		}

		if ( empty( $batch_data['items'] ) ) {
			break;
		}

		$processed_count = $this->process_batch( $batch_data['items'], $mapper );

		$total_processed_in_session += $processed_count;

		if ( ! $this->parsed_args['dry_run'] ) {
			$this->session->bump_imported_entities_counts( array( 'post' => $processed_count ) );
			$after_cursor = $batch_data['cursor'];
			$this->session->set_reentrancy_cursor( $after_cursor );
		} else {
			$after_cursor = $batch_data['cursor'];
		}

		$limit_remaining -= count( $batch_data['items'] );
		$has_next_page    = $batch_data['has_next_page'] ?? false;

		$progress->tick( $processed_count, sprintf( 'Processed %d products', $total_processed_in_session ) );
	} while ( $has_next_page && $limit_remaining > 0 );

	if ( ! $has_next_page && ! $this->parsed_args['dry_run'] ) {
		$this->session->set_stage( ImportSession::STAGE_FINISHED );
	}
}