Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_order_data_for_ids()protectedWC 1.0

Return order data for multiple IDs.

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

Хуков нет.

Возвращает

\stdClass[]. DB Order objects or error.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_order_data_for_ids( $ids );
$ids(массив) (обязательный)
List of order IDs.

Код OrdersTableDataStore::get_order_data_for_ids() WC 8.6.1

protected function get_order_data_for_ids( $ids ) {
	global $wpdb;

	if ( ! $ids || empty( $ids ) ) {
		return array();
	}

	$table_aliases     = array(
		'orders'           => $this->get_order_table_alias(),
		'billing_address'  => $this->get_address_table_alias( 'billing' ),
		'shipping_address' => $this->get_address_table_alias( 'shipping' ),
		'operational_data' => $this->get_op_table_alias(),
	);
	$order_table_alias = $table_aliases['orders'];
	$order_table_query = $this->get_order_table_select_statement();
	$id_placeholder    = implode( ', ', array_fill( 0, count( $ids ), '%d' ) );
	$order_meta_table  = self::get_meta_table_name();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_table_query is autogenerated and should already be prepared.
	$table_data = $wpdb->get_results(
		$wpdb->prepare(
			"$order_table_query WHERE $order_table_alias.id in ( $id_placeholder )",
			$ids
		)
	);
	// phpcs:enable

	$meta_data_query = $this->get_order_meta_select_statement();
	$order_data      = array();
	$meta_data       = $wpdb->get_results(
		$wpdb->prepare(
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $meta_data_query and $order_meta_table is autogenerated and should already be prepared. $id_placeholder is already prepared.
			"$meta_data_query WHERE $order_meta_table.order_id in ( $id_placeholder )",
			$ids
		)
	);

	foreach ( $table_data as $table_datum ) {
		$id                = $table_datum->{"{$order_table_alias}_id"};
		$order_data[ $id ] = new \stdClass();
		foreach ( $this->get_all_order_column_mappings() as $table_name => $column_mappings ) {
			$table_alias = $table_aliases[ $table_name ];
			// This remapping is required to keep the query length small enough to be supported by implementations such as HyperDB (i.e. fetching some tables in join via alias.*, while others via full name). We can revert this commit if HyperDB starts supporting SRTM for query length more than 3076 characters.
			foreach ( $column_mappings as $field => $map ) {
				$field_name = $map['name'] ?? "{$table_name}_$field";
				if ( property_exists( $table_datum, $field_name ) ) {
					$field_value = $table_datum->{ $field_name }; // Unique column, field name is different prop name.
				} elseif ( property_exists( $table_datum, "{$table_alias}_$field" ) ) {
					$field_value = $table_datum->{"{$table_alias}_$field"}; // Non-unique column (billing, shipping etc).
				} else {
					$field_value = $table_datum->{ $field }; // Unique column, field name is same as prop name.
				}
				$order_data[ $id ]->{$field_name} = $field_value;
			}
		}
		$order_data[ $id ]->id        = $id;
		$order_data[ $id ]->meta_data = array();
	}

	foreach ( $meta_data as $meta_datum ) {
		// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Not a meta query.
		$order_data[ $meta_datum->order_id ]->meta_data[] = (object) array(
			'meta_id'    => $meta_datum->id,
			'meta_key'   => $meta_datum->meta_key,
			'meta_value' => $meta_datum->meta_value,
		);
		// phpcs:enable
	}
	return $order_data;
}