Automattic\WooCommerce\Internal\Tax

TaxRateDataStore::get_rate_objects_for_ids │ public │ WC 11.0.0

Fetch multiple tax rate rows in a single query, keyed by tax_rate_id.

Method of the class: TaxRateDataStore{}

No Hooks.

Returns

array<int,object>.

Usage

$TaxRateDataStore = new TaxRateDataStore();
$TaxRateDataStore->get_rate_objects_for_ids( $ids ): array;
$ids(int[]) (required)
Tax rate IDs to fetch.

Changelog

Since 11.0.0 Introduced.

TaxRateDataStore::get_rate_objects_for_ids() code WC 11.1.2

public function get_rate_objects_for_ids( array $ids ): array {
	global $wpdb;

	$ids          = array_filter( array_map( 'absint', array_unique( $ids ) ) );
	$uncached_ids = array_diff( $ids, array_keys( $this->rate_objects_cache ) );
	if ( ! empty( $uncached_ids ) ) {
		$list = implode( ', ', $uncached_ids );
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$rows = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id IN ( $list )" );
		foreach ( $rows as $row ) {
			$this->rate_objects_cache[ (int) $row->tax_rate_id ] = $row;
		}
	}

	$result = array();
	foreach ( $ids as $id ) {
		if ( isset( $this->rate_objects_cache[ $id ] ) ) {
			$result[ $id ] = $this->rate_objects_cache[ $id ];
		}
	}

	return $result;
}