Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_order_data_for_ids_from_db()privateWC 1.0

Retrieve raw order data from the database for the given a set of IDs.

Method of the class: OrdersTableDataStore{}

No Hooks.

Return

\stdClass[]. Keyed array of objects containing raw order data keyed by the order IDs.

Usage

// private - for code of main (parent) class only
$result = $this->get_order_data_for_ids_from_db( $ids ): array;
$ids(int[]) (required)
List of order IDs.

OrdersTableDataStore::get_order_data_for_ids_from_db() code WC 9.6.0

private function get_order_data_for_ids_from_db( array $ids ): array {
	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' ) );

	// 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

	$order_data = array();
	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();
	}

	return $order_data;
}