WC_Order_Item_Tax

Order Line Item (tax).

Defined (1)

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

/includes/class-wc-order-item-tax.php  
  1. class WC_Order_Item_Tax 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. 'rate_code' => '',  
  10. 'rate_id' => 0,  
  11. 'label' => '',  
  12. 'compound' => false,  
  13. 'tax_total' => 0,  
  14. 'shipping_tax_total' => 0,  
  15. ); 
  16.  
  17. /** 
  18. |-------------------------------------------------------------------------- 
  19. | Setters 
  20. |-------------------------------------------------------------------------- 
  21. */ 
  22.  
  23. /** 
  24. * Set order item name. 
  25. * @param string $value 
  26. * @throws WC_Data_Exception 
  27. */ 
  28. public function set_name( $value ) { 
  29. $this->set_rate_code( $value ); 
  30.  
  31. /** 
  32. * Set item name. 
  33. * @param string $value 
  34. * @throws WC_Data_Exception 
  35. */ 
  36. public function set_rate_code( $value ) { 
  37. $this->set_prop( 'rate_code', wc_clean( $value ) ); 
  38.  
  39. /** 
  40. * Set item name. 
  41. * @param string $value 
  42. * @throws WC_Data_Exception 
  43. */ 
  44. public function set_label( $value ) { 
  45. $this->set_prop( 'label', wc_clean( $value ) ); 
  46.  
  47. /** 
  48. * Set tax rate id. 
  49. * @param int $value 
  50. * @throws WC_Data_Exception 
  51. */ 
  52. public function set_rate_id( $value ) { 
  53. $this->set_prop( 'rate_id', absint( $value ) ); 
  54.  
  55. /** 
  56. * Set tax total. 
  57. * @param string $value 
  58. * @throws WC_Data_Exception 
  59. */ 
  60. public function set_tax_total( $value ) { 
  61. $this->set_prop( 'tax_total', $value ? wc_format_decimal( $value ) : 0 ); 
  62.  
  63. /** 
  64. * Set shipping_tax_total 
  65. * @param string $value 
  66. * @throws WC_Data_Exception 
  67. */ 
  68. public function set_shipping_tax_total( $value ) { 
  69. $this->set_prop( 'shipping_tax_total', $value ? wc_format_decimal( $value ) : 0 ); 
  70.  
  71. /** 
  72. * Set compound 
  73. * @param bool $value 
  74. * @throws WC_Data_Exception 
  75. */ 
  76. public function set_compound( $value ) { 
  77. $this->set_prop( 'compound', (bool) $value ); 
  78.  
  79. /** 
  80. * Set properties based on passed in tax rate by ID. 
  81. * @param int $tax_rate_id 
  82. * @throws WC_Data_Exception 
  83. */ 
  84. public function set_rate( $tax_rate_id ) { 
  85. $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT ); 
  86.  
  87. $this->set_rate_id( $tax_rate_id ); 
  88. $this->set_rate_code( WC_Tax::get_rate_code( $tax_rate ) ); 
  89. $this->set_label( WC_Tax::get_rate_label( $tax_rate ) ); 
  90. $this->set_compound( WC_Tax::is_compound( $tax_rate ) ); 
  91.  
  92. /** 
  93. |-------------------------------------------------------------------------- 
  94. | Getters 
  95. |-------------------------------------------------------------------------- 
  96. */ 
  97.  
  98. /** 
  99. * Get order item type. 
  100. * @return string 
  101. */ 
  102. public function get_type() { 
  103. return 'tax'; 
  104.  
  105. /** 
  106. * Get rate code/name. 
  107. * @param string $context 
  108. * @return string 
  109. */ 
  110. public function get_name( $context = 'view' ) { 
  111. return $this->get_rate_code( $context ); 
  112.  
  113. /** 
  114. * Get rate code/name. 
  115. * @param string $context 
  116. * @return string 
  117. */ 
  118. public function get_rate_code( $context = 'view' ) { 
  119. return $this->get_prop( 'rate_code', $context ); 
  120.  
  121. /** 
  122. * Get label. 
  123. * @param string $context 
  124. * @return string 
  125. */ 
  126. public function get_label( $context = 'view' ) { 
  127. $label = $this->get_prop( 'label', $context ); 
  128. if ( 'view' === $context ) { 
  129. return $label ? $label : __( 'Tax', 'woocommerce' ); 
  130. } else { 
  131. return $label; 
  132.  
  133. /** 
  134. * Get tax rate ID. 
  135. * @param string $context 
  136. * @return int 
  137. */ 
  138. public function get_rate_id( $context = 'view' ) { 
  139. return $this->get_prop( 'rate_id', $context ); 
  140.  
  141. /** 
  142. * Get tax_total 
  143. * @param string $context 
  144. * @return string 
  145. */ 
  146. public function get_tax_total( $context = 'view' ) { 
  147. return $this->get_prop( 'tax_total', $context ); 
  148.  
  149. /** 
  150. * Get shipping_tax_total 
  151. * @param string $context 
  152. * @return string 
  153. */ 
  154. public function get_shipping_tax_total( $context = 'view' ) { 
  155. return $this->get_prop( 'shipping_tax_total', $context ); 
  156.  
  157. /** 
  158. * Get compound. 
  159. * @param string $context 
  160. * @return bool 
  161. */ 
  162. public function get_compound( $context = 'view' ) { 
  163. return $this->get_prop( 'compound', $context ); 
  164.  
  165. /** 
  166. * Is this a compound tax rate? 
  167. * @return boolean 
  168. */ 
  169. public function is_compound() { 
  170. return $this->get_compound(); 
  171.  
  172. /** 
  173. |-------------------------------------------------------------------------- 
  174. | Array Access Methods 
  175. |-------------------------------------------------------------------------- 
  176. | For backwards compat with legacy arrays. 
  177. */ 
  178.  
  179. /** 
  180. * offsetGet for ArrayAccess/Backwards compatibility. 
  181. * @deprecated Add deprecation notices in future release. 
  182. * @param string $offset 
  183. * @return mixed 
  184. */ 
  185. public function offsetGet( $offset ) { 
  186. if ( 'tax_amount' === $offset ) { 
  187. $offset = 'tax_total'; 
  188. } elseif ( 'shipping_tax_amount' === $offset ) { 
  189. $offset = 'shipping_tax_total'; 
  190. return parent::offsetGet( $offset ); 
  191.  
  192. /** 
  193. * offsetSet for ArrayAccess/Backwards compatibility. 
  194. * @deprecated Add deprecation notices in future release. 
  195. * @param string $offset 
  196. * @param mixed $value 
  197. */ 
  198. public function offsetSet( $offset, $value ) { 
  199. if ( 'tax_amount' === $offset ) { 
  200. $offset = 'tax_total'; 
  201. } elseif ( 'shipping_tax_amount' === $offset ) { 
  202. $offset = 'shipping_tax_total'; 
  203. parent::offsetSet( $offset, $value ); 
  204.  
  205. /** 
  206. * offsetExists for ArrayAccess 
  207. * @param string $offset 
  208. * @return bool 
  209. */ 
  210. public function offsetExists( $offset ) { 
  211. if ( in_array( $offset, array( 'tax_amount', 'shipping_tax_amount' ) ) ) { 
  212. return true; 
  213. return parent::offsetExists( $offset );