WC_Order_Item

Order Item.

Defined (1)

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

/includes/class-wc-order-item.php  
  1. class WC_Order_Item extends WC_Data implements ArrayAccess { 
  2.  
  3. /** 
  4. * Order Data array. This is the core order data exposed in APIs since 3.0.0. 
  5. * @since 3.0.0 
  6. * @var array 
  7. */ 
  8. protected $data = array( 
  9. 'order_id' => 0,  
  10. 'name' => '',  
  11. ); 
  12.  
  13. /** 
  14. * Stores meta in cache for future reads. 
  15. * A group must be set to to enable caching. 
  16. * @var string 
  17. */ 
  18. protected $cache_group = 'order-items'; 
  19.  
  20. /** 
  21. * Meta type. This should match up with 
  22. * the types avaiable at https://codex.wordpress.org/Function_Reference/add_metadata. 
  23. * WP defines 'post', 'user', 'comment', and 'term'. 
  24. */ 
  25. protected $meta_type = 'order_item'; 
  26.  
  27. /** 
  28. * This is the name of this object type. 
  29. * @var string 
  30. */ 
  31. protected $object_type = 'order_item'; 
  32.  
  33. /** 
  34. * Constructor. 
  35. * @param int|object|array $item ID to load from the DB, or WC_Order_Item Object 
  36. */ 
  37. public function __construct( $item = 0 ) { 
  38. parent::__construct( $item ); 
  39.  
  40. if ( $item instanceof WC_Order_Item ) { 
  41. $this->set_id( $item->get_id() ); 
  42. } elseif ( is_numeric( $item ) && $item > 0 ) { 
  43. $this->set_id( $item ); 
  44. } else { 
  45. $this->set_object_read( true ); 
  46.  
  47. $type = 'line_item' === $this->get_type() ? 'product' : $this->get_type(); 
  48. $this->data_store = WC_Data_Store::load( 'order-item-' . $type ); 
  49. if ( $this->get_id() > 0 ) { 
  50. $this->data_store->read( $this ); 
  51.  
  52. /** 
  53. |-------------------------------------------------------------------------- 
  54. | Getters 
  55. |-------------------------------------------------------------------------- 
  56. */ 
  57.  
  58. /** 
  59. * Get order ID this meta belongs to. 
  60. * @param string $context 
  61. * @return int 
  62. */ 
  63. public function get_order_id( $context = 'view' ) { 
  64. return $this->get_prop( 'order_id', $context ); 
  65.  
  66. /** 
  67. * Get order item name. 
  68. * @param string $context 
  69. * @return string 
  70. */ 
  71. public function get_name( $context = 'view' ) { 
  72. return $this->get_prop( 'name', $context ); 
  73.  
  74. /** 
  75. * Get order item type. Overridden by child classes. 
  76. * @return string 
  77. */ 
  78. public function get_type() { 
  79. return; 
  80.  
  81. /** 
  82. * Get quantity. 
  83. * @return int 
  84. */ 
  85. public function get_quantity() { 
  86. return 1; 
  87.  
  88. /** 
  89. * Get parent order object. 
  90. * @return int 
  91. */ 
  92. public function get_order() { 
  93. return wc_get_order( $this->get_order_id() ); 
  94.  
  95. /** 
  96. |-------------------------------------------------------------------------- 
  97. | Setters 
  98. |-------------------------------------------------------------------------- 
  99. */ 
  100.  
  101. /** 
  102. * Set order ID. 
  103. * @param int $value 
  104. * @throws WC_Data_Exception 
  105. */ 
  106. public function set_order_id( $value ) { 
  107. $this->set_prop( 'order_id', absint( $value ) ); 
  108.  
  109. /** 
  110. * Set order item name. 
  111. * @param string $value 
  112. * @throws WC_Data_Exception 
  113. */ 
  114. public function set_name( $value ) { 
  115. $this->set_prop( 'name', wc_clean( $value ) ); 
  116.  
  117. /** 
  118. |-------------------------------------------------------------------------- 
  119. | Other Methods 
  120. |-------------------------------------------------------------------------- 
  121. */ 
  122.  
  123. /** 
  124. * Type checking 
  125. * @param string|array $Type 
  126. * @return boolean 
  127. */ 
  128. public function is_type( $type ) { 
  129. return is_array( $type ) ? in_array( $this->get_type(), $type ) : $type === $this->get_type(); 
  130.  
  131. /** 
  132. |-------------------------------------------------------------------------- 
  133. | Meta Data Handling 
  134. |-------------------------------------------------------------------------- 
  135. */ 
  136.  
  137. /** 
  138. * Expands things like term slugs before return. 
  139. * @param string $hideprefix (default: _) 
  140. * @return array 
  141. */ 
  142. public function get_formatted_meta_data( $hideprefix = '_' ) { 
  143. $formatted_meta = array(); 
  144. $meta_data = $this->get_meta_data(); 
  145. $hideprefix_length = ! empty( $hideprefix ) ? strlen( $hideprefix ) : 0; 
  146. $product = is_callable( array( $this, 'get_product' ) ) ? $this->get_product() : false; 
  147. $order_item_name = $this->get_name(); 
  148.  
  149. foreach ( $meta_data as $meta ) { 
  150. if ( empty( $meta->id ) || "" === $meta->value || is_array( $meta->value ) || ( $hideprefix_length && substr( $meta->key, 0, $hideprefix_length ) === $hideprefix ) ) { 
  151. continue; 
  152.  
  153. $meta->key = rawurldecode( (string) $meta->key ); 
  154. $meta->value = rawurldecode( (string) $meta->value ); 
  155. $attribute_key = str_replace( 'attribute_', '', $meta->key ); 
  156. $display_key = wc_attribute_label( $attribute_key, $product ); 
  157. $display_value = $meta->value; 
  158.  
  159. if ( taxonomy_exists( $attribute_key ) ) { 
  160. $term = get_term_by( 'slug', $meta->value, $attribute_key ); 
  161. if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { 
  162. $display_value = $term->name; 
  163.  
  164. // Skip items with values already in the product details area of the product name 
  165. if ( $product && wc_is_attribute_in_product_name( $display_value, $order_item_name ) ) { 
  166. continue; 
  167.  
  168. $formatted_meta[ $meta->id ] = (object) array( 
  169. 'key' => $meta->key,  
  170. 'value' => $meta->value,  
  171. 'display_key' => apply_filters( 'woocommerce_order_item_display_meta_key', $display_key ),  
  172. 'display_value' => wpautop( make_clickable( apply_filters( 'woocommerce_order_item_display_meta_value', $display_value ) ) ),  
  173. ); 
  174.  
  175. return apply_filters( 'woocommerce_order_item_get_formatted_meta_data', $formatted_meta, $this ); 
  176.  
  177. /** 
  178. |-------------------------------------------------------------------------- 
  179. | Array Access Methods 
  180. |-------------------------------------------------------------------------- 
  181. | For backwards compat with legacy arrays. 
  182. */ 
  183.  
  184. /** 
  185. * offsetSet for ArrayAccess 
  186. * @param string $offset 
  187. * @param mixed $value 
  188. */ 
  189. public function offsetSet( $offset, $value ) { 
  190. if ( 'item_meta_array' === $offset ) { 
  191. foreach ( $value as $meta_id => $meta ) { 
  192. $this->update_meta_data( $meta->key, $meta->value, $meta_id ); 
  193. return; 
  194.  
  195. if ( array_key_exists( $offset, $this->data ) ) { 
  196. $setter = "set_$offset"; 
  197. if ( is_callable( array( $this, $setter ) ) ) { 
  198. $this->$setter( $value ); 
  199. return; 
  200.  
  201. $this->update_meta_data( $offset, $value ); 
  202.  
  203. /** 
  204. * offsetUnset for ArrayAccess 
  205. * @param string $offset 
  206. */ 
  207. public function offsetUnset( $offset ) { 
  208. $this->maybe_read_meta_data(); 
  209.  
  210. if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) { 
  211. $this->meta_data = array(); 
  212. return; 
  213.  
  214. if ( array_key_exists( $offset, $this->data ) ) { 
  215. unset( $this->data[ $offset ] ); 
  216.  
  217. if ( array_key_exists( $offset, $this->changes ) ) { 
  218. unset( $this->changes[ $offset ] ); 
  219.  
  220. $this->delete_meta_data( $offset ); 
  221.  
  222. /** 
  223. * offsetExists for ArrayAccess 
  224. * @param string $offset 
  225. * @return bool 
  226. */ 
  227. public function offsetExists( $offset ) { 
  228. $this->maybe_read_meta_data(); 
  229. if ( 'item_meta_array' === $offset || 'item_meta' === $offset || array_key_exists( $offset, $this->data ) ) { 
  230. return true; 
  231. return array_key_exists( $offset, wp_list_pluck( $this->meta_data, 'value', 'key' ) ) || array_key_exists( '_' . $offset, wp_list_pluck( $this->meta_data, 'value', 'key' ) ); 
  232.  
  233. /** 
  234. * offsetGet for ArrayAccess 
  235. * @param string $offset 
  236. * @return mixed 
  237. */ 
  238. public function offsetGet( $offset ) { 
  239. $this->maybe_read_meta_data(); 
  240.  
  241. if ( 'item_meta_array' === $offset ) { 
  242. $return = array(); 
  243.  
  244. foreach ( $this->meta_data as $meta ) { 
  245. $return[ $meta->key ] = $meta; 
  246.  
  247. return $return; 
  248.  
  249. $meta_values = wp_list_pluck( $this->meta_data, 'value', 'key' ); 
  250.  
  251. if ( 'item_meta' === $offset ) { 
  252. return $meta_values; 
  253. } elseif ( 'type' === $offset ) { 
  254. return $this->get_type(); 
  255. } elseif ( array_key_exists( $offset, $this->data ) ) { 
  256. $getter = "get_$offset"; 
  257. if ( is_callable( array( $this, $getter ) ) ) { 
  258. return $this->$getter(); 
  259. } elseif ( array_key_exists( '_' . $offset, $meta_values ) ) { 
  260. // Item meta was expanded in previous versions, with prefixes removed. This maintains support. 
  261. return $meta_values[ '_' . $offset ]; 
  262. } elseif ( array_key_exists( $offset, $meta_values ) ) { 
  263. return $meta_values[ $offset ]; 
  264.  
  265. return null;