class-wc-order-item-fee.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. /**
  3. * Order Line Item (fee)
  4. *
  5. * Fee is an amount of money charged for a particular piece of work
  6. * or for a particular right or service, and not supposed to be negative.
  7. *
  8. * @package WooCommerce/Classes
  9. * @version 3.0.0
  10. * @since 3.0.0
  11. */
  12. defined( 'ABSPATH' ) || exit;
  13. /**
  14. * Order item fee.
  15. */
  16. class WC_Order_Item_Fee extends WC_Order_Item {
  17. /**
  18. * Order Data array. This is the core order data exposed in APIs since 3.0.0.
  19. *
  20. * @since 3.0.0
  21. * @var array
  22. */
  23. protected $extra_data = array(
  24. 'tax_class' => '',
  25. 'tax_status' => 'taxable',
  26. 'amount' => '',
  27. 'total' => '',
  28. 'total_tax' => '',
  29. 'taxes' => array(
  30. 'total' => array(),
  31. ),
  32. );
  33. /**
  34. * Get item costs grouped by tax class.
  35. *
  36. * @since 3.2.0
  37. * @param WC_Order $order Order object.
  38. * @return array
  39. */
  40. protected function get_tax_class_costs( $order ) {
  41. $order_item_tax_classes = $order->get_items_tax_classes();
  42. $costs = array_fill_keys( $order_item_tax_classes, 0 );
  43. $costs['non-taxable'] = 0;
  44. foreach ( $order->get_items( array( 'line_item', 'fee', 'shipping' ) ) as $item ) {
  45. if ( 0 > $item->get_total() ) {
  46. continue;
  47. }
  48. if ( 'taxable' !== $item->get_tax_status() ) {
  49. $costs['non-taxable'] += $item->get_total();
  50. } elseif ( 'inherit' === $item->get_tax_class() ) {
  51. $inherit_class = reset( $order_item_tax_classes );
  52. $costs[ $inherit_class ] += $item->get_total();
  53. } else {
  54. $costs[ $item->get_tax_class() ] += $item->get_total();
  55. }
  56. }
  57. return array_filter( $costs );
  58. }
  59. /**
  60. * Calculate item taxes.
  61. *
  62. * @since 3.2.0
  63. * @param array $calculate_tax_for Location data to get taxes for. Required.
  64. * @return bool True if taxes were calculated.
  65. */
  66. public function calculate_taxes( $calculate_tax_for = array() ) {
  67. if ( ! isset( $calculate_tax_for['country'], $calculate_tax_for['state'], $calculate_tax_for['postcode'], $calculate_tax_for['city'] ) ) {
  68. return false;
  69. }
  70. // Use regular calculation unless the fee is negative.
  71. if ( 0 <= $this->get_total() ) {
  72. return parent::calculate_taxes( $calculate_tax_for );
  73. }
  74. if ( wc_tax_enabled() && $this->get_order() ) {
  75. // Apportion taxes to order items, shipping, and fees.
  76. $order = $this->get_order();
  77. $tax_class_costs = $this->get_tax_class_costs( $order );
  78. $total_costs = array_sum( $tax_class_costs );
  79. $discount_taxes = array();
  80. if ( $total_costs ) {
  81. foreach ( $tax_class_costs as $tax_class => $tax_class_cost ) {
  82. if ( 'non-taxable' === $tax_class ) {
  83. continue;
  84. }
  85. $proportion = $tax_class_cost / $total_costs;
  86. $cart_discount_proportion = $this->get_total() * $proportion;
  87. $calculate_tax_for['tax_class'] = $tax_class;
  88. $tax_rates = WC_Tax::find_rates( $calculate_tax_for );
  89. $discount_taxes = wc_array_merge_recursive_numeric( $discount_taxes, WC_Tax::calc_tax( $cart_discount_proportion, $tax_rates ) );
  90. }
  91. }
  92. $this->set_taxes( array( 'total' => $discount_taxes ) );
  93. } else {
  94. $this->set_taxes( false );
  95. }
  96. do_action( 'woocommerce_order_item_fee_after_calculate_taxes', $this, $calculate_tax_for );
  97. return true;
  98. }
  99. /*
  100. |--------------------------------------------------------------------------
  101. | Setters
  102. |--------------------------------------------------------------------------
  103. */
  104. /**
  105. * Set fee amount.
  106. *
  107. * @param string $value Amount.
  108. */
  109. public function set_amount( $value ) {
  110. $this->set_prop( 'amount', wc_format_decimal( $value ) );
  111. }
  112. /**
  113. * Set tax class.
  114. *
  115. * @param string $value Tax class.
  116. */
  117. public function set_tax_class( $value ) {
  118. if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs(), true ) ) {
  119. $this->error( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
  120. }
  121. $this->set_prop( 'tax_class', $value );
  122. }
  123. /**
  124. * Set tax_status.
  125. *
  126. * @param string $value Tax status.
  127. */
  128. public function set_tax_status( $value ) {
  129. if ( in_array( $value, array( 'taxable', 'none' ), true ) ) {
  130. $this->set_prop( 'tax_status', $value );
  131. } else {
  132. $this->set_prop( 'tax_status', 'taxable' );
  133. }
  134. }
  135. /**
  136. * Set total.
  137. *
  138. * @param string $amount Fee amount (do not enter negative amounts).
  139. */
  140. public function set_total( $amount ) {
  141. $this->set_prop( 'total', wc_format_decimal( $amount ) );
  142. }
  143. /**
  144. * Set total tax.
  145. *
  146. * @param string $amount Amount.
  147. */
  148. public function set_total_tax( $amount ) {
  149. $this->set_prop( 'total_tax', wc_format_decimal( $amount ) );
  150. }
  151. /**
  152. * Set taxes.
  153. *
  154. * This is an array of tax ID keys with total amount values.
  155. *
  156. * @param array $raw_tax_data Raw tax data.
  157. */
  158. public function set_taxes( $raw_tax_data ) {
  159. $raw_tax_data = maybe_unserialize( $raw_tax_data );
  160. $tax_data = array(
  161. 'total' => array(),
  162. );
  163. if ( ! empty( $raw_tax_data['total'] ) ) {
  164. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
  165. }
  166. $this->set_prop( 'taxes', $tax_data );
  167. $this->set_total_tax( array_sum( $tax_data['total'] ) );
  168. }
  169. /*
  170. |--------------------------------------------------------------------------
  171. | Getters
  172. |--------------------------------------------------------------------------
  173. */
  174. /**
  175. * Get fee amount.
  176. *
  177. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  178. * @return string
  179. */
  180. public function get_amount( $context = 'view' ) {
  181. return $this->get_prop( 'amount', $context );
  182. }
  183. /**
  184. * Get order item name.
  185. *
  186. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  187. * @return string
  188. */
  189. public function get_name( $context = 'view' ) {
  190. $name = $this->get_prop( 'name', $context );
  191. if ( 'view' === $context ) {
  192. return $name ? $name : __( 'Fee', 'woocommerce' );
  193. } else {
  194. return $name;
  195. }
  196. }
  197. /**
  198. * Get order item type.
  199. *
  200. * @return string
  201. */
  202. public function get_type() {
  203. return 'fee';
  204. }
  205. /**
  206. * Get tax class.
  207. *
  208. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  209. * @return string
  210. */
  211. public function get_tax_class( $context = 'view' ) {
  212. return $this->get_prop( 'tax_class', $context );
  213. }
  214. /**
  215. * Get tax status.
  216. *
  217. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  218. * @return string
  219. */
  220. public function get_tax_status( $context = 'view' ) {
  221. return $this->get_prop( 'tax_status', $context );
  222. }
  223. /**
  224. * Get total fee.
  225. *
  226. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  227. * @return string
  228. */
  229. public function get_total( $context = 'view' ) {
  230. return $this->get_prop( 'total', $context );
  231. }
  232. /**
  233. * Get total tax.
  234. *
  235. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  236. * @return string
  237. */
  238. public function get_total_tax( $context = 'view' ) {
  239. return $this->get_prop( 'total_tax', $context );
  240. }
  241. /**
  242. * Get fee taxes.
  243. *
  244. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  245. * @return array
  246. */
  247. public function get_taxes( $context = 'view' ) {
  248. return $this->get_prop( 'taxes', $context );
  249. }
  250. /*
  251. |--------------------------------------------------------------------------
  252. | Array Access Methods
  253. |--------------------------------------------------------------------------
  254. |
  255. | For backwards compatibility with legacy arrays.
  256. |
  257. */
  258. /**
  259. * OffsetGet for ArrayAccess/Backwards compatibility.
  260. *
  261. * @deprecated Add deprecation notices in future release.
  262. * @param string $offset Offset.
  263. * @return mixed
  264. */
  265. public function offsetGet( $offset ) {
  266. if ( 'line_total' === $offset ) {
  267. $offset = 'total';
  268. } elseif ( 'line_tax' === $offset ) {
  269. $offset = 'total_tax';
  270. } elseif ( 'line_tax_data' === $offset ) {
  271. $offset = 'taxes';
  272. }
  273. return parent::offsetGet( $offset );
  274. }
  275. /**
  276. * OffsetSet for ArrayAccess/Backwards compatibility.
  277. *
  278. * @deprecated Add deprecation notices in future release.
  279. * @param string $offset Offset.
  280. * @param mixed $value Value.
  281. */
  282. public function offsetSet( $offset, $value ) {
  283. if ( 'line_total' === $offset ) {
  284. $offset = 'total';
  285. } elseif ( 'line_tax' === $offset ) {
  286. $offset = 'total_tax';
  287. } elseif ( 'line_tax_data' === $offset ) {
  288. $offset = 'taxes';
  289. }
  290. parent::offsetSet( $offset, $value );
  291. }
  292. /**
  293. * OffsetExists for ArrayAccess
  294. *
  295. * @param string $offset Offset.
  296. * @return bool
  297. */
  298. public function offsetExists( $offset ) {
  299. if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ), true ) ) {
  300. return true;
  301. }
  302. return parent::offsetExists( $offset );
  303. }
  304. }