Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_diff_meta_data_between_orders()privateWC 1.0

Helper function to compute diff between metadata of post and cot data for an order.

Also provides an option to sync the metadata as well, since we are already computing the diff.

{} Это метод класса: OrdersTableDataStore{}

Хуков нет.

Возвращает

Массив. Difference between post and COT meta data.

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

// private - только в коде основоного (родительского) класса
$result = $this->get_diff_meta_data_between_orders( $order1, $order2, $sync ): array;
$order1(\WC_Abstract_Order) (обязательный)
Order object read from posts.
$order2(\WC_Abstract_Order) (обязательный)
Order object read from COT.
$sync(true|false)
Whether to also sync the meta data.
По умолчанию: false

Код OrdersTableDataStore::get_diff_meta_data_between_orders() WC 7.1.0

private function get_diff_meta_data_between_orders( \WC_Abstract_Order &$order1, \WC_Abstract_Order $order2, $sync = false ): array {
	$order1_meta        = ArrayUtil::select( $order1->get_meta_data(), 'get_data', ArrayUtil::SELECT_BY_OBJECT_METHOD );
	$order2_meta        = ArrayUtil::select( $order2->get_meta_data(), 'get_data', ArrayUtil::SELECT_BY_OBJECT_METHOD );
	$order1_meta_by_key = ArrayUtil::select_as_assoc( $order1_meta, 'key', ArrayUtil::SELECT_BY_ARRAY_KEY );
	$order2_meta_by_key = ArrayUtil::select_as_assoc( $order2_meta, 'key', ArrayUtil::SELECT_BY_ARRAY_KEY );

	$diff = array();
	foreach ( $order1_meta_by_key as $key => $value ) {
		if ( in_array( $key, $this->internal_meta_keys, true ) ) {
			// These should have already been verified in the base data comparison.
			continue;
		}
		$order1_values = ArrayUtil::select( $value, 'value', ArrayUtil::SELECT_BY_ARRAY_KEY );
		if ( ! array_key_exists( $key, $order2_meta_by_key ) ) {
			$sync && $order1->delete_meta_data( $key );
			$diff[ $key ] = $order1_values;
			unset( $order2_meta_by_key[ $key ] );
			continue;
		}

		$order2_values = ArrayUtil::select( $order2_meta_by_key[ $key ], 'value', ArrayUtil::SELECT_BY_ARRAY_KEY );
		$new_diff      = ArrayUtil::deep_assoc_array_diff( $order1_values, $order2_values );
		if ( ! empty( $new_diff ) && $sync ) {
			if ( count( $order2_values ) > 1 ) {
				$sync && $order1->delete_meta_data( $key );
				foreach ( $order2_values as $post_order_value ) {
					$sync && $order1->add_meta_data( $key, $post_order_value, false );
				}
			} else {
				$sync && $order1->update_meta_data( $key, $order2_values[0] );
			}
			$diff[ $key ] = $new_diff;
			unset( $order2_meta_by_key[ $key ] );
		}
	}

	foreach ( $order2_meta_by_key as $key => $value ) {
		if ( array_key_exists( $key, $order1_meta_by_key ) || in_array( $key, $this->internal_meta_keys, true ) ) {
			continue;
		}
		$order2_values = ArrayUtil::select( $value, 'value', ArrayUtil::SELECT_BY_ARRAY_KEY );
		foreach ( $order2_values as $meta_value ) {
			$sync && $order1->add_meta_data( $key, $meta_value );
		}
		$diff[ $key ] = $order2_values;
	}
	return $diff;
}