Automattic\WooCommerce\DataBase\Migrations\CustomOrderTable

CLIRunner::diff()publicWC 8.6.0

Displays differences for an order between the HPOS and post datastore.

OPTIONS

<order_id>
The ID of the order.
[--format=<format>]
Render output in a particular format.
--- default: table options:
  • table
  • csv
  • json
  • yaml

EXAMPLES

# Find differences between datastores for order 123.
$ wp wc hpos diff 123
# Find differences for order 123 and display as CSV.
$ wp wc hpos diff 123 --format=csv

Method of the class: CLIRunner{}

No Hooks.

Return

null. Nothing (null).

Usage

$CLIRunner = new CLIRunner();
$CLIRunner->diff( $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()

Changelog

Since 8.6.0 Introduced.

CLIRunner::diff() code WC 9.0.1

public function diff( array $args = array(), array $assoc_args = array() ) {
	$id = absint( $args[0] );

	try {
		$diff = wc_get_container()->get( LegacyDataHandler::class )->get_diff_for_order( $id );
	} catch ( \Exception $e ) {
		// translators: %1$d is an order ID, %2$s is an error message.
		WP_CLI::error( sprintf( __( 'An error occurred while computing a diff for order %1$d: %2$s', 'woocommerce' ), $id, $e->getMessage() ) );
	}

	if ( ! $diff ) {
		WP_CLI::success( __( 'No differences found.', 'woocommerce' ) );
		return;
	}

	// Format the diff array.
	$diff = array_map(
		function ( $key, $hpos_value, $cpt_value ) {
			// Format for dates.
			$hpos_value = is_a( $hpos_value, \WC_DateTime::class ) ? $hpos_value->format( DATE_ATOM ) : $hpos_value;
			$cpt_value  = is_a( $cpt_value, \WC_DateTime::class ) ? $cpt_value->format( DATE_ATOM ) : $cpt_value;

			// Format for NULL.
			$hpos_value = is_null( $hpos_value ) ? '' : $hpos_value;
			$cpt_value  = is_null( $cpt_value ) ? '' : $cpt_value;

			return array(
				'property' => $key,
				'hpos'     => $hpos_value,
				'post'     => $cpt_value,
			);
		},
		array_keys( $diff ),
		array_column( $diff, 0 ),
		array_column( $diff, 1 ),
	);

	WP_CLI::warning(
		// translators: %d is an order ID.
		sprintf( __( 'Differences found for order %d:', 'woocommerce' ), $id )
	);
	WP_CLI\Utils\format_items(
		$assoc_args['format'] ?? 'table',
		$diff,
		array( 'property', 'hpos', 'post' )
	);
}