Automattic\WooCommerce\Internal\DataStores\Orders

LegacyDataHandler::get_diff_for_orderpublicWC 8.6.0

Builds an array with properties and metadata for which HPOS and post record have different values. Given it's mostly informative nature, it doesn't perform any deep or recursive searches and operates only on top-level properties/metadata.

Method of the class: LegacyDataHandler{}

No Hooks.

Returns

Array. Array of [HPOS value, post value] keyed by property, for all properties where HPOS and post value differ.

Usage

$LegacyDataHandler = new LegacyDataHandler();
$LegacyDataHandler->get_diff_for_order( $order_id ): array;
$order_id(int) (required)
Order ID.

Changelog

Since 8.6.0 Introduced.

LegacyDataHandler::get_diff_for_order() code WC 10.7.0

public function get_diff_for_order( int $order_id ): array {
	$diff = array();

	$hpos_order = $this->get_order_from_datastore( $order_id, 'hpos' );
	$cpt_order  = $this->get_order_from_datastore( $order_id, 'posts' );

	if ( $hpos_order->get_type() !== $cpt_order->get_type() ) {
		$diff['type'] = array( $hpos_order->get_type(), $cpt_order->get_type() );
	}

	$hpos_meta = $this->order_meta_to_array( $hpos_order );
	$cpt_meta  = $this->order_meta_to_array( $cpt_order );

	// Consider only keys for which we actually have a corresponding HPOS column or are meta.
	$all_keys = array_unique(
		array_diff(
			array_merge(
				$this->get_order_base_props(),
				array_keys( $hpos_meta ),
				array_keys( $cpt_meta )
			),
			$this->data_synchronizer->get_ignored_order_props()
		)
	);

	foreach ( $all_keys as $key ) {
		$val1 = in_array( $key, $this->get_order_base_props(), true ) ? $hpos_order->{"get_$key"}() : ( $hpos_meta[ $key ] ?? null );
		$val2 = in_array( $key, $this->get_order_base_props(), true ) ? $cpt_order->{"get_$key"}() : ( $cpt_meta[ $key ] ?? null );

		// Workaround for https://github.com/woocommerce/woocommerce/issues/43126.
		if ( ! $val2 && in_array( $key, array( '_billing_address_index', '_shipping_address_index' ), true ) ) {
			$val2 = get_post_meta( $order_id, $key, true );
		}

		if ( $val1 != $val2 ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison,Universal.Operators.StrictComparisons.LooseNotEqual
			$diff[ $key ] = array( $val1, $val2 );
		}
	}

	return $diff;
}