Automattic\WooCommerce\Internal\CLI\Migrator\Core

ProductsController::migrate_productspublicWC 1.0

Main entry point for migrating products.

Method of the class: ProductsController{}

Returns

null. Nothing (null).

Usage

$ProductsController = new ProductsController();
$ProductsController->migrate_products( $assoc_args, $platform ): void;
$assoc_args(array) (required)
Command-line arguments.
$platform(string)
Optional pre-resolved platform (to avoid duplicate resolution).
Default: ''

ProductsController::migrate_products() code WC 10.7.0

public function migrate_products( array $assoc_args, string $platform = '' ): void {
	$this->parsed_args = $this->parse_and_validate_args( $assoc_args, $platform );
	if ( empty( $this->parsed_args ) ) {
		return;
	}

	$this->session_start_time = time();

	if ( $this->parsed_args['dry_run'] ) {
		WP_CLI::line( WP_CLI::colorize( '%Y--- DRY RUN MODE ENABLED ---%n' ) );
		WP_CLI::line( 'No products will be created or modified. This is a simulation only.' );
		WP_CLI::line( '' );
	}

	if ( ! $this->parsed_args['dry_run'] ) {
		$this->session = $this->manage_session_lifecycle( $this->parsed_args );
		if ( ! $this->session ) {
			return;
		}

		/**
		 * Fires when a migration session starts.
		 *
		 * @since 10.3.0
		 *
		 * @param string $platform The platform being migrated from.
		 * @param array  $metadata Session metadata including session_id, filters, and fields.
		 */
		do_action(
			'wc_migrator_session_started',
			$this->parsed_args['platform'],
			array(
				'session_id' => $this->session->get_id(),
				'filters'    => $this->parsed_args['filters'],
				'fields'     => $this->fields_to_process,
				'is_dry_run' => $this->parsed_args['dry_run'],
				'resume'     => $this->parsed_args['resume'],
			)
		);
	}

	$fetcher = $this->platform_registry->get_fetcher( $this->parsed_args['platform'] );
	$mapper  = $this->platform_registry->get_mapper( $this->parsed_args['platform'], array( 'fields' => $this->fields_to_process ) );

	$total_count = $fetcher->fetch_total_count( $this->parsed_args['filters'] );

	if ( ! $this->parsed_args['dry_run'] ) {
		$existing_total = $this->session->count_all_total_entities();
		if ( 0 < $total_count && 0 === $existing_total ) {
			$this->session->bump_total_number_of_entities( array( 'post' => $total_count ) );
		}
	}

	WP_CLI::line( "Total entities found: {$total_count}" );
	$progress_label = $this->parsed_args['dry_run']
		? 'Simulating Products from ' . ucfirst( $this->parsed_args['platform'] )
		: 'Importing Products from ' . ucfirst( $this->parsed_args['platform'] );
	$progress       = \WP_CLI\Utils\make_progress_bar( $progress_label, $total_count );

	// Set initial progress - either show resumed progress or 1% for new sessions.
	$initial_tick = max( 1, (int) ceil( $total_count * 0.01 ) );

	if ( ! $this->parsed_args['dry_run'] ) {
		$already_imported = $this->session->count_all_imported_entities();
		if ( $already_imported > 0 ) {
			// Show actual resumed progress.
			$progress->tick( $already_imported );
		} else {
			// Show 1% for new sessions to indicate activity has started.
			$progress->tick( $initial_tick );
		}
	} else {
		// For dry runs, show initial 1% tick.
		$progress->tick( $initial_tick );
	}

	$this->configure_product_importer();

	$this->execute_migration_loop( $fetcher, $mapper, $progress );

	$progress->finish();

	$this->display_migration_summary();

	$this->display_feedback_survey();

	if ( ! $this->parsed_args['dry_run'] ) {
		$final_stats = array(
			'total_found'    => $total_count,
			'total_imported' => $this->session->count_all_imported_entities(),
		);
		/**
		 * Fires when a migration session completes.
		 *
		 * @since 10.3.0
		 *
		 * @param string $platform    The platform being migrated from.
		 * @param array  $final_stats Final migration statistics.
		 */
		do_action( 'wc_migrator_session_completed', $this->parsed_args['platform'], $final_stats );

		$this->log_session_time_metrics( $final_stats );
	}

	if ( $this->parsed_args['dry_run'] ) {
		WP_CLI::success( 'Dry-run completed successfully. No products were actually created or modified.' );
	} else {
		WP_CLI::success( 'Migration completed successfully.' );
	}
}