Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable

CLIRunner::backfill()publicWC 8.6.0

Backfills an order from either the HPOS or the posts datastore.

OPTIONS

<order_id>
The ID of the order.
--from=<datastore>
Source datastore. Either 'hpos' or 'posts'.
--- options:
  • hpos
  • posts

--to=<datastore>
Destination datastore. Either 'hpos' or 'posts'.
--- options:
  • hpos
  • posts

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

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$CLIRunner = new CLIRunner();
$CLIRunner->backfill( $args, $assoc_args );
$args(массив)
Positional arguments passed to the command.
По умолчанию: array()
$assoc_args(массив)
Associative arguments (options) passed to the command.
По умолчанию: array()

Список изменений

С версии 8.6.0 Введена.

Код CLIRunner::backfill() WC 8.7.0

public function backfill( array $args = array(), array $assoc_args = array() ) {
	$legacy_handler = wc_get_container()->get( LegacyDataHandler::class );

	$from     = $assoc_args['from'] ?? '';
	$to       = $assoc_args['to'] ?? '';
	$order_id = absint( $args[0] );

	if ( ! $order_id ) {
		WP_CLI::error( __( 'Please provide a valid order ID.', 'woocommerce' ) );
	}

	foreach ( array( 'from', 'to' ) as $datastore ) {
		if ( ! in_array( ${"$datastore"}, array( 'posts', 'hpos' ), true ) ) {
			// translators: %s is a shell argument representing a datastore name.
			WP_CLI::error( sprintf( __( '\'%s\' is not a valid datastore.', 'woocommerce' ), ${"$datastore"} ) );
		}
	}

	if ( $from === $to ) {
		WP_CLI::error( __( 'Please use different source (--from) and destination (--to) datastores.', 'woocommerce' ) );
	}

	try {
		$legacy_handler->backfill_order_to_datastore( $order_id, $from, $to );
	} catch ( \Exception $e ) {
		WP_CLI::error(
			sprintf(
				// translators: %1$d is an order ID, %2$s and %3$s are datastore names, %4$s is an error message.
				__( 'An error occurred while backfilling order %1$d from %2$s to %3$s: %4$s', 'woocommerce' ),
				$order_id,
				$from,
				$to,
				$e->getMessage()
			)
		);
	}

	WP_CLI::success(
		sprintf(
			// translators: %1$d is an order ID, %2$s and %3$s are datastore names ("hpos" or "posts" for example).
			__( 'Order %1$d backfilled from %2$s to %3$s.', 'woocommerce' ),
			$order_id,
			$from,
			$to
		)
	);
}