class-wc-order-item-tax.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Order Line Item (tax)
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.0.0
  7. * @since 3.0.0
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Order item tax.
  12. */
  13. class WC_Order_Item_Tax extends WC_Order_Item {
  14. /**
  15. * Order Data array. This is the core order data exposed in APIs since 3.0.0.
  16. *
  17. * @since 3.0.0
  18. * @var array
  19. */
  20. protected $extra_data = array(
  21. 'rate_code' => '',
  22. 'rate_id' => 0,
  23. 'label' => '',
  24. 'compound' => false,
  25. 'tax_total' => 0,
  26. 'shipping_tax_total' => 0,
  27. );
  28. /*
  29. |--------------------------------------------------------------------------
  30. | Setters
  31. |--------------------------------------------------------------------------
  32. */
  33. /**
  34. * Set order item name.
  35. *
  36. * @param string $value Name.
  37. */
  38. public function set_name( $value ) {
  39. $this->set_rate_code( $value );
  40. }
  41. /**
  42. * Set item name.
  43. *
  44. * @param string $value Rate code.
  45. */
  46. public function set_rate_code( $value ) {
  47. $this->set_prop( 'rate_code', wc_clean( $value ) );
  48. }
  49. /**
  50. * Set item name.
  51. *
  52. * @param string $value Label.
  53. */
  54. public function set_label( $value ) {
  55. $this->set_prop( 'label', wc_clean( $value ) );
  56. }
  57. /**
  58. * Set tax rate id.
  59. *
  60. * @param int $value Rate ID.
  61. */
  62. public function set_rate_id( $value ) {
  63. $this->set_prop( 'rate_id', absint( $value ) );
  64. }
  65. /**
  66. * Set tax total.
  67. *
  68. * @param string $value Tax total.
  69. */
  70. public function set_tax_total( $value ) {
  71. $this->set_prop( 'tax_total', $value ? wc_format_decimal( $value ) : 0 );
  72. }
  73. /**
  74. * Set shipping tax total.
  75. *
  76. * @param string $value Shipping tax total.
  77. */
  78. public function set_shipping_tax_total( $value ) {
  79. $this->set_prop( 'shipping_tax_total', $value ? wc_format_decimal( $value ) : 0 );
  80. }
  81. /**
  82. * Set compound.
  83. *
  84. * @param bool $value If tax is compound.
  85. */
  86. public function set_compound( $value ) {
  87. $this->set_prop( 'compound', (bool) $value );
  88. }
  89. /**
  90. * Set properties based on passed in tax rate by ID.
  91. *
  92. * @param int $tax_rate_id Tax rate ID.
  93. */
  94. public function set_rate( $tax_rate_id ) {
  95. $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT );
  96. $this->set_rate_id( $tax_rate_id );
  97. $this->set_rate_code( WC_Tax::get_rate_code( $tax_rate ) );
  98. $this->set_label( WC_Tax::get_rate_label( $tax_rate ) );
  99. $this->set_compound( WC_Tax::is_compound( $tax_rate ) );
  100. }
  101. /*
  102. |--------------------------------------------------------------------------
  103. | Getters
  104. |--------------------------------------------------------------------------
  105. */
  106. /**
  107. * Get order item type.
  108. *
  109. * @return string
  110. */
  111. public function get_type() {
  112. return 'tax';
  113. }
  114. /**
  115. * Get rate code/name.
  116. *
  117. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  118. * @return string
  119. */
  120. public function get_name( $context = 'view' ) {
  121. return $this->get_rate_code( $context );
  122. }
  123. /**
  124. * Get rate code/name.
  125. *
  126. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  127. * @return string
  128. */
  129. public function get_rate_code( $context = 'view' ) {
  130. return $this->get_prop( 'rate_code', $context );
  131. }
  132. /**
  133. * Get label.
  134. *
  135. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  136. * @return string
  137. */
  138. public function get_label( $context = 'view' ) {
  139. $label = $this->get_prop( 'label', $context );
  140. if ( 'view' === $context ) {
  141. return $label ? $label : __( 'Tax', 'woocommerce' );
  142. } else {
  143. return $label;
  144. }
  145. }
  146. /**
  147. * Get tax rate ID.
  148. *
  149. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  150. * @return int
  151. */
  152. public function get_rate_id( $context = 'view' ) {
  153. return $this->get_prop( 'rate_id', $context );
  154. }
  155. /**
  156. * Get tax_total
  157. *
  158. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  159. * @return string
  160. */
  161. public function get_tax_total( $context = 'view' ) {
  162. return $this->get_prop( 'tax_total', $context );
  163. }
  164. /**
  165. * Get shipping_tax_total
  166. *
  167. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  168. * @return string
  169. */
  170. public function get_shipping_tax_total( $context = 'view' ) {
  171. return $this->get_prop( 'shipping_tax_total', $context );
  172. }
  173. /**
  174. * Get compound.
  175. *
  176. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  177. * @return bool
  178. */
  179. public function get_compound( $context = 'view' ) {
  180. return $this->get_prop( 'compound', $context );
  181. }
  182. /**
  183. * Is this a compound tax rate?
  184. *
  185. * @return boolean
  186. */
  187. public function is_compound() {
  188. return $this->get_compound();
  189. }
  190. /*
  191. |--------------------------------------------------------------------------
  192. | Array Access Methods
  193. |--------------------------------------------------------------------------
  194. |
  195. | For backwards compatibility with legacy arrays.
  196. |
  197. */
  198. /**
  199. * O for ArrayAccess/Backwards compatibility.
  200. *
  201. * @deprecated Add deprecation notices in future release.
  202. * @param string $offset Offset.
  203. * @return mixed
  204. */
  205. public function offsetGet( $offset ) {
  206. if ( 'tax_amount' === $offset ) {
  207. $offset = 'tax_total';
  208. } elseif ( 'shipping_tax_amount' === $offset ) {
  209. $offset = 'shipping_tax_total';
  210. }
  211. return parent::offsetGet( $offset );
  212. }
  213. /**
  214. * OffsetSet for ArrayAccess/Backwards compatibility.
  215. *
  216. * @deprecated Add deprecation notices in future release.
  217. * @param string $offset Offset.
  218. * @param mixed $value Value.
  219. */
  220. public function offsetSet( $offset, $value ) {
  221. if ( 'tax_amount' === $offset ) {
  222. $offset = 'tax_total';
  223. } elseif ( 'shipping_tax_amount' === $offset ) {
  224. $offset = 'shipping_tax_total';
  225. }
  226. parent::offsetSet( $offset, $value );
  227. }
  228. /**
  229. * OffsetExists for ArrayAccess.
  230. *
  231. * @param string $offset Offset.
  232. * @return bool
  233. */
  234. public function offsetExists( $offset ) {
  235. if ( in_array( $offset, array( 'tax_amount', 'shipping_tax_amount' ), true ) ) {
  236. return true;
  237. }
  238. return parent::offsetExists( $offset );
  239. }
  240. }