Automattic\WooCommerce\Internal\OrderReviews

ItemEligibility::unique_slot_items │ public static │ WC 11.3.0

Collapse line items to one per (product, variation) review slot.

Reviews are stored per product/variation, but an order can carry the same product on several line items (add-ons, gift cards, bookings). Keep the first line item for each slot so the page renders one row per review instead of duplicate rows that all resolve to the same comment.

Method of the class: ItemEligibility{}

No Hooks.

Returns

array<int, WC_Order_Item_Product>. One line item per review slot.

Usage

$result = ItemEligibility::unique_slot_items( $items ): array;
$items(array<int|string, mixed>) (required)
Order line items.

Changelog

Since 11.3.0 Introduced.

ItemEligibility::unique_slot_items() code WC 11.1.2

public static function unique_slot_items( array $items ): array {
	$seen   = array();
	$unique = array();
	foreach ( $items as $item ) {
		if ( ! $item instanceof WC_Order_Item_Product ) {
			continue;
		}
		$slot = $item->get_product_id() . '|' . $item->get_variation_id();
		if ( isset( $seen[ $slot ] ) ) {
			continue;
		}
		$seen[ $slot ] = true;
		$unique[]      = $item;
	}
	return $unique;
}