Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable

CLIRunner::sync()publicWC 1.0

Sync order data between the custom order tables and the core WordPress post tables.

OPTIONS

[--batch-size=<batch-size>]
The number of orders to process in each batch.
--- default: 500
---

EXAMPLES

wp wc cot sync --batch-size=500

Method of the class: CLIRunner{}

No Hooks.

Return

null. Nothing (null).

Usage

$CLIRunner = new CLIRunner();
$CLIRunner->sync( $args, $assoc_args );
$args(array)
Positional arguments passed to the command.
Default: array()
$assoc_args(array)
Associative arguments (options) passed to the command.
Default: array()

CLIRunner::sync() code WC 8.6.1

public function sync( $args = array(), $assoc_args = array() ) {
	if ( ! $this->synchronizer->check_orders_table_exists() ) {
		WP_CLI::warning( __( 'Custom order tables does not exist, creating...', 'woocommerce' ) );
		$this->synchronizer->create_database_tables();
		if ( $this->synchronizer->check_orders_table_exists() ) {
			WP_CLI::success( __( 'Custom order tables were created successfully.', 'woocommerce' ) );
		} else {
			WP_CLI::error( __( 'Custom order tables could not be created.', 'woocommerce' ) );
		}
	}

	$order_count = $this->count_unmigrated();

	// Abort if there are no orders to migrate.
	if ( ! $order_count ) {
		return WP_CLI::warning( __( 'There are no orders to sync, aborting.', 'woocommerce' ) );
	}

	$assoc_args       = wp_parse_args(
		$assoc_args,
		array(
			'batch-size' => 500,
		)
	);
	$batch_size       = ( (int) $assoc_args['batch-size'] ) === 0 ? 500 : (int) $assoc_args['batch-size'];
	$progress         = WP_CLI\Utils\make_progress_bar( 'Order Data Sync', $order_count / $batch_size );
	$processed        = 0;
	$batch_count      = 1;
	$total_time       = 0;
	$orders_remaining = true;

	while ( $order_count > 0 || $orders_remaining ) {
		$remaining_count = $order_count;

		WP_CLI::debug(
			sprintf(
				/* Translators: %1$d is the batch number and %2$d is the batch size. */
				__( 'Beginning batch #%1$d (%2$d orders/batch).', 'woocommerce' ),
				$batch_count,
				$batch_size
			)
		);
		$batch_start_time = microtime( true );
		$order_ids        = $this->synchronizer->get_next_batch_to_process( $batch_size );
		if ( count( $order_ids ) ) {
			$this->synchronizer->process_batch( $order_ids );
		}
		$processed       += count( $order_ids );
		$batch_total_time = microtime( true ) - $batch_start_time;

		WP_CLI::debug(
			sprintf(
				// Translators: %1$d is the batch number, %2$d is the number of processed orders and %3$d is the execution time in seconds.
				__( 'Batch %1$d (%2$d orders) completed in %3$d seconds', 'woocommerce' ),
				$batch_count,
				count( $order_ids ),
				$batch_total_time
			)
		);

		$batch_count ++;
		$total_time += $batch_total_time;

		$progress->tick();

		$orders_remaining = count( $this->synchronizer->get_next_batch_to_process( 1 ) ) > 0;
		$order_count      = $remaining_count - $batch_size;
	}

	$progress->finish();

	// Issue a warning if no orders were migrated.
	if ( ! $processed ) {
		return WP_CLI::warning( __( 'No orders were synced.', 'woocommerce' ) );
	}

	WP_CLI::log( __( 'Sync completed.', 'woocommerce' ) );

	return WP_CLI::success(
		sprintf(
			/* Translators: %1$d is the number of migrated orders and %2$d is the execution time in seconds. */
			_n(
				'%1$d order was synced in %2$d seconds.',
				'%1$d orders were synced in %2$d seconds.',
				$processed,
				'woocommerce'
			),
			$processed,
			$total_time
		)
	);
}