wc_get_formatted_variation() │ WC 1.0

Variation Formatting.

Gets a formatted version of variation data or item meta.

No Hooks.

Returns

string.

Usage

wc_get_formatted_variation( $variation, $flat, $include_names, $skip_attributes_in_name );
$variation(array|WC_Product_Variation) (required)
Variation object.
$flat(true|false)
Should this be a flat list or HTML list? .
Default: false)
$include_names(true|false)
include attribute names/labels in the list.
Default: true
$skip_attributes_in_name(true|false)
Do not list attributes already part of the variation name.
Default: false

wc_get_formatted_variation() code WC 11.3.0

function wc_get_formatted_variation( $variation, $flat = false, $include_names = true, $skip_attributes_in_name = false ) {
	$return = '';

	if ( is_a( $variation, 'WC_Product_Variation' ) ) {
		$variation_attributes = $variation->get_attributes();
		$product              = $variation;
		$variation_name       = $variation->get_name();
	} else {
		$product        = false;
		$variation_name = '';
		// Remove attribute_ prefix from names.
		$variation_attributes = array();
		if ( is_array( $variation ) ) {
			foreach ( $variation as $key => $value ) {
				$variation_attributes[ str_replace( 'attribute_', '', $key ) ] = $value;
			}
		}
	}

	$list_type = $include_names ? 'dl' : 'ul';

	if ( is_array( $variation_attributes ) && ! empty( $variation_attributes ) ) {

		if ( ! $flat ) {
			$return = '<' . $list_type . ' class="variation">';
		}

		// Performance note: prefetch parent taxonomy terms to avoid per-attribute get_term_by queries.
		$taxonomy_terms = array();
		$parent_id      = $product instanceof WC_Product_Variation ? $product->get_parent_id() : 0;
		if ( $parent_id ) {
			foreach ( array_filter( array_keys( $variation_attributes ), 'taxonomy_exists' ) as $taxonomy ) {
				$terms = get_the_terms( $parent_id, $taxonomy );
				if ( is_array( $terms ) ) {
					foreach ( array_filter( $terms, static fn( $term ) => $term instanceof \WP_Term ) as $term ) { // @phpstan-ignore instanceof.alwaysTrue (defensive checks agains get_the_terms filter)
						$taxonomy_terms[ $taxonomy ][ $term->slug ] = $term;
					}
				}
			}
		}

		$variation_list = array();

		foreach ( $variation_attributes as $name => $value ) {
			// If this is a term slug, get the term's nice name.
			if ( taxonomy_exists( $name ) ) {
				$term = $taxonomy_terms[ $name ][ $value ] ?? get_term_by( 'slug', $value, $name );
				if ( ! is_wp_error( $term ) && $term && null !== $term->name && '' !== $term->name ) {
					$value = $term->name;
				}
			}

			// Do not list attributes already part of the variation name.
			if ( '' === $value || ( $skip_attributes_in_name && wc_is_attribute_in_product_name( $value, $variation_name, $product ? $product : null ) ) ) {
				continue;
			}

			if ( $include_names ) {
				if ( $flat ) {
					$variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
				} else {
					$variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
				}
			} elseif ( $flat ) {
					$variation_list[] = rawurldecode( $value );
			} else {
				$variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
			}
		}

		if ( $flat ) {
			$return .= implode( ', ', $variation_list );
		} else {
			$return .= implode( '', $variation_list );
		}

		if ( ! $flat ) {
			$return .= '</' . $list_type . '>';
		}
	}
	return $return;
}