WC_Order_Item_Meta

Order Item Meta.

Defined (1)

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

/includes/class-wc-order-item-meta.php  
  1. class WC_Order_Item_Meta { 
  2.  
  3. /** @var bool For handling backwards comp */ 
  4. private $legacy = false; 
  5.  
  6. /** @var Array Order item */ 
  7. private $item = null; 
  8.  
  9. /** @var Array Post meta data */ 
  10. public $meta = null; 
  11.  
  12. /** @var Product object */ 
  13. public $product = null; 
  14.  
  15. /** 
  16. * Constructor. 
  17. * @param array $item defaults to array() 
  18. * @param \WC_Product $product defaults to null 
  19. */ 
  20. public function __construct( $item = array(), $product = null ) { 
  21. // Backwards (pre 2.4) compat 
  22. if ( ! isset( $item['item_meta'] ) ) { 
  23. $this->legacy = true; 
  24. $this->meta = array_filter( (array) $item ); 
  25. return; 
  26. $this->item = $item; 
  27. $this->meta = array_filter( (array) $item['item_meta'] ); 
  28. $this->product = $product; 
  29.  
  30. /** 
  31. * Display meta in a formatted list. 
  32. * @param bool $flat (default: false) 
  33. * @param bool $return (default: false) 
  34. * @param string $hideprefix (default: _) 
  35. * @param string $delimiter Delimiter used to separate items when $flat is true 
  36. * @return string|void 
  37. */ 
  38. public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) { 
  39. $output = ''; 
  40. $formatted_meta = $this->get_formatted( $hideprefix ); 
  41.  
  42. if ( ! empty( $formatted_meta ) ) { 
  43. $meta_list = array(); 
  44.  
  45. foreach ( $formatted_meta as $meta ) { 
  46. if ( $flat ) { 
  47. $meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] ); 
  48. } else { 
  49. $meta_list[] = ' 
  50. <dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt> 
  51. <dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd> 
  52. '; 
  53.  
  54. if ( ! empty( $meta_list ) ) { 
  55. if ( $flat ) { 
  56. $output .= implode( $delimiter, $meta_list ); 
  57. } else { 
  58. $output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>'; 
  59.  
  60. $output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this ); 
  61.  
  62. if ( $return ) { 
  63. return $output; 
  64. } else { 
  65. echo $output; 
  66.  
  67. /** 
  68. * Return an array of formatted item meta in format e.g. 
  69. * array( 
  70. * 'pa_size' => array( 
  71. * 'label' => 'Size',  
  72. * 'value' => 'Medium',  
  73. * ) 
  74. * ) 
  75. * @since 2.4 
  76. * @param string $hideprefix exclude meta when key is prefixed with this, defaults to `_` 
  77. * @return array 
  78. */ 
  79. public function get_formatted( $hideprefix = '_' ) { 
  80. if ( $this->legacy ) { 
  81. return $this->get_formatted_legacy( $hideprefix ); 
  82.  
  83. $formatted_meta = array(); 
  84.  
  85. if ( ! empty( $this->item['item_meta_array'] ) ) { 
  86. foreach ( $this->item['item_meta_array'] as $meta_id => $meta ) { 
  87. if ( "" === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) { 
  88. continue; 
  89.  
  90. $attribute_key = urldecode( str_replace( 'attribute_', '', $meta->key ) ); 
  91. $meta_value = $meta->value; 
  92.  
  93. // If this is a term slug, get the term's nice name 
  94. if ( taxonomy_exists( $attribute_key ) ) { 
  95. $term = get_term_by( 'slug', $meta_value, $attribute_key ); 
  96.  
  97. if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { 
  98. $meta_value = $term->name; 
  99.  
  100. $formatted_meta[ $meta_id ] = array( 
  101. 'key' => $meta->key,  
  102. 'label' => wc_attribute_label( $attribute_key, $this->product ),  
  103. 'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),  
  104. ); 
  105.  
  106. return apply_filters( 'woocommerce_order_items_meta_get_formatted', $formatted_meta, $this ); 
  107.  
  108. /** 
  109. * Return an array of formatted item meta in format e.g. 
  110. * Handles @deprecated args. 
  111. * @return array 
  112. */ 
  113. public function get_formatted_legacy( $hideprefix = '_' ) { 
  114. if ( ! is_ajax() ) { 
  115. wc_deprecated_function( 'get_formatted_legacy', '2.4', 'Item Meta Data is being called with legacy arguments' ); 
  116.  
  117. $formatted_meta = array(); 
  118.  
  119. foreach ( $this->meta as $meta_key => $meta_values ) { 
  120. if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) == $hideprefix ) ) { 
  121. continue; 
  122. foreach ( (array) $meta_values as $meta_value ) { 
  123. // Skip serialised meta 
  124. if ( is_serialized( $meta_value ) ) { 
  125. continue; 
  126.  
  127. $attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) ); 
  128.  
  129. // If this is a term slug, get the term's nice name 
  130. if ( taxonomy_exists( $attribute_key ) ) { 
  131. $term = get_term_by( 'slug', $meta_value, $attribute_key ); 
  132. if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { 
  133. $meta_value = $term->name; 
  134.  
  135. // Unique key required 
  136. $formatted_meta_key = $meta_key; 
  137. $loop = 0; 
  138. while ( isset( $formatted_meta[ $formatted_meta_key ] ) ) { 
  139. $loop ++; 
  140. $formatted_meta_key = $meta_key . '-' . $loop; 
  141.  
  142. $formatted_meta[ $formatted_meta_key ] = array( 
  143. 'key' => $meta_key,  
  144. 'label' => wc_attribute_label( $attribute_key, $this->product ),  
  145. 'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),  
  146. ); 
  147.  
  148. return $formatted_meta;