wc_get_formatted_cart_item_data() │ WC 3.3.0

Gets and formats a list of cart item data + variations for display on the frontend.

Returns

string.

Usage

wc_get_formatted_cart_item_data( $cart_item, $flat, $product_name );
$cart_item(array) (required)
Cart item object.
$flat(true|false)
Should the data be returned flat or in a list.
Default: false
$product_name(string|null)
Product name displayed for the cart item, used to avoid duplicating variation attributes.
Default: stored product name

Changelog

Since 3.3.0 Introduced.
Since 11.2.0 Added the $product_name parameter.

wc_get_formatted_cart_item_data() code WC 11.2.1

function wc_get_formatted_cart_item_data( $cart_item, $flat = false, $product_name = null ) {
	$item_data = array();

	// Variation values are shown only if they are not found in the title as of 3.0.
	// This is because variation titles display the attributes.
	if ( $cart_item['data']->is_type( ProductType::VARIATION ) && is_array( $cart_item['variation'] ) ) {
		$product_name = is_string( $product_name ) ? $product_name : $cart_item['data']->get_name();
		$product_name = wp_specialchars_decode( wp_strip_all_tags( $product_name ), ENT_QUOTES );

		foreach ( $cart_item['variation'] as $name => $value ) {
			if ( ! is_scalar( $value ) || '' === (string) $value ) {
				continue;
			}
			$value    = (string) $value;
			$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );

			if ( taxonomy_exists( $taxonomy ) ) {
				// If this is a term slug, get the term's nice name.
				$term = get_term_by( 'slug', $value, $taxonomy );
				if ( ! is_wp_error( $term ) && $term && $term->name ) {
					$value = $term->name;
				}
				$label = wc_attribute_label( $taxonomy );
			} else {
				// If this is a custom option slug, get the options name.
				$value = apply_filters( 'woocommerce_variation_option_name', $value, null, $taxonomy, $cart_item['data'] );
				$label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $cart_item['data'] );
			}

			// The option-name filter can return anything; skip values that cannot be rendered.
			if ( ! is_scalar( $value ) || '' === (string) $value ) {
				continue;
			}

			// Variation values can be stored URL-encoded, while variation titles are built
			// from decoded values by wc_get_formatted_variation(). Decode here so an encoded
			// value is matched against the title and displayed the same way the title shows it.
			$value = rawurldecode( (string) $value );

			// Check the display value against the title.
			if ( wc_is_attribute_in_product_name( wp_specialchars_decode( $value, ENT_QUOTES ), $product_name ) ) {
				continue;
			}

			$item_data[] = array(
				'key'   => $label,
				'value' => $value,
			);
		}
	}

	// Filter item data to allow 3rd parties to add more to the array.
	$item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $cart_item );

	if ( is_array( $item_data ) ) {
		// Format item data ready to display.
		foreach ( $item_data as $key => $data ) {
			if ( ! is_array( $data ) ) {
				unset( $item_data[ $key ] );
				continue;
			}
			// Set hidden to true to not display meta on cart.
			if ( ! empty( $data['hidden'] ) ) {
				unset( $item_data[ $key ] );
				continue;
			}
			$label   = ! empty( $data['key'] ) ? $data['key'] : ( $data['name'] ?? '' );
			$display = ! empty( $data['display'] ) ? $data['display'] : ( $data['value'] ?? '' );

			$label_is_renderable   = is_scalar( $label ) || ( is_object( $label ) && method_exists( $label, '__toString' ) );
			$display_is_renderable = is_scalar( $display ) || ( is_object( $display ) && method_exists( $display, '__toString' ) );

			if ( ! $label_is_renderable || ! $display_is_renderable ) {
				unset( $item_data[ $key ] );
				continue;
			}

			$item_data[ $key ]['key']     = (string) $label;
			$item_data[ $key ]['display'] = (string) $display;
		}

		// Output flat or in list format.
		if ( count( $item_data ) > 0 ) {
			ob_start();

			if ( $flat ) {
				foreach ( $item_data as $data ) {
					echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['display'] ) . "\n";
				}
			} else {
				wc_get_template( 'cart/cart-item-data.php', array( 'item_data' => $item_data ) );
			}

			return ob_get_clean();
		}
	}

	return '';
}