wc_get_formatted_variation

Variation Formatting.

Description

(string) wc_get_formatted_variation( (array|WC_Product_Variation) $variation, (constant) $flat = false, (bool) $include_names = true ); 

Gets a formatted version of variation data or item meta.

Returns (string)

Parameters (3)

0. $variation (array|WC_Product_Variation)
The variation.
1. $flat — Optional. (constant) => false
(default: false)
2. $include_names — Optional. (bool) => true
Include attribute names/labels

Usage

  1. if ( !function_exists( 'wc_get_formatted_variation' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The variation. 
  5. $variation = null; 
  6.  
  7. // (default: false) 
  8. $flat = false; 
  9.  
  10. // include attribute names/labels 
  11. $include_names = true; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_get_formatted_variation($variation, $flat, $include_names); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-product-functions.php  
  1. function wc_get_formatted_variation( $variation, $flat = false, $include_names = true ) { 
  2. $return = ''; 
  3.  
  4. if ( is_a( $variation, 'WC_Product_Variation' ) ) { 
  5. $variation_attributes = $variation->get_attributes(); 
  6. $product = $variation; 
  7. } else { 
  8. $product = false; 
  9. // Remove attribute_ prefix from names. 
  10. $variation_attributes = array(); 
  11. if ( is_array( $variation ) ) { 
  12. foreach ( $variation as $key => $value ) { 
  13. $variation_attributes[ str_replace( 'attribute_', '', $key ) ] = $value; 
  14.  
  15. $list_type = $include_names ? 'dl' : 'ul'; 
  16.  
  17. if ( is_array( $variation_attributes ) ) { 
  18.  
  19. if ( ! $flat ) { 
  20. $return = '<' . $list_type . ' class="variation">'; 
  21.  
  22. $variation_list = array(); 
  23.  
  24. foreach ( $variation_attributes as $name => $value ) { 
  25. if ( ! $value ) { 
  26. continue; 
  27.  
  28. // If this is a term slug, get the term's nice name 
  29. if ( taxonomy_exists( $name ) ) { 
  30. $term = get_term_by( 'slug', $value, $name ); 
  31. if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) { 
  32. $value = $term->name; 
  33. } else { 
  34. $value = ucwords( str_replace( '-', ' ', $value ) ); 
  35.  
  36. if ( $include_names ) { 
  37. if ( $flat ) { 
  38. $variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value ); 
  39. } else { 
  40. $variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>'; 
  41. } else { 
  42. if ( $flat ) { 
  43. $variation_list[] = rawurldecode( $value ); 
  44. } else { 
  45. $variation_list[] = '<li>' . rawurldecode( $value ) . '</li>'; 
  46.  
  47. if ( $flat ) { 
  48. $return .= implode( ', ', $variation_list ); 
  49. } else { 
  50. $return .= implode( '', $variation_list ); 
  51.  
  52. if ( ! $flat ) { 
  53. $return .= '</' . $list_type . '>'; 
  54. return $return;