PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-order-item-shipping.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 319 lines | 123 code | 30 blank | 166 comment | 13 complexity | d43471934592d71e849af5dd7ce994e9 MD5 | raw file
  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. if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) {
  132. $this->set_total_tax( array_sum( $tax_data['total'] ) );
  133. } else {
  134. $this->set_total_tax( array_sum( array_map( 'wc_round_tax_total', $tax_data['total'] ) ) );
  135. }
  136. }
  137. /**
  138. * Set properties based on passed in shipping rate object.
  139. *
  140. * @param WC_Shipping_Rate $shipping_rate Shipping rate to set.
  141. */
  142. public function set_shipping_rate( $shipping_rate ) {
  143. $this->set_method_title( $shipping_rate->get_label() );
  144. $this->set_method_id( $shipping_rate->get_method_id() );
  145. $this->set_instance_id( $shipping_rate->get_instance_id() );
  146. $this->set_total( $shipping_rate->get_cost() );
  147. $this->set_taxes( $shipping_rate->get_taxes() );
  148. $this->set_meta_data( $shipping_rate->get_meta_data() );
  149. }
  150. /*
  151. |--------------------------------------------------------------------------
  152. | Getters
  153. |--------------------------------------------------------------------------
  154. */
  155. /**
  156. * Get order item type.
  157. *
  158. * @return string
  159. */
  160. public function get_type() {
  161. return 'shipping';
  162. }
  163. /**
  164. * Get order item name.
  165. *
  166. * @param string $context View or edit context.
  167. * @return string
  168. */
  169. public function get_name( $context = 'view' ) {
  170. return $this->get_method_title( $context );
  171. }
  172. /**
  173. * Get title.
  174. *
  175. * @param string $context View or edit context.
  176. * @return string
  177. */
  178. public function get_method_title( $context = 'view' ) {
  179. $method_title = $this->get_prop( 'method_title', $context );
  180. if ( 'view' === $context ) {
  181. return $method_title ? $method_title : __( 'Shipping', 'woocommerce' );
  182. } else {
  183. return $method_title;
  184. }
  185. }
  186. /**
  187. * Get method ID.
  188. *
  189. * @param string $context View or edit context.
  190. * @return string
  191. */
  192. public function get_method_id( $context = 'view' ) {
  193. return $this->get_prop( 'method_id', $context );
  194. }
  195. /**
  196. * Get instance ID.
  197. *
  198. * @param string $context View or edit context.
  199. * @return string
  200. */
  201. public function get_instance_id( $context = 'view' ) {
  202. return $this->get_prop( 'instance_id', $context );
  203. }
  204. /**
  205. * Get total cost.
  206. *
  207. * @param string $context View or edit context.
  208. * @return string
  209. */
  210. public function get_total( $context = 'view' ) {
  211. return $this->get_prop( 'total', $context );
  212. }
  213. /**
  214. * Get total tax.
  215. *
  216. * @param string $context View or edit context.
  217. * @return string
  218. */
  219. public function get_total_tax( $context = 'view' ) {
  220. return $this->get_prop( 'total_tax', $context );
  221. }
  222. /**
  223. * Get taxes.
  224. *
  225. * @param string $context View or edit context.
  226. * @return array
  227. */
  228. public function get_taxes( $context = 'view' ) {
  229. return $this->get_prop( 'taxes', $context );
  230. }
  231. /**
  232. * Get tax class.
  233. *
  234. * @param string $context View or edit context.
  235. * @return string
  236. */
  237. public function get_tax_class( $context = 'view' ) {
  238. return get_option( 'woocommerce_shipping_tax_class' );
  239. }
  240. /*
  241. |--------------------------------------------------------------------------
  242. | Array Access Methods
  243. |--------------------------------------------------------------------------
  244. |
  245. | For backwards compatibility with legacy arrays.
  246. |
  247. */
  248. /**
  249. * Offset get: for ArrayAccess/Backwards compatibility.
  250. *
  251. * @param string $offset Key.
  252. * @return mixed
  253. */
  254. #[\ReturnTypeWillChange]
  255. public function offsetGet( $offset ) {
  256. if ( 'cost' === $offset ) {
  257. $offset = 'total';
  258. }
  259. return parent::offsetGet( $offset );
  260. }
  261. /**
  262. * Offset set: for ArrayAccess/Backwards compatibility.
  263. *
  264. * @deprecated 4.4.0
  265. * @param string $offset Key.
  266. * @param mixed $value Value to set.
  267. */
  268. #[\ReturnTypeWillChange]
  269. public function offsetSet( $offset, $value ) {
  270. wc_deprecated_function( 'WC_Order_Item_Shipping::offsetSet', '4.4.0', '' );
  271. if ( 'cost' === $offset ) {
  272. $offset = 'total';
  273. }
  274. parent::offsetSet( $offset, $value );
  275. }
  276. /**
  277. * Offset exists: for ArrayAccess.
  278. *
  279. * @param string $offset Key.
  280. * @return bool
  281. */
  282. #[\ReturnTypeWillChange]
  283. public function offsetExists( $offset ) {
  284. if ( in_array( $offset, array( 'cost' ), true ) ) {
  285. return true;
  286. }
  287. return parent::offsetExists( $offset );
  288. }
  289. }