Nessuna descrizione

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. 'rate_percent' => null,
  28. );
  29. /*
  30. |--------------------------------------------------------------------------
  31. | Setters
  32. |--------------------------------------------------------------------------
  33. */
  34. /**
  35. * Set order item name.
  36. *
  37. * @param string $value Name.
  38. */
  39. public function set_name( $value ) {
  40. $this->set_rate_code( $value );
  41. }
  42. /**
  43. * Set item name.
  44. *
  45. * @param string $value Rate code.
  46. */
  47. public function set_rate_code( $value ) {
  48. $this->set_prop( 'rate_code', wc_clean( $value ) );
  49. }
  50. /**
  51. * Set item name.
  52. *
  53. * @param string $value Label.
  54. */
  55. public function set_label( $value ) {
  56. $this->set_prop( 'label', wc_clean( $value ) );
  57. }
  58. /**
  59. * Set tax rate id.
  60. *
  61. * @param int $value Rate ID.
  62. */
  63. public function set_rate_id( $value ) {
  64. $this->set_prop( 'rate_id', absint( $value ) );
  65. }
  66. /**
  67. * Set tax total.
  68. *
  69. * @param string $value Tax total.
  70. */
  71. public function set_tax_total( $value ) {
  72. $this->set_prop( 'tax_total', $value ? wc_format_decimal( $value ) : 0 );
  73. }
  74. /**
  75. * Set shipping tax total.
  76. *
  77. * @param string $value Shipping tax total.
  78. */
  79. public function set_shipping_tax_total( $value ) {
  80. $this->set_prop( 'shipping_tax_total', $value ? wc_format_decimal( $value ) : 0 );
  81. }
  82. /**
  83. * Set compound.
  84. *
  85. * @param bool $value If tax is compound.
  86. */
  87. public function set_compound( $value ) {
  88. $this->set_prop( 'compound', (bool) $value );
  89. }
  90. /**
  91. * Set rate value.
  92. *
  93. * @param float $value tax rate value.
  94. */
  95. public function set_rate_percent( $value ) {
  96. $this->set_prop( 'rate_percent', (float) $value );
  97. }
  98. /**
  99. * Set properties based on passed in tax rate by ID.
  100. *
  101. * @param int $tax_rate_id Tax rate ID.
  102. */
  103. public function set_rate( $tax_rate_id ) {
  104. $tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT );
  105. $this->set_rate_id( $tax_rate_id );
  106. $this->set_rate_code( WC_Tax::get_rate_code( $tax_rate ) );
  107. $this->set_label( WC_Tax::get_rate_label( $tax_rate ) );
  108. $this->set_compound( WC_Tax::is_compound( $tax_rate ) );
  109. $this->set_rate_percent( WC_Tax::get_rate_percent_value( $tax_rate ) );
  110. }
  111. /*
  112. |--------------------------------------------------------------------------
  113. | Getters
  114. |--------------------------------------------------------------------------
  115. */
  116. /**
  117. * Get order item type.
  118. *
  119. * @return string
  120. */
  121. public function get_type() {
  122. return 'tax';
  123. }
  124. /**
  125. * Get rate code/name.
  126. *
  127. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  128. * @return string
  129. */
  130. public function get_name( $context = 'view' ) {
  131. return $this->get_rate_code( $context );
  132. }
  133. /**
  134. * Get rate code/name.
  135. *
  136. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  137. * @return string
  138. */
  139. public function get_rate_code( $context = 'view' ) {
  140. return $this->get_prop( 'rate_code', $context );
  141. }
  142. /**
  143. * Get label.
  144. *
  145. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  146. * @return string
  147. */
  148. public function get_label( $context = 'view' ) {
  149. $label = $this->get_prop( 'label', $context );
  150. if ( 'view' === $context ) {
  151. return $label ? $label : __( 'Tax', 'woocommerce' );
  152. } else {
  153. return $label;
  154. }
  155. }
  156. /**
  157. * Get tax rate ID.
  158. *
  159. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  160. * @return int
  161. */
  162. public function get_rate_id( $context = 'view' ) {
  163. return $this->get_prop( 'rate_id', $context );
  164. }
  165. /**
  166. * Get tax_total
  167. *
  168. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  169. * @return string
  170. */
  171. public function get_tax_total( $context = 'view' ) {
  172. return $this->get_prop( 'tax_total', $context );
  173. }
  174. /**
  175. * Get shipping_tax_total
  176. *
  177. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  178. * @return string
  179. */
  180. public function get_shipping_tax_total( $context = 'view' ) {
  181. return $this->get_prop( 'shipping_tax_total', $context );
  182. }
  183. /**
  184. * Get compound.
  185. *
  186. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  187. * @return bool
  188. */
  189. public function get_compound( $context = 'view' ) {
  190. return $this->get_prop( 'compound', $context );
  191. }
  192. /**
  193. * Is this a compound tax rate?
  194. *
  195. * @return boolean
  196. */
  197. public function is_compound() {
  198. return $this->get_compound();
  199. }
  200. /**
  201. * Get rate value
  202. *
  203. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  204. * @return float
  205. */
  206. public function get_rate_percent( $context = 'view' ) {
  207. return $this->get_prop( 'rate_percent', $context );
  208. }
  209. /*
  210. |--------------------------------------------------------------------------
  211. | Array Access Methods
  212. |--------------------------------------------------------------------------
  213. |
  214. | For backwards compatibility with legacy arrays.
  215. |
  216. */
  217. /**
  218. * O for ArrayAccess/Backwards compatibility.
  219. *
  220. * @param string $offset Offset.
  221. * @return mixed
  222. */
  223. public function offsetGet( $offset ) {
  224. if ( 'tax_amount' === $offset ) {
  225. $offset = 'tax_total';
  226. } elseif ( 'shipping_tax_amount' === $offset ) {
  227. $offset = 'shipping_tax_total';
  228. }
  229. return parent::offsetGet( $offset );
  230. }
  231. /**
  232. * OffsetSet for ArrayAccess/Backwards compatibility.
  233. *
  234. * @deprecated 4.4.0
  235. * @param string $offset Offset.
  236. * @param mixed $value Value.
  237. */
  238. public function offsetSet( $offset, $value ) {
  239. wc_deprecated_function( 'WC_Order_Item_Tax::offsetSet', '4.4.0', '' );
  240. if ( 'tax_amount' === $offset ) {
  241. $offset = 'tax_total';
  242. } elseif ( 'shipping_tax_amount' === $offset ) {
  243. $offset = 'shipping_tax_total';
  244. }
  245. parent::offsetSet( $offset, $value );
  246. }
  247. /**
  248. * OffsetExists for ArrayAccess.
  249. *
  250. * @param string $offset Offset.
  251. * @return bool
  252. */
  253. public function offsetExists( $offset ) {
  254. if ( in_array( $offset, array( 'tax_amount', 'shipping_tax_amount' ), true ) ) {
  255. return true;
  256. }
  257. return parent::offsetExists( $offset );
  258. }
  259. }