Automattic\WooCommerce\StoreApi\Routes\V1\Ucp

AbstractCatalogRoute::truncation_messages │ protected │ WC 1.0

Build messages entries for products whose variant list was cut short.

UCP has no variant pagination, so this message is the only disclosure of a shortened list — without it an agent reports the missing variants as nonexistent. path is the RFC 9535 JSONPath of the affected list.

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

Хуков нет.

Возвращает

array.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->truncation_messages( $truncations, $products, $path_prefix ): array;
$truncations(массив) (обязательный)
Keyed by product id, from UcpProductMapper::take_truncations().
$products(массив) (обязательный)
Mapped products, in response order.
$path_prefix(строка) (обязательный)
JSONPath of the products container.

Код AbstractCatalogRoute::truncation_messages() WC 11.3.0

protected function truncation_messages( array $truncations, array $products, string $path_prefix ): array {
	if ( empty( $truncations ) ) {
		return array();
	}

	$messages = array();

	foreach ( $products as $index => $product ) {
		$product_id = isset( $product['id'] ) ? (int) $product['id'] : 0;

		if ( ! isset( $truncations[ $product_id ] ) ) {
			continue;
		}

		$truncation = $truncations[ $product_id ];

		$messages[] = array(
			'type'         => 'info',
			'code'         => 'variants_truncated',
			'content'      => sprintf(
				/* translators: 1: number of variants returned, 2: total published variants, 3: product title. */
				__( 'Showing %1$d of %2$d variants for "%3$s". Request this product directly, or name a variant by id, to reach the others.', 'woocommerce' ),
				$truncation['returned'],
				$truncation['total'],
				$truncation['title']
			),
			'path'         => '$.product' === $path_prefix ? '$.product.variants' : sprintf( '%s[%d].variants', $path_prefix, $index ),
			'content_type' => 'plain',
		);
	}

	return $messages;
}