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

Метод класса: CLIRunner{}

No Hooks.

Return

null. Nothing.

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()

Code of CLIRunner::sync() WC 7.1.0

public function sync( $args = array(), $assoc_args = array() ) {
	$this->log_production_warning();
	if ( ! $this->is_enabled() ) {
		return;
	}

	$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;

	while ( $order_count > 0 ) {

		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();

		$remaining_count = $this->count_unmigrated( array(), array( 'log' => false ) );
		if ( $remaining_count === $order_count ) {
			return WP_CLI::error( __( 'Infinite loop detected, aborting.', 'woocommerce' ) );
		}
		$order_count = $remaining_count;
	}

	$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
		)
	);
}