1 <?php
2 3 4 5 6 7 8 9 10 11
12 class WC_Order_Item_Meta {
13
14 public $meta;
15 public $product;
16
17 18 19 20 21 22 23
24 public function __construct( $item_meta = array(), $product = null ) {
25 $this->meta = $item_meta;
26 $this->product = $product;
27 }
28
29 30 31 32 33 34 35 36 37
38 public function display( $flat = false, $return = false, $hideprefix = '_' ) {
39
40 if ( ! empty( $this->meta ) ) {
41
42 $meta_list = array();
43
44 foreach ( $this->meta as $meta_key => $meta_values ) {
45
46 if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) == $hideprefix ) ) {
47 continue;
48 }
49
50 foreach( $meta_values as $meta_value ) {
51
52
53 if ( is_serialized( $meta_value ) ) {
54 continue;
55 }
56
57 $attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) );
58
59
60 if ( taxonomy_exists( $attribute_key ) ) {
61 $term = get_term_by( 'slug', $meta_value, $attribute_key );
62
63 if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) {
64 $meta_value = $term->name;
65 }
66
67
68 } elseif ( $this->product ) {
69 $product_attributes = $this->product->get_attributes();
70
71 if ( isset( $product_attributes[ $attribute_key ] ) ) {
72 $meta_key = wc_attribute_label( $product_attributes[ $attribute_key ]['name'] );
73 }
74 }
75
76 if ( $flat ) {
77 $meta_list[] = wp_kses_post( wc_attribute_label( $attribute_key ) . ': ' . apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) );
78 } else {
79 $meta_list[] = '
80 <dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta_key ) ) . '">' . wp_kses_post( wc_attribute_label( $attribute_key ) ) . ':</dt>
81 <dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta_key ) ) . '">' . wp_kses_post( wpautop( apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) ) ) . '</dd>
82 ';
83 }
84 }
85 }
86
87 if ( ! sizeof( $meta_list ) )
88 return '';
89
90 $output = $flat ? '' : '<dl class="variation">';
91
92 if ( $flat )
93 $output .= implode( ", \n", $meta_list );
94 else
95 $output .= implode( '', $meta_list );
96
97 if ( ! $flat )
98 $output .= '</dl>';
99
100 if ( $return )
101 return $output;
102 else
103 echo $output;
104 }
105
106 return '';
107 }
108 }