Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_order_data_for_ids_from_cacheprivateWC 1.0

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

Method of the class: OrdersTableDataStore{}

No Hooks.

Returns

\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_cache( $ids ): array;
$ids(int[]) (required)
List of order IDs.

OrdersTableDataStore::get_order_data_for_ids_from_cache() code WC 11.0.1

private function get_order_data_for_ids_from_cache( array $ids ): array {
	$cache_engine = wc_get_container()->get( WPCacheEngine::class );
	$order_data   = $cache_engine->get_cached_objects( $ids, $this->get_cache_group() );

	foreach ( $order_data as $id => $datum ) {
		/*
		 * Cached order data is always a complete plain stdClass whose id matches the cache key
		 * and which carries every mapped column property (see get_order_data_for_ids_from_db()).
		 * Accept only that shape. Anything else - a scalar, an array, a foreign/incomplete
		 * object such as __PHP_Incomplete_Class, a record whose id is missing or belongs to a
		 * different order (cross-contamination), or a truncated record missing mapped columns -
		 * would fatal or silently hydrate the wrong/default order state downstream
		 * (read_multiple() dereferences $order_data->id and indexes $orders[$order_id];
		 * set_order_props_from_data() defaults any missing property). This can happen when a
		 * third-party persistent object cache returns a corrupt or cross-contaminated value, or
		 * when a stale entry written by an older version predates the complete-column-set fix.
		 * Invalidate the entry so it is re-read from the database, and surface it for diagnosis.
		 */
		if ( $this->is_valid_cached_order_data( $datum, (int) $id ) ) {
			continue;
		}

		if ( null !== $datum ) {
			$cache_engine->delete_cached_object( $id, $this->get_cache_group() );
			$this->error_logger->warning(
				sprintf(
					'Discarded a corrupt HPOS order cache entry for order %1$d (unexpected shape, mismatched id, or missing mapped columns); it will be re-read from the database.',
					(int) $id
				),
				array( 'source' => 'hpos-data-cache' )
			);
		}

		unset( $order_data[ $id ] );
	}

	return $order_data;
}