class-wc-order-item-shipping.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Order Line Item (shipping)
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.0.0
  7. * @since 3.0.0
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Order item shipping class.
  12. */
  13. class WC_Order_Item_Shipping 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. 'method_title' => '',
  22. 'method_id' => '',
  23. 'instance_id' => '',
  24. 'total' => 0,
  25. 'total_tax' => 0,
  26. 'taxes' => array(
  27. 'total' => array(),
  28. ),
  29. );
  30. /**
  31. * Calculate item taxes.
  32. *
  33. * @since 3.2.0
  34. * @param array $calculate_tax_for Location data to get taxes for. Required.
  35. * @return bool True if taxes were calculated.
  36. */
  37. public function calculate_taxes( $calculate_tax_for = array() ) {
  38. if ( ! isset( $calculate_tax_for['country'], $calculate_tax_for['state'], $calculate_tax_for['postcode'], $calculate_tax_for['city'], $calculate_tax_for['tax_class'] ) ) {
  39. return false;
  40. }
  41. if ( wc_tax_enabled() ) {
  42. $tax_rates = WC_Tax::find_shipping_rates( $calculate_tax_for );
  43. $taxes = WC_Tax::calc_tax( $this->get_total(), $tax_rates, false );
  44. $this->set_taxes( array( 'total' => $taxes ) );
  45. } else {
  46. $this->set_taxes( false );
  47. }
  48. do_action( 'woocommerce_order_item_shipping_after_calculate_taxes', $this, $calculate_tax_for );
  49. return true;
  50. }
  51. /*
  52. |--------------------------------------------------------------------------
  53. | Setters
  54. |--------------------------------------------------------------------------
  55. */
  56. /**
  57. * Set order item name.
  58. *
  59. * @param string $value Value to set.
  60. * @throws WC_Data_Exception May throw exception if data is invalid.
  61. */
  62. public function set_name( $value ) {
  63. $this->set_method_title( $value );
  64. }
  65. /**
  66. * Set method title.
  67. *
  68. * @param string $value Value to set.
  69. * @throws WC_Data_Exception May throw exception if data is invalid.
  70. */
  71. public function set_method_title( $value ) {
  72. $this->set_prop( 'name', wc_clean( $value ) );
  73. $this->set_prop( 'method_title', wc_clean( $value ) );
  74. }
  75. /**
  76. * Set shipping method id.
  77. *
  78. * @param string $value Value to set.
  79. * @throws WC_Data_Exception May throw exception if data is invalid.
  80. */
  81. public function set_method_id( $value ) {
  82. $this->set_prop( 'method_id', wc_clean( $value ) );
  83. }
  84. /**
  85. * Set shipping instance id.
  86. *
  87. * @param string $value Value to set.
  88. * @throws WC_Data_Exception May throw exception if data is invalid.
  89. */
  90. public function set_instance_id( $value ) {
  91. $this->set_prop( 'instance_id', wc_clean( $value ) );
  92. }
  93. /**
  94. * Set total.
  95. *
  96. * @param string $value Value to set.
  97. * @throws WC_Data_Exception May throw exception if data is invalid.
  98. */
  99. public function set_total( $value ) {
  100. $this->set_prop( 'total', wc_format_decimal( $value ) );
  101. }
  102. /**
  103. * Set total tax.
  104. *
  105. * @param string $value Value to set.
  106. * @throws WC_Data_Exception May throw exception if data is invalid.
  107. */
  108. protected function set_total_tax( $value ) {
  109. $this->set_prop( 'total_tax', wc_format_decimal( $value ) );
  110. }
  111. /**
  112. * Set taxes.
  113. *
  114. * This is an array of tax ID keys with total amount values.
  115. *
  116. * @param array $raw_tax_data Value to set.
  117. * @throws WC_Data_Exception May throw exception if data is invalid.
  118. */
  119. public function set_taxes( $raw_tax_data ) {
  120. $raw_tax_data = maybe_unserialize( $raw_tax_data );
  121. $tax_data = array(
  122. 'total' => array(),
  123. );
  124. if ( isset( $raw_tax_data['total'] ) ) {
  125. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
  126. } elseif ( ! empty( $raw_tax_data ) && is_array( $raw_tax_data ) ) {
  127. // Older versions just used an array.
  128. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data );
  129. }
  130. $this->set_prop( 'taxes', $tax_data );
  131. $this->set_total_tax( array_sum( $tax_data['total'] ) );
  132. }
  133. /**
  134. * Set properties based on passed in shipping rate object.
  135. *
  136. * @param WC_Shipping_Rate $shipping_rate Shipping rate to set.
  137. */
  138. public function set_shipping_rate( $shipping_rate ) {
  139. $this->set_method_title( $shipping_rate->get_label() );
  140. $this->set_method_id( $shipping_rate->get_method_id() );
  141. $this->set_instance_id( $shipping_rate->get_instance_id() );
  142. $this->set_total( $shipping_rate->get_cost() );
  143. $this->set_taxes( $shipping_rate->get_taxes() );
  144. $this->set_meta_data( $shipping_rate->get_meta_data() );
  145. }
  146. /*
  147. |--------------------------------------------------------------------------
  148. | Getters
  149. |--------------------------------------------------------------------------
  150. */
  151. /**
  152. * Get order item type.
  153. *
  154. * @return string
  155. */
  156. public function get_type() {
  157. return 'shipping';
  158. }
  159. /**
  160. * Get order item name.
  161. *
  162. * @param string $context View or edit context.
  163. * @return string
  164. */
  165. public function get_name( $context = 'view' ) {
  166. return $this->get_method_title( $context );
  167. }
  168. /**
  169. * Get title.
  170. *
  171. * @param string $context View or edit context.
  172. * @return string
  173. */
  174. public function get_method_title( $context = 'view' ) {
  175. $method_title = $this->get_prop( 'method_title', $context );
  176. if ( 'view' === $context ) {
  177. return $method_title ? $method_title : __( 'Shipping', 'woocommerce' );
  178. } else {
  179. return $method_title;
  180. }
  181. }
  182. /**
  183. * Get method ID.
  184. *
  185. * @param string $context View or edit context.
  186. * @return string
  187. */
  188. public function get_method_id( $context = 'view' ) {
  189. return $this->get_prop( 'method_id', $context );
  190. }
  191. /**
  192. * Get instance ID.
  193. *
  194. * @param string $context View or edit context.
  195. * @return string
  196. */
  197. public function get_instance_id( $context = 'view' ) {
  198. return $this->get_prop( 'instance_id', $context );
  199. }
  200. /**
  201. * Get total cost.
  202. *
  203. * @param string $context View or edit context.
  204. * @return string
  205. */
  206. public function get_total( $context = 'view' ) {
  207. return $this->get_prop( 'total', $context );
  208. }
  209. /**
  210. * Get total tax.
  211. *
  212. * @param string $context View or edit context.
  213. * @return string
  214. */
  215. public function get_total_tax( $context = 'view' ) {
  216. return $this->get_prop( 'total_tax', $context );
  217. }
  218. /**
  219. * Get taxes.
  220. *
  221. * @param string $context View or edit context.
  222. * @return array
  223. */
  224. public function get_taxes( $context = 'view' ) {
  225. return $this->get_prop( 'taxes', $context );
  226. }
  227. /**
  228. * Get tax class.
  229. *
  230. * @param string $context View or edit context.
  231. * @return string
  232. */
  233. public function get_tax_class( $context = 'view' ) {
  234. return get_option( 'woocommerce_shipping_tax_class' );
  235. }
  236. /*
  237. |--------------------------------------------------------------------------
  238. | Array Access Methods
  239. |--------------------------------------------------------------------------
  240. |
  241. | For backwards compatibility with legacy arrays.
  242. |
  243. */
  244. /**
  245. * Offset get: for ArrayAccess/Backwards compatibility.
  246. *
  247. * @deprecated Add deprecation notices in future release.
  248. * @param string $offset Key.
  249. * @return mixed
  250. */
  251. public function offsetGet( $offset ) {
  252. if ( 'cost' === $offset ) {
  253. $offset = 'total';
  254. }
  255. return parent::offsetGet( $offset );
  256. }
  257. /**
  258. * Offset set: for ArrayAccess/Backwards compatibility.
  259. *
  260. * @deprecated Add deprecation notices in future release.
  261. * @param string $offset Key.
  262. * @param mixed $value Value to set.
  263. */
  264. public function offsetSet( $offset, $value ) {
  265. if ( 'cost' === $offset ) {
  266. $offset = 'total';
  267. }
  268. parent::offsetSet( $offset, $value );
  269. }
  270. /**
  271. * Offset exists: for ArrayAccess.
  272. *
  273. * @param string $offset Key.
  274. * @return bool
  275. */
  276. public function offsetExists( $offset ) {
  277. if ( in_array( $offset, array( 'cost' ), true ) ) {
  278. return true;
  279. }
  280. return parent::offsetExists( $offset );
  281. }
  282. }