Order_Endpoint::format_order_item_name( string $text )

Formats the order item name by truncating it to a specified length.

If the text exceeds the maximum character length, it is truncated at the last space within the limit and an ellipsis is added at the end.


Parameters

$text

(string) (Required) The original order item name text.


Top ↑

Return

(string) The formatted order item name text.


Top ↑

Source

File: src/Tickets/Commerce/Gateways/PayPal/REST/Order_Endpoint.php

	public function format_order_item_name( string $text ): string {
		$max_character_length = 127;
		$ellipsis             = '...';
		$truncate_length      = $max_character_length - strlen( $ellipsis );

		if ( strlen( $text ) <= $max_character_length ) {
			return $text;
		}

		// Cut the text to the desired length
		$truncated_text = substr( $text, 0, $truncate_length );

		// Find the last space within the truncated text
		$last_space = strrpos( $truncated_text, ' ' );

		// Cut the text at the last space to avoid cutting in the middle of a word
		if ( $last_space !== false ) {
			$truncated_text = substr( $truncated_text, 0, $last_space );
		}

		// Add an ellipsis at the end
		$truncated_text .= $ellipsis;

		return $truncated_text;
	}

Top ↑

Changelog

Changelog
Version Description
5.6.5 Introduced.