WC_Order_Item_Fee

Order Line Item (fee).

Defined (1)

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

/includes/class-wc-order-item-fee.php  
  1. class WC_Order_Item_Fee extends WC_Order_Item { 
  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 $extra_data = array( 
  9. 'tax_class' => '',  
  10. 'tax_status' => 'taxable',  
  11. 'total' => '',  
  12. 'total_tax' => '',  
  13. 'taxes' => array( 
  14. 'total' => array(),  
  15. ),  
  16. ); 
  17.  
  18. /** 
  19. |-------------------------------------------------------------------------- 
  20. | Setters 
  21. |-------------------------------------------------------------------------- 
  22. */ 
  23.  
  24. /** 
  25. * Set tax class. 
  26. * @param string $value 
  27. * @throws WC_Data_Exception 
  28. */ 
  29. public function set_tax_class( $value ) { 
  30. if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs() ) ) { 
  31. $this->error( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) ); 
  32. $this->set_prop( 'tax_class', $value ); 
  33.  
  34. /** 
  35. * Set tax_status. 
  36. * @param string $value 
  37. * @throws WC_Data_Exception 
  38. */ 
  39. public function set_tax_status( $value ) { 
  40. if ( in_array( $value, array( 'taxable', 'none' ) ) ) { 
  41. $this->set_prop( 'tax_status', $value ); 
  42. } else { 
  43. $this->set_prop( 'tax_status', 'taxable' ); 
  44.  
  45. /** 
  46. * Set total. 
  47. * @param string $amount Fee amount (do not enter negative amounts). 
  48. * @throws WC_Data_Exception 
  49. */ 
  50. public function set_total( $amount ) { 
  51. $this->set_prop( 'total', wc_format_decimal( $amount ) ); 
  52.  
  53. /** 
  54. * Set total tax. 
  55. * @param string $amount 
  56. * @throws WC_Data_Exception 
  57. */ 
  58. public function set_total_tax( $amount ) { 
  59. $this->set_prop( 'total_tax', wc_format_decimal( $amount ) ); 
  60.  
  61. /** 
  62. * Set taxes. 
  63. * This is an array of tax ID keys with total amount values. 
  64. * @param array $raw_tax_data 
  65. * @throws WC_Data_Exception 
  66. */ 
  67. public function set_taxes( $raw_tax_data ) { 
  68. $raw_tax_data = maybe_unserialize( $raw_tax_data ); 
  69. $tax_data = array( 
  70. 'total' => array(),  
  71. ); 
  72. if ( ! empty( $raw_tax_data['total'] ) ) { 
  73. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); 
  74. $this->set_prop( 'taxes', $tax_data ); 
  75. $this->set_total_tax( array_sum( $tax_data['total'] ) ); 
  76.  
  77. /** 
  78. |-------------------------------------------------------------------------- 
  79. | Getters 
  80. |-------------------------------------------------------------------------- 
  81. */ 
  82.  
  83. /** 
  84. * Get order item name. 
  85. * @param string $context 
  86. * @return string 
  87. */ 
  88. public function get_name( $context = 'view' ) { 
  89. $name = $this->get_prop( 'name', $context ); 
  90. if ( 'view' === $context ) { 
  91. return $name ? $name : __( 'Fee', 'woocommerce' ); 
  92. } else { 
  93. return $name; 
  94.  
  95. /** 
  96. * Get order item type. 
  97. * @return string 
  98. */ 
  99. public function get_type() { 
  100. return 'fee'; 
  101.  
  102. /** 
  103. * Get tax class. 
  104. * @param string $context 
  105. * @return string 
  106. */ 
  107. public function get_tax_class( $context = 'view' ) { 
  108. return $this->get_prop( 'tax_class', $context ); 
  109.  
  110. /** 
  111. * Get tax status. 
  112. * @param string $context 
  113. * @return string 
  114. */ 
  115. public function get_tax_status( $context = 'view' ) { 
  116. return $this->get_prop( 'tax_status', $context ); 
  117.  
  118. /** 
  119. * Get total fee. 
  120. * @param string $context 
  121. * @return string 
  122. */ 
  123. public function get_total( $context = 'view' ) { 
  124. return $this->get_prop( 'total', $context ); 
  125.  
  126. /** 
  127. * Get total tax. 
  128. * @param string $context 
  129. * @return string 
  130. */ 
  131. public function get_total_tax( $context = 'view' ) { 
  132. return $this->get_prop( 'total_tax', $context ); 
  133.  
  134. /** 
  135. * Get fee taxes. 
  136. * @param string $context 
  137. * @return array 
  138. */ 
  139. public function get_taxes( $context = 'view' ) { 
  140. return $this->get_prop( 'taxes', $context ); 
  141.  
  142. /** 
  143. |-------------------------------------------------------------------------- 
  144. | Array Access Methods 
  145. |-------------------------------------------------------------------------- 
  146. | For backwards compat with legacy arrays. 
  147. */ 
  148.  
  149. /** 
  150. * offsetGet for ArrayAccess/Backwards compatibility. 
  151. * @deprecated Add deprecation notices in future release. 
  152. * @param string $offset 
  153. * @return mixed 
  154. */ 
  155. public function offsetGet( $offset ) { 
  156. if ( 'line_total' === $offset ) { 
  157. $offset = 'total'; 
  158. } elseif ( 'line_tax' === $offset ) { 
  159. $offset = 'total_tax'; 
  160. } elseif ( 'line_tax_data' === $offset ) { 
  161. $offset = 'taxes'; 
  162. return parent::offsetGet( $offset ); 
  163.  
  164. /** 
  165. * offsetSet for ArrayAccess/Backwards compatibility. 
  166. * @deprecated Add deprecation notices in future release. 
  167. * @param string $offset 
  168. * @param mixed $value 
  169. */ 
  170. public function offsetSet( $offset, $value ) { 
  171. if ( 'line_total' === $offset ) { 
  172. $offset = 'total'; 
  173. } elseif ( 'line_tax' === $offset ) { 
  174. $offset = 'total_tax'; 
  175. } elseif ( 'line_tax_data' === $offset ) { 
  176. $offset = 'taxes'; 
  177. parent::offsetSet( $offset, $value ); 
  178.  
  179. /** 
  180. * offsetExists for ArrayAccess 
  181. * @param string $offset 
  182. * @return bool 
  183. */ 
  184. public function offsetExists( $offset ) { 
  185. if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ) ) ) { 
  186. return true; 
  187. return parent::offsetExists( $offset );