Automattic\WooCommerce\Internal\CLI\Migrator\Core

ProductsController::process_batchprivateWC 1.0

Process a batch of items using the mapper and importer.

Method of the class: ProductsController{}

Returns

Int. Number of successfully processed items.

Usage

// private - for code of main (parent) class only
$result = $this->process_batch( $batch_items, $mapper ): int;
$batch_items(array) (required)
Array of source platform items.
$mapper(object) (required)
Platform mapper instance.

ProductsController::process_batch() code WC 10.7.0

private function process_batch( array $batch_items, $mapper ): int {
	$processed_count   = 0;
	$mapped_products   = array();
	$source_data_batch = array();

	foreach ( $batch_items as $item ) {
		try {
			// Extract the actual product node from GraphQL response structure.
			// Handle both object and array GraphQL shapes.
			if ( is_object( $item ) && isset( $item->node ) ) {
				$product_data = $item->node;
			} elseif ( is_array( $item ) && isset( $item['node'] ) ) {
				$product_data = $item['node'];
			} else {
				$product_data = $item;
			}

			$mapped_product = $mapper->map_product_data( $product_data );
			if ( ! empty( $mapped_product ) ) {
				$mapped_products[]   = $mapped_product;
				$source_data_batch[] = is_object( $product_data ) ? (array) $product_data : $product_data;
			}
		} 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',
				'mapping',
				$e->getMessage(),
				array(
					'product_data' => $product_data,
					'platform'     => $this->parsed_args['platform'],
				)
			);

			WP_CLI::warning( sprintf( 'Error mapping product: %s', $e->getMessage() ) );
			continue;
		}
	}

	if ( ! empty( $mapped_products ) ) {
		if ( $this->parsed_args['dry_run'] ) {
			$batch_results = $this->simulate_import_batch( $mapped_products );
		} else {
			$batch_results = $this->product_importer->import_batch( $mapped_products, $source_data_batch );
		}

		/**
		 * Fires when a batch has been processed during migration.
		 *
		 * @since 10.3.0
		 *
		 * @param array $batch_results   Results from the batch import.
		 * @param array $source_data     Source platform data for the batch.
		 * @param array $mapped_products Mapped WooCommerce data for the batch.
		 */
		do_action( 'wc_migrator_batch_processed', $batch_results, $source_data_batch, $mapped_products );

		$this->log_batch_results( $batch_results );
		$processed_count = $batch_results['stats']['successful'];

		if ( $processed_count > 0 && ! $this->parsed_args['dry_run'] ) {
			$current_count = get_option( 'wc_migrator_products_count', 0 );
			update_option( 'wc_migrator_products_count', $current_count + $processed_count );
		}
	}

	return $processed_count;
}