Geen omschrijving

class-wc-order.php 62KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. <?php
  2. /**
  3. * Regular order
  4. *
  5. * @package WooCommerce\Classes
  6. * @version 2.2.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Order Class.
  11. *
  12. * These are regular WooCommerce orders, which extend the abstract order class.
  13. */
  14. class WC_Order extends WC_Abstract_Order {
  15. /**
  16. * Stores data about status changes so relevant hooks can be fired.
  17. *
  18. * @var bool|array
  19. */
  20. protected $status_transition = false;
  21. /**
  22. * Order Data array. This is the core order data exposed in APIs since 3.0.0.
  23. *
  24. * @since 3.0.0
  25. * @var array
  26. */
  27. protected $data = array(
  28. // Abstract order props.
  29. 'parent_id' => 0,
  30. 'status' => '',
  31. 'currency' => '',
  32. 'version' => '',
  33. 'prices_include_tax' => false,
  34. 'date_created' => null,
  35. 'date_modified' => null,
  36. 'discount_total' => 0,
  37. 'discount_tax' => 0,
  38. 'shipping_total' => 0,
  39. 'shipping_tax' => 0,
  40. 'cart_tax' => 0,
  41. 'total' => 0,
  42. 'total_tax' => 0,
  43. // Order props.
  44. 'customer_id' => 0,
  45. 'order_key' => '',
  46. 'billing' => array(
  47. 'first_name' => '',
  48. 'last_name' => '',
  49. 'company' => '',
  50. 'address_1' => '',
  51. 'address_2' => '',
  52. 'city' => '',
  53. 'state' => '',
  54. 'postcode' => '',
  55. 'country' => '',
  56. 'email' => '',
  57. 'phone' => '',
  58. ),
  59. 'shipping' => array(
  60. 'first_name' => '',
  61. 'last_name' => '',
  62. 'company' => '',
  63. 'address_1' => '',
  64. 'address_2' => '',
  65. 'city' => '',
  66. 'state' => '',
  67. 'postcode' => '',
  68. 'country' => '',
  69. 'phone' => '',
  70. ),
  71. 'payment_method' => '',
  72. 'payment_method_title' => '',
  73. 'transaction_id' => '',
  74. 'customer_ip_address' => '',
  75. 'customer_user_agent' => '',
  76. 'created_via' => '',
  77. 'customer_note' => '',
  78. 'date_completed' => null,
  79. 'date_paid' => null,
  80. 'cart_hash' => '',
  81. );
  82. /**
  83. * When a payment is complete this function is called.
  84. *
  85. * Most of the time this should mark an order as 'processing' so that admin can process/post the items.
  86. * If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action.
  87. * Stock levels are reduced at this point.
  88. * Sales are also recorded for products.
  89. * Finally, record the date of payment.
  90. *
  91. * @param string $transaction_id Optional transaction id to store in post meta.
  92. * @return bool success
  93. */
  94. public function payment_complete( $transaction_id = '' ) {
  95. if ( ! $this->get_id() ) { // Order must exist.
  96. return false;
  97. }
  98. try {
  99. do_action( 'woocommerce_pre_payment_complete', $this->get_id() );
  100. if ( WC()->session ) {
  101. WC()->session->set( 'order_awaiting_payment', false );
  102. }
  103. if ( $this->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', array( 'on-hold', 'pending', 'failed', 'cancelled' ), $this ) ) ) {
  104. if ( ! empty( $transaction_id ) ) {
  105. $this->set_transaction_id( $transaction_id );
  106. }
  107. if ( ! $this->get_date_paid( 'edit' ) ) {
  108. $this->set_date_paid( time() );
  109. }
  110. $this->set_status( apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? 'processing' : 'completed', $this->get_id(), $this ) );
  111. $this->save();
  112. do_action( 'woocommerce_payment_complete', $this->get_id() );
  113. } else {
  114. do_action( 'woocommerce_payment_complete_order_status_' . $this->get_status(), $this->get_id() );
  115. }
  116. } catch ( Exception $e ) {
  117. /**
  118. * If there was an error completing the payment, log to a file and add an order note so the admin can take action.
  119. */
  120. $logger = wc_get_logger();
  121. $logger->error(
  122. sprintf(
  123. 'Error completing payment for order #%d',
  124. $this->get_id()
  125. ),
  126. array(
  127. 'order' => $this,
  128. 'error' => $e,
  129. )
  130. );
  131. $this->add_order_note( __( 'Payment complete event failed.', 'woocommerce' ) . ' ' . $e->getMessage() );
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * Gets order total - formatted for display.
  138. *
  139. * @param string $tax_display Type of tax display.
  140. * @param bool $display_refunded If should include refunded value.
  141. *
  142. * @return string
  143. */
  144. public function get_formatted_order_total( $tax_display = '', $display_refunded = true ) {
  145. $formatted_total = wc_price( $this->get_total(), array( 'currency' => $this->get_currency() ) );
  146. $order_total = $this->get_total();
  147. $total_refunded = $this->get_total_refunded();
  148. $tax_string = '';
  149. // Tax for inclusive prices.
  150. if ( wc_tax_enabled() && 'incl' === $tax_display ) {
  151. $tax_string_array = array();
  152. $tax_totals = $this->get_tax_totals();
  153. if ( 'itemized' === get_option( 'woocommerce_tax_total_display' ) ) {
  154. foreach ( $tax_totals as $code => $tax ) {
  155. $tax_amount = ( $total_refunded && $display_refunded ) ? wc_price( WC_Tax::round( $tax->amount - $this->get_total_tax_refunded_by_rate_id( $tax->rate_id ) ), array( 'currency' => $this->get_currency() ) ) : $tax->formatted_amount;
  156. $tax_string_array[] = sprintf( '%s %s', $tax_amount, $tax->label );
  157. }
  158. } elseif ( ! empty( $tax_totals ) ) {
  159. $tax_amount = ( $total_refunded && $display_refunded ) ? $this->get_total_tax() - $this->get_total_tax_refunded() : $this->get_total_tax();
  160. $tax_string_array[] = sprintf( '%s %s', wc_price( $tax_amount, array( 'currency' => $this->get_currency() ) ), WC()->countries->tax_or_vat() );
  161. }
  162. if ( ! empty( $tax_string_array ) ) {
  163. /* translators: %s: taxes */
  164. $tax_string = ' <small class="includes_tax">' . sprintf( __( '(includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) ) . '</small>';
  165. }
  166. }
  167. if ( $total_refunded && $display_refunded ) {
  168. $formatted_total = '<del aria-hidden="true">' . wp_strip_all_tags( $formatted_total ) . '</del> <ins>' . wc_price( $order_total - $total_refunded, array( 'currency' => $this->get_currency() ) ) . $tax_string . '</ins>';
  169. } else {
  170. $formatted_total .= $tax_string;
  171. }
  172. /**
  173. * Filter WooCommerce formatted order total.
  174. *
  175. * @param string $formatted_total Total to display.
  176. * @param WC_Order $order Order data.
  177. * @param string $tax_display Type of tax display.
  178. * @param bool $display_refunded If should include refunded value.
  179. */
  180. return apply_filters( 'woocommerce_get_formatted_order_total', $formatted_total, $this, $tax_display, $display_refunded );
  181. }
  182. /*
  183. |--------------------------------------------------------------------------
  184. | CRUD methods
  185. |--------------------------------------------------------------------------
  186. |
  187. | Methods which create, read, update and delete orders from the database.
  188. | Written in abstract fashion so that the way orders are stored can be
  189. | changed more easily in the future.
  190. |
  191. | A save method is included for convenience (chooses update or create based
  192. | on if the order exists yet).
  193. |
  194. */
  195. /**
  196. * Save data to the database.
  197. *
  198. * @since 3.0.0
  199. * @return int order ID
  200. */
  201. public function save() {
  202. $this->maybe_set_user_billing_email();
  203. parent::save();
  204. $this->status_transition();
  205. return $this->get_id();
  206. }
  207. /**
  208. * Log an error about this order is exception is encountered.
  209. *
  210. * @param Exception $e Exception object.
  211. * @param string $message Message regarding exception thrown.
  212. * @since 3.7.0
  213. */
  214. protected function handle_exception( $e, $message = 'Error' ) {
  215. wc_get_logger()->error(
  216. $message,
  217. array(
  218. 'order' => $this,
  219. 'error' => $e,
  220. )
  221. );
  222. $this->add_order_note( $message . ' ' . $e->getMessage() );
  223. }
  224. /**
  225. * Set order status.
  226. *
  227. * @since 3.0.0
  228. * @param string $new_status Status to change the order to. No internal wc- prefix is required.
  229. * @param string $note Optional note to add.
  230. * @param bool $manual_update Is this a manual order status change?.
  231. * @return array
  232. */
  233. public function set_status( $new_status, $note = '', $manual_update = false ) {
  234. $result = parent::set_status( $new_status );
  235. if ( true === $this->object_read && ! empty( $result['from'] ) && $result['from'] !== $result['to'] ) {
  236. $this->status_transition = array(
  237. 'from' => ! empty( $this->status_transition['from'] ) ? $this->status_transition['from'] : $result['from'],
  238. 'to' => $result['to'],
  239. 'note' => $note,
  240. 'manual' => (bool) $manual_update,
  241. );
  242. if ( $manual_update ) {
  243. do_action( 'woocommerce_order_edit_status', $this->get_id(), $result['to'] );
  244. }
  245. $this->maybe_set_date_paid();
  246. $this->maybe_set_date_completed();
  247. }
  248. return $result;
  249. }
  250. /**
  251. * Maybe set date paid.
  252. *
  253. * Sets the date paid variable when transitioning to the payment complete
  254. * order status. This is either processing or completed. This is not filtered
  255. * to avoid infinite loops e.g. if loading an order via the filter.
  256. *
  257. * Date paid is set once in this manner - only when it is not already set.
  258. * This ensures the data exists even if a gateway does not use the
  259. * `payment_complete` method.
  260. *
  261. * @since 3.0.0
  262. */
  263. public function maybe_set_date_paid() {
  264. // This logic only runs if the date_paid prop has not been set yet.
  265. if ( ! $this->get_date_paid( 'edit' ) ) {
  266. $payment_completed_status = apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? 'processing' : 'completed', $this->get_id(), $this );
  267. if ( $this->has_status( $payment_completed_status ) ) {
  268. // If payment complete status is reached, set paid now.
  269. $this->set_date_paid( time() );
  270. } elseif ( 'processing' === $payment_completed_status && $this->has_status( 'completed' ) ) {
  271. // If payment complete status was processing, but we've passed that and still have no date, set it now.
  272. $this->set_date_paid( time() );
  273. }
  274. }
  275. }
  276. /**
  277. * Maybe set date completed.
  278. *
  279. * Sets the date completed variable when transitioning to completed status.
  280. *
  281. * @since 3.0.0
  282. */
  283. protected function maybe_set_date_completed() {
  284. if ( $this->has_status( 'completed' ) ) {
  285. $this->set_date_completed( time() );
  286. }
  287. }
  288. /**
  289. * Updates status of order immediately.
  290. *
  291. * @uses WC_Order::set_status()
  292. * @param string $new_status Status to change the order to. No internal wc- prefix is required.
  293. * @param string $note Optional note to add.
  294. * @param bool $manual Is this a manual order status change?.
  295. * @return bool
  296. */
  297. public function update_status( $new_status, $note = '', $manual = false ) {
  298. if ( ! $this->get_id() ) { // Order must exist.
  299. return false;
  300. }
  301. try {
  302. $this->set_status( $new_status, $note, $manual );
  303. $this->save();
  304. } catch ( Exception $e ) {
  305. $logger = wc_get_logger();
  306. $logger->error(
  307. sprintf(
  308. 'Error updating status for order #%d',
  309. $this->get_id()
  310. ),
  311. array(
  312. 'order' => $this,
  313. 'error' => $e,
  314. )
  315. );
  316. $this->add_order_note( __( 'Update status event failed.', 'woocommerce' ) . ' ' . $e->getMessage() );
  317. return false;
  318. }
  319. return true;
  320. }
  321. /**
  322. * Handle the status transition.
  323. */
  324. protected function status_transition() {
  325. $status_transition = $this->status_transition;
  326. // Reset status transition variable.
  327. $this->status_transition = false;
  328. if ( $status_transition ) {
  329. try {
  330. do_action( 'woocommerce_order_status_' . $status_transition['to'], $this->get_id(), $this );
  331. if ( ! empty( $status_transition['from'] ) ) {
  332. /* translators: 1: old order status 2: new order status */
  333. $transition_note = sprintf( __( 'Order status changed from %1$s to %2$s.', 'woocommerce' ), wc_get_order_status_name( $status_transition['from'] ), wc_get_order_status_name( $status_transition['to'] ) );
  334. // Note the transition occurred.
  335. $this->add_status_transition_note( $transition_note, $status_transition );
  336. do_action( 'woocommerce_order_status_' . $status_transition['from'] . '_to_' . $status_transition['to'], $this->get_id(), $this );
  337. do_action( 'woocommerce_order_status_changed', $this->get_id(), $status_transition['from'], $status_transition['to'], $this );
  338. // Work out if this was for a payment, and trigger a payment_status hook instead.
  339. if (
  340. in_array( $status_transition['from'], apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $this ), true )
  341. && in_array( $status_transition['to'], wc_get_is_paid_statuses(), true )
  342. ) {
  343. /**
  344. * Fires when the order progresses from a pending payment status to a paid one.
  345. *
  346. * @since 3.9.0
  347. * @param int Order ID
  348. * @param WC_Order Order object
  349. */
  350. do_action( 'woocommerce_order_payment_status_changed', $this->get_id(), $this );
  351. }
  352. } else {
  353. /* translators: %s: new order status */
  354. $transition_note = sprintf( __( 'Order status set to %s.', 'woocommerce' ), wc_get_order_status_name( $status_transition['to'] ) );
  355. // Note the transition occurred.
  356. $this->add_status_transition_note( $transition_note, $status_transition );
  357. }
  358. } catch ( Exception $e ) {
  359. $logger = wc_get_logger();
  360. $logger->error(
  361. sprintf(
  362. 'Status transition of order #%d errored!',
  363. $this->get_id()
  364. ),
  365. array(
  366. 'order' => $this,
  367. 'error' => $e,
  368. )
  369. );
  370. $this->add_order_note( __( 'Error during status transition.', 'woocommerce' ) . ' ' . $e->getMessage() );
  371. }
  372. }
  373. }
  374. /*
  375. |--------------------------------------------------------------------------
  376. | Getters
  377. |--------------------------------------------------------------------------
  378. |
  379. | Methods for getting data from the order object.
  380. |
  381. */
  382. /**
  383. * Get basic order data in array format.
  384. *
  385. * @return array
  386. */
  387. public function get_base_data() {
  388. return array_merge(
  389. array( 'id' => $this->get_id() ),
  390. $this->data,
  391. array( 'number' => $this->get_order_number() )
  392. );
  393. }
  394. /**
  395. * Get all class data in array format.
  396. *
  397. * @since 3.0.0
  398. * @return array
  399. */
  400. public function get_data() {
  401. return array_merge(
  402. $this->get_base_data(),
  403. array(
  404. 'meta_data' => $this->get_meta_data(),
  405. 'line_items' => $this->get_items( 'line_item' ),
  406. 'tax_lines' => $this->get_items( 'tax' ),
  407. 'shipping_lines' => $this->get_items( 'shipping' ),
  408. 'fee_lines' => $this->get_items( 'fee' ),
  409. 'coupon_lines' => $this->get_items( 'coupon' ),
  410. )
  411. );
  412. }
  413. /**
  414. * Expands the shipping and billing information in the changes array.
  415. */
  416. public function get_changes() {
  417. $changed_props = parent::get_changes();
  418. $subs = array( 'shipping', 'billing' );
  419. foreach ( $subs as $sub ) {
  420. if ( ! empty( $changed_props[ $sub ] ) ) {
  421. foreach ( $changed_props[ $sub ] as $sub_prop => $value ) {
  422. $changed_props[ $sub . '_' . $sub_prop ] = $value;
  423. }
  424. }
  425. }
  426. if ( isset( $changed_props['customer_note'] ) ) {
  427. $changed_props['post_excerpt'] = $changed_props['customer_note'];
  428. }
  429. return $changed_props;
  430. }
  431. /**
  432. * Gets the order number for display (by default, order ID).
  433. *
  434. * @return string
  435. */
  436. public function get_order_number() {
  437. return (string) apply_filters( 'woocommerce_order_number', $this->get_id(), $this );
  438. }
  439. /**
  440. * Get order key.
  441. *
  442. * @since 3.0.0
  443. * @param string $context What the value is for. Valid values are view and edit.
  444. * @return string
  445. */
  446. public function get_order_key( $context = 'view' ) {
  447. return $this->get_prop( 'order_key', $context );
  448. }
  449. /**
  450. * Get customer_id.
  451. *
  452. * @param string $context What the value is for. Valid values are view and edit.
  453. * @return int
  454. */
  455. public function get_customer_id( $context = 'view' ) {
  456. return $this->get_prop( 'customer_id', $context );
  457. }
  458. /**
  459. * Alias for get_customer_id().
  460. *
  461. * @param string $context What the value is for. Valid values are view and edit.
  462. * @return int
  463. */
  464. public function get_user_id( $context = 'view' ) {
  465. return $this->get_customer_id( $context );
  466. }
  467. /**
  468. * Get the user associated with the order. False for guests.
  469. *
  470. * @return WP_User|false
  471. */
  472. public function get_user() {
  473. return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false;
  474. }
  475. /**
  476. * Gets a prop for a getter method.
  477. *
  478. * @since 3.0.0
  479. * @param string $prop Name of prop to get.
  480. * @param string $address billing or shipping.
  481. * @param string $context What the value is for. Valid values are view and edit.
  482. * @return mixed
  483. */
  484. protected function get_address_prop( $prop, $address = 'billing', $context = 'view' ) {
  485. $value = null;
  486. if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
  487. $value = isset( $this->changes[ $address ][ $prop ] ) ? $this->changes[ $address ][ $prop ] : $this->data[ $address ][ $prop ];
  488. if ( 'view' === $context ) {
  489. $value = apply_filters( $this->get_hook_prefix() . $address . '_' . $prop, $value, $this );
  490. }
  491. }
  492. return $value;
  493. }
  494. /**
  495. * Get billing first name.
  496. *
  497. * @param string $context What the value is for. Valid values are view and edit.
  498. * @return string
  499. */
  500. public function get_billing_first_name( $context = 'view' ) {
  501. return $this->get_address_prop( 'first_name', 'billing', $context );
  502. }
  503. /**
  504. * Get billing last name.
  505. *
  506. * @param string $context What the value is for. Valid values are view and edit.
  507. * @return string
  508. */
  509. public function get_billing_last_name( $context = 'view' ) {
  510. return $this->get_address_prop( 'last_name', 'billing', $context );
  511. }
  512. /**
  513. * Get billing company.
  514. *
  515. * @param string $context What the value is for. Valid values are view and edit.
  516. * @return string
  517. */
  518. public function get_billing_company( $context = 'view' ) {
  519. return $this->get_address_prop( 'company', 'billing', $context );
  520. }
  521. /**
  522. * Get billing address line 1.
  523. *
  524. * @param string $context What the value is for. Valid values are view and edit.
  525. * @return string
  526. */
  527. public function get_billing_address_1( $context = 'view' ) {
  528. return $this->get_address_prop( 'address_1', 'billing', $context );
  529. }
  530. /**
  531. * Get billing address line 2.
  532. *
  533. * @param string $context What the value is for. Valid values are view and edit.
  534. * @return string
  535. */
  536. public function get_billing_address_2( $context = 'view' ) {
  537. return $this->get_address_prop( 'address_2', 'billing', $context );
  538. }
  539. /**
  540. * Get billing city.
  541. *
  542. * @param string $context What the value is for. Valid values are view and edit.
  543. * @return string
  544. */
  545. public function get_billing_city( $context = 'view' ) {
  546. return $this->get_address_prop( 'city', 'billing', $context );
  547. }
  548. /**
  549. * Get billing state.
  550. *
  551. * @param string $context What the value is for. Valid values are view and edit.
  552. * @return string
  553. */
  554. public function get_billing_state( $context = 'view' ) {
  555. return $this->get_address_prop( 'state', 'billing', $context );
  556. }
  557. /**
  558. * Get billing postcode.
  559. *
  560. * @param string $context What the value is for. Valid values are view and edit.
  561. * @return string
  562. */
  563. public function get_billing_postcode( $context = 'view' ) {
  564. return $this->get_address_prop( 'postcode', 'billing', $context );
  565. }
  566. /**
  567. * Get billing country.
  568. *
  569. * @param string $context What the value is for. Valid values are view and edit.
  570. * @return string
  571. */
  572. public function get_billing_country( $context = 'view' ) {
  573. return $this->get_address_prop( 'country', 'billing', $context );
  574. }
  575. /**
  576. * Get billing email.
  577. *
  578. * @param string $context What the value is for. Valid values are view and edit.
  579. * @return string
  580. */
  581. public function get_billing_email( $context = 'view' ) {
  582. return $this->get_address_prop( 'email', 'billing', $context );
  583. }
  584. /**
  585. * Get billing phone.
  586. *
  587. * @param string $context What the value is for. Valid values are view and edit.
  588. * @return string
  589. */
  590. public function get_billing_phone( $context = 'view' ) {
  591. return $this->get_address_prop( 'phone', 'billing', $context );
  592. }
  593. /**
  594. * Get shipping first name.
  595. *
  596. * @param string $context What the value is for. Valid values are view and edit.
  597. * @return string
  598. */
  599. public function get_shipping_first_name( $context = 'view' ) {
  600. return $this->get_address_prop( 'first_name', 'shipping', $context );
  601. }
  602. /**
  603. * Get shipping_last_name.
  604. *
  605. * @param string $context What the value is for. Valid values are view and edit.
  606. * @return string
  607. */
  608. public function get_shipping_last_name( $context = 'view' ) {
  609. return $this->get_address_prop( 'last_name', 'shipping', $context );
  610. }
  611. /**
  612. * Get shipping company.
  613. *
  614. * @param string $context What the value is for. Valid values are view and edit.
  615. * @return string
  616. */
  617. public function get_shipping_company( $context = 'view' ) {
  618. return $this->get_address_prop( 'company', 'shipping', $context );
  619. }
  620. /**
  621. * Get shipping address line 1.
  622. *
  623. * @param string $context What the value is for. Valid values are view and edit.
  624. * @return string
  625. */
  626. public function get_shipping_address_1( $context = 'view' ) {
  627. return $this->get_address_prop( 'address_1', 'shipping', $context );
  628. }
  629. /**
  630. * Get shipping address line 2.
  631. *
  632. * @param string $context What the value is for. Valid values are view and edit.
  633. * @return string
  634. */
  635. public function get_shipping_address_2( $context = 'view' ) {
  636. return $this->get_address_prop( 'address_2', 'shipping', $context );
  637. }
  638. /**
  639. * Get shipping city.
  640. *
  641. * @param string $context What the value is for. Valid values are view and edit.
  642. * @return string
  643. */
  644. public function get_shipping_city( $context = 'view' ) {
  645. return $this->get_address_prop( 'city', 'shipping', $context );
  646. }
  647. /**
  648. * Get shipping state.
  649. *
  650. * @param string $context What the value is for. Valid values are view and edit.
  651. * @return string
  652. */
  653. public function get_shipping_state( $context = 'view' ) {
  654. return $this->get_address_prop( 'state', 'shipping', $context );
  655. }
  656. /**
  657. * Get shipping postcode.
  658. *
  659. * @param string $context What the value is for. Valid values are view and edit.
  660. * @return string
  661. */
  662. public function get_shipping_postcode( $context = 'view' ) {
  663. return $this->get_address_prop( 'postcode', 'shipping', $context );
  664. }
  665. /**
  666. * Get shipping country.
  667. *
  668. * @param string $context What the value is for. Valid values are view and edit.
  669. * @return string
  670. */
  671. public function get_shipping_country( $context = 'view' ) {
  672. return $this->get_address_prop( 'country', 'shipping', $context );
  673. }
  674. /**
  675. * Get shipping phone.
  676. *
  677. * @since 5.6.0
  678. * @param string $context What the value is for. Valid values are view and edit.
  679. * @return string
  680. */
  681. public function get_shipping_phone( $context = 'view' ) {
  682. return $this->get_address_prop( 'phone', 'shipping', $context );
  683. }
  684. /**
  685. * Get the payment method.
  686. *
  687. * @param string $context What the value is for. Valid values are view and edit.
  688. * @return string
  689. */
  690. public function get_payment_method( $context = 'view' ) {
  691. return $this->get_prop( 'payment_method', $context );
  692. }
  693. /**
  694. * Get payment method title.
  695. *
  696. * @param string $context What the value is for. Valid values are view and edit.
  697. * @return string
  698. */
  699. public function get_payment_method_title( $context = 'view' ) {
  700. return $this->get_prop( 'payment_method_title', $context );
  701. }
  702. /**
  703. * Get transaction d.
  704. *
  705. * @param string $context What the value is for. Valid values are view and edit.
  706. * @return string
  707. */
  708. public function get_transaction_id( $context = 'view' ) {
  709. return $this->get_prop( 'transaction_id', $context );
  710. }
  711. /**
  712. * Get customer ip address.
  713. *
  714. * @param string $context What the value is for. Valid values are view and edit.
  715. * @return string
  716. */
  717. public function get_customer_ip_address( $context = 'view' ) {
  718. return $this->get_prop( 'customer_ip_address', $context );
  719. }
  720. /**
  721. * Get customer user agent.
  722. *
  723. * @param string $context What the value is for. Valid values are view and edit.
  724. * @return string
  725. */
  726. public function get_customer_user_agent( $context = 'view' ) {
  727. return $this->get_prop( 'customer_user_agent', $context );
  728. }
  729. /**
  730. * Get created via.
  731. *
  732. * @param string $context What the value is for. Valid values are view and edit.
  733. * @return string
  734. */
  735. public function get_created_via( $context = 'view' ) {
  736. return $this->get_prop( 'created_via', $context );
  737. }
  738. /**
  739. * Get customer note.
  740. *
  741. * @param string $context What the value is for. Valid values are view and edit.
  742. * @return string
  743. */
  744. public function get_customer_note( $context = 'view' ) {
  745. return $this->get_prop( 'customer_note', $context );
  746. }
  747. /**
  748. * Get date completed.
  749. *
  750. * @param string $context What the value is for. Valid values are view and edit.
  751. * @return WC_DateTime|NULL object if the date is set or null if there is no date.
  752. */
  753. public function get_date_completed( $context = 'view' ) {
  754. return $this->get_prop( 'date_completed', $context );
  755. }
  756. /**
  757. * Get date paid.
  758. *
  759. * @param string $context What the value is for. Valid values are view and edit.
  760. * @return WC_DateTime|NULL object if the date is set or null if there is no date.
  761. */
  762. public function get_date_paid( $context = 'view' ) {
  763. $date_paid = $this->get_prop( 'date_paid', $context );
  764. if ( 'view' === $context && ! $date_paid && version_compare( $this->get_version( 'edit' ), '3.0', '<' ) && $this->has_status( apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? 'processing' : 'completed', $this->get_id(), $this ) ) ) {
  765. // In view context, return a date if missing.
  766. $date_paid = $this->get_date_created( 'edit' );
  767. }
  768. return $date_paid;
  769. }
  770. /**
  771. * Get cart hash.
  772. *
  773. * @param string $context What the value is for. Valid values are view and edit.
  774. * @return string
  775. */
  776. public function get_cart_hash( $context = 'view' ) {
  777. return $this->get_prop( 'cart_hash', $context );
  778. }
  779. /**
  780. * Returns the requested address in raw, non-formatted way.
  781. * Note: Merges raw data with get_prop data so changes are returned too.
  782. *
  783. * @since 2.4.0
  784. * @param string $type Billing or shipping. Anything else besides 'billing' will return shipping address.
  785. * @return array The stored address after filter.
  786. */
  787. public function get_address( $type = 'billing' ) {
  788. return apply_filters( 'woocommerce_get_order_address', array_merge( $this->data[ $type ], $this->get_prop( $type, 'view' ) ), $type, $this );
  789. }
  790. /**
  791. * Get a formatted shipping address for the order.
  792. *
  793. * @return string
  794. */
  795. public function get_shipping_address_map_url() {
  796. $address = $this->get_address( 'shipping' );
  797. // Remove name and company before generate the Google Maps URL.
  798. unset( $address['first_name'], $address['last_name'], $address['company'], $address['phone'] );
  799. $address = apply_filters( 'woocommerce_shipping_address_map_url_parts', $address, $this );
  800. return apply_filters( 'woocommerce_shipping_address_map_url', 'https://maps.google.com/maps?&q=' . rawurlencode( implode( ', ', $address ) ) . '&z=16', $this );
  801. }
  802. /**
  803. * Get a formatted billing full name.
  804. *
  805. * @return string
  806. */
  807. public function get_formatted_billing_full_name() {
  808. /* translators: 1: first name 2: last name */
  809. return sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $this->get_billing_first_name(), $this->get_billing_last_name() );
  810. }
  811. /**
  812. * Get a formatted shipping full name.
  813. *
  814. * @return string
  815. */
  816. public function get_formatted_shipping_full_name() {
  817. /* translators: 1: first name 2: last name */
  818. return sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $this->get_shipping_first_name(), $this->get_shipping_last_name() );
  819. }
  820. /**
  821. * Get a formatted billing address for the order.
  822. *
  823. * @param string $empty_content Content to show if no address is present. @since 3.3.0.
  824. * @return string
  825. */
  826. public function get_formatted_billing_address( $empty_content = '' ) {
  827. $raw_address = apply_filters( 'woocommerce_order_formatted_billing_address', $this->get_address( 'billing' ), $this );
  828. $address = WC()->countries->get_formatted_address( $raw_address );
  829. /**
  830. * Filter orders formatted billing address.
  831. *
  832. * @since 3.8.0
  833. * @param string $address Formatted billing address string.
  834. * @param array $raw_address Raw billing address.
  835. * @param WC_Order $order Order data. @since 3.9.0
  836. */
  837. return apply_filters( 'woocommerce_order_get_formatted_billing_address', $address ? $address : $empty_content, $raw_address, $this );
  838. }
  839. /**
  840. * Get a formatted shipping address for the order.
  841. *
  842. * @param string $empty_content Content to show if no address is present. @since 3.3.0.
  843. * @return string
  844. */
  845. public function get_formatted_shipping_address( $empty_content = '' ) {
  846. $address = '';
  847. $raw_address = $this->get_address( 'shipping' );
  848. if ( $this->has_shipping_address() ) {
  849. $raw_address = apply_filters( 'woocommerce_order_formatted_shipping_address', $raw_address, $this );
  850. $address = WC()->countries->get_formatted_address( $raw_address );
  851. }
  852. /**
  853. * Filter orders formatted shipping address.
  854. *
  855. * @since 3.8.0
  856. * @param string $address Formatted billing address string.
  857. * @param array $raw_address Raw billing address.
  858. * @param WC_Order $order Order data. @since 3.9.0
  859. */
  860. return apply_filters( 'woocommerce_order_get_formatted_shipping_address', $address ? $address : $empty_content, $raw_address, $this );
  861. }
  862. /**
  863. * Returns true if the order has a billing address.
  864. *
  865. * @since 3.0.4
  866. * @return boolean
  867. */
  868. public function has_billing_address() {
  869. return $this->get_billing_address_1() || $this->get_billing_address_2();
  870. }
  871. /**
  872. * Returns true if the order has a shipping address.
  873. *
  874. * @since 3.0.4
  875. * @return boolean
  876. */
  877. public function has_shipping_address() {
  878. return $this->get_shipping_address_1() || $this->get_shipping_address_2();
  879. }
  880. /*
  881. |--------------------------------------------------------------------------
  882. | Setters
  883. |--------------------------------------------------------------------------
  884. |
  885. | Functions for setting order data. These should not update anything in the
  886. | database itself and should only change what is stored in the class
  887. | object. However, for backwards compatibility pre 3.0.0 some of these
  888. | setters may handle both.
  889. |
  890. */
  891. /**
  892. * Sets a prop for a setter method.
  893. *
  894. * @since 3.0.0
  895. * @param string $prop Name of prop to set.
  896. * @param string $address Name of address to set. billing or shipping.
  897. * @param mixed $value Value of the prop.
  898. */
  899. protected function set_address_prop( $prop, $address, $value ) {
  900. if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
  901. if ( true === $this->object_read ) {
  902. if ( $value !== $this->data[ $address ][ $prop ] || ( isset( $this->changes[ $address ] ) && array_key_exists( $prop, $this->changes[ $address ] ) ) ) {
  903. $this->changes[ $address ][ $prop ] = $value;
  904. }
  905. } else {
  906. $this->data[ $address ][ $prop ] = $value;
  907. }
  908. }
  909. }
  910. /**
  911. * Set order key.
  912. *
  913. * @param string $value Max length 22 chars.
  914. * @throws WC_Data_Exception Throws exception when invalid data is found.
  915. */
  916. public function set_order_key( $value ) {
  917. $this->set_prop( 'order_key', substr( $value, 0, 22 ) );
  918. }
  919. /**
  920. * Set customer id.
  921. *
  922. * @param int $value Customer ID.
  923. * @throws WC_Data_Exception Throws exception when invalid data is found.
  924. */
  925. public function set_customer_id( $value ) {
  926. $this->set_prop( 'customer_id', absint( $value ) );
  927. }
  928. /**
  929. * Set billing first name.
  930. *
  931. * @param string $value Billing first name.
  932. * @throws WC_Data_Exception Throws exception when invalid data is found.
  933. */
  934. public function set_billing_first_name( $value ) {
  935. $this->set_address_prop( 'first_name', 'billing', $value );
  936. }
  937. /**
  938. * Set billing last name.
  939. *
  940. * @param string $value Billing last name.
  941. * @throws WC_Data_Exception Throws exception when invalid data is found.
  942. */
  943. public function set_billing_last_name( $value ) {
  944. $this->set_address_prop( 'last_name', 'billing', $value );
  945. }
  946. /**
  947. * Set billing company.
  948. *
  949. * @param string $value Billing company.
  950. * @throws WC_Data_Exception Throws exception when invalid data is found.
  951. */
  952. public function set_billing_company( $value ) {
  953. $this->set_address_prop( 'company', 'billing', $value );
  954. }
  955. /**
  956. * Set billing address line 1.
  957. *
  958. * @param string $value Billing address line 1.
  959. * @throws WC_Data_Exception Throws exception when invalid data is found.
  960. */
  961. public function set_billing_address_1( $value ) {
  962. $this->set_address_prop( 'address_1', 'billing', $value );
  963. }
  964. /**
  965. * Set billing address line 2.
  966. *
  967. * @param string $value Billing address line 2.
  968. * @throws WC_Data_Exception Throws exception when invalid data is found.
  969. */
  970. public function set_billing_address_2( $value ) {
  971. $this->set_address_prop( 'address_2', 'billing', $value );
  972. }
  973. /**
  974. * Set billing city.
  975. *
  976. * @param string $value Billing city.
  977. * @throws WC_Data_Exception Throws exception when invalid data is found.
  978. */
  979. public function set_billing_city( $value ) {
  980. $this->set_address_prop( 'city', 'billing', $value );
  981. }
  982. /**
  983. * Set billing state.
  984. *
  985. * @param string $value Billing state.
  986. * @throws WC_Data_Exception Throws exception when invalid data is found.
  987. */
  988. public function set_billing_state( $value ) {
  989. $this->set_address_prop( 'state', 'billing', $value );
  990. }
  991. /**
  992. * Set billing postcode.
  993. *
  994. * @param string $value Billing postcode.
  995. * @throws WC_Data_Exception Throws exception when invalid data is found.
  996. */
  997. public function set_billing_postcode( $value ) {
  998. $this->set_address_prop( 'postcode', 'billing', $value );
  999. }
  1000. /**
  1001. * Set billing country.
  1002. *
  1003. * @param string $value Billing country.
  1004. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1005. */
  1006. public function set_billing_country( $value ) {
  1007. $this->set_address_prop( 'country', 'billing', $value );
  1008. }
  1009. /**
  1010. * Maybe set empty billing email to that of the user who owns the order.
  1011. */
  1012. protected function maybe_set_user_billing_email() {
  1013. $user = $this->get_user();
  1014. if ( ! $this->get_billing_email() && $user ) {
  1015. try {
  1016. $this->set_billing_email( $user->user_email );
  1017. } catch ( WC_Data_Exception $e ) {
  1018. unset( $e );
  1019. }
  1020. }
  1021. }
  1022. /**
  1023. * Set billing email.
  1024. *
  1025. * @param string $value Billing email.
  1026. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1027. */
  1028. public function set_billing_email( $value ) {
  1029. if ( $value && ! is_email( $value ) ) {
  1030. $this->error( 'order_invalid_billing_email', __( 'Invalid billing email address', 'woocommerce' ) );
  1031. }
  1032. $this->set_address_prop( 'email', 'billing', sanitize_email( $value ) );
  1033. }
  1034. /**
  1035. * Set billing phone.
  1036. *
  1037. * @param string $value Billing phone.
  1038. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1039. */
  1040. public function set_billing_phone( $value ) {
  1041. $this->set_address_prop( 'phone', 'billing', $value );
  1042. }
  1043. /**
  1044. * Set shipping first name.
  1045. *
  1046. * @param string $value Shipping first name.
  1047. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1048. */
  1049. public function set_shipping_first_name( $value ) {
  1050. $this->set_address_prop( 'first_name', 'shipping', $value );
  1051. }
  1052. /**
  1053. * Set shipping last name.
  1054. *
  1055. * @param string $value Shipping last name.
  1056. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1057. */
  1058. public function set_shipping_last_name( $value ) {
  1059. $this->set_address_prop( 'last_name', 'shipping', $value );
  1060. }
  1061. /**
  1062. * Set shipping company.
  1063. *
  1064. * @param string $value Shipping company.
  1065. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1066. */
  1067. public function set_shipping_company( $value ) {
  1068. $this->set_address_prop( 'company', 'shipping', $value );
  1069. }
  1070. /**
  1071. * Set shipping address line 1.
  1072. *
  1073. * @param string $value Shipping address line 1.
  1074. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1075. */
  1076. public function set_shipping_address_1( $value ) {
  1077. $this->set_address_prop( 'address_1', 'shipping', $value );
  1078. }
  1079. /**
  1080. * Set shipping address line 2.
  1081. *
  1082. * @param string $value Shipping address line 2.
  1083. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1084. */
  1085. public function set_shipping_address_2( $value ) {
  1086. $this->set_address_prop( 'address_2', 'shipping', $value );
  1087. }
  1088. /**
  1089. * Set shipping city.
  1090. *
  1091. * @param string $value Shipping city.
  1092. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1093. */
  1094. public function set_shipping_city( $value ) {
  1095. $this->set_address_prop( 'city', 'shipping', $value );
  1096. }
  1097. /**
  1098. * Set shipping state.
  1099. *
  1100. * @param string $value Shipping state.
  1101. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1102. */
  1103. public function set_shipping_state( $value ) {
  1104. $this->set_address_prop( 'state', 'shipping', $value );
  1105. }
  1106. /**
  1107. * Set shipping postcode.
  1108. *
  1109. * @param string $value Shipping postcode.
  1110. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1111. */
  1112. public function set_shipping_postcode( $value ) {
  1113. $this->set_address_prop( 'postcode', 'shipping', $value );
  1114. }
  1115. /**
  1116. * Set shipping country.
  1117. *
  1118. * @param string $value Shipping country.
  1119. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1120. */
  1121. public function set_shipping_country( $value ) {
  1122. $this->set_address_prop( 'country', 'shipping', $value );
  1123. }
  1124. /**
  1125. * Set shipping phone.
  1126. *
  1127. * @since 5.6.0
  1128. * @param string $value Shipping phone.
  1129. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1130. */
  1131. public function set_shipping_phone( $value ) {
  1132. $this->set_address_prop( 'phone', 'shipping', $value );
  1133. }
  1134. /**
  1135. * Set the payment method.
  1136. *
  1137. * @param string $payment_method Supports WC_Payment_Gateway for bw compatibility with < 3.0.
  1138. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1139. */
  1140. public function set_payment_method( $payment_method = '' ) {
  1141. if ( is_object( $payment_method ) ) {
  1142. $this->set_payment_method( $payment_method->id );
  1143. $this->set_payment_method_title( $payment_method->get_title() );
  1144. } elseif ( '' === $payment_method ) {
  1145. $this->set_prop( 'payment_method', '' );
  1146. $this->set_prop( 'payment_method_title', '' );
  1147. } else {
  1148. $this->set_prop( 'payment_method', $payment_method );
  1149. }
  1150. }
  1151. /**
  1152. * Set payment method title.
  1153. *
  1154. * @param string $value Payment method title.
  1155. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1156. */
  1157. public function set_payment_method_title( $value ) {
  1158. $this->set_prop( 'payment_method_title', $value );
  1159. }
  1160. /**
  1161. * Set transaction id.
  1162. *
  1163. * @param string $value Transaction id.
  1164. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1165. */
  1166. public function set_transaction_id( $value ) {
  1167. $this->set_prop( 'transaction_id', $value );
  1168. }
  1169. /**
  1170. * Set customer ip address.
  1171. *
  1172. * @param string $value Customer ip address.
  1173. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1174. */
  1175. public function set_customer_ip_address( $value ) {
  1176. $this->set_prop( 'customer_ip_address', $value );
  1177. }
  1178. /**
  1179. * Set customer user agent.
  1180. *
  1181. * @param string $value Customer user agent.
  1182. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1183. */
  1184. public function set_customer_user_agent( $value ) {
  1185. $this->set_prop( 'customer_user_agent', $value );
  1186. }
  1187. /**
  1188. * Set created via.
  1189. *
  1190. * @param string $value Created via.
  1191. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1192. */
  1193. public function set_created_via( $value ) {
  1194. $this->set_prop( 'created_via', $value );
  1195. }
  1196. /**
  1197. * Set customer note.
  1198. *
  1199. * @param string $value Customer note.
  1200. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1201. */
  1202. public function set_customer_note( $value ) {
  1203. $this->set_prop( 'customer_note', $value );
  1204. }
  1205. /**
  1206. * Set date completed.
  1207. *
  1208. * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
  1209. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1210. */
  1211. public function set_date_completed( $date = null ) {
  1212. $this->set_date_prop( 'date_completed', $date );
  1213. }
  1214. /**
  1215. * Set date paid.
  1216. *
  1217. * @param string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
  1218. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1219. */
  1220. public function set_date_paid( $date = null ) {
  1221. $this->set_date_prop( 'date_paid', $date );
  1222. }
  1223. /**
  1224. * Set cart hash.
  1225. *
  1226. * @param string $value Cart hash.
  1227. * @throws WC_Data_Exception Throws exception when invalid data is found.
  1228. */
  1229. public function set_cart_hash( $value ) {
  1230. $this->set_prop( 'cart_hash', $value );
  1231. }
  1232. /*
  1233. |--------------------------------------------------------------------------
  1234. | Conditionals
  1235. |--------------------------------------------------------------------------
  1236. |
  1237. | Checks if a condition is true or false.
  1238. |
  1239. */
  1240. /**
  1241. * Check if an order key is valid.
  1242. *
  1243. * @param string $key Order key.
  1244. * @return bool
  1245. */
  1246. public function key_is_valid( $key ) {
  1247. return hash_equals( $this->get_order_key(), $key );
  1248. }
  1249. /**
  1250. * See if order matches cart_hash.
  1251. *
  1252. * @param string $cart_hash Cart hash.
  1253. * @return bool
  1254. */
  1255. public function has_cart_hash( $cart_hash = '' ) {
  1256. return hash_equals( $this->get_cart_hash(), $cart_hash ); // @codingStandardsIgnoreLine
  1257. }
  1258. /**
  1259. * Checks if an order can be edited, specifically for use on the Edit Order screen.
  1260. *
  1261. * @return bool
  1262. */
  1263. public function is_editable() {
  1264. return apply_filters( 'wc_order_is_editable', in_array( $this->get_status(), array( 'pending', 'on-hold', 'auto-draft' ), true ), $this );
  1265. }
  1266. /**
  1267. * Returns if an order has been paid for based on the order status.
  1268. *
  1269. * @since 2.5.0
  1270. * @return bool
  1271. */
  1272. public function is_paid() {
  1273. return apply_filters( 'woocommerce_order_is_paid', $this->has_status( wc_get_is_paid_statuses() ), $this );
  1274. }
  1275. /**
  1276. * Checks if product download is permitted.
  1277. *
  1278. * @return bool
  1279. */
  1280. public function is_download_permitted() {
  1281. return apply_filters( 'woocommerce_order_is_download_permitted', $this->has_status( 'completed' ) || ( 'yes' === get_option( 'woocommerce_downloads_grant_access_after_payment' ) && $this->has_status( 'processing' ) ), $this );
  1282. }
  1283. /**
  1284. * Checks if an order needs display the shipping address, based on shipping method.
  1285. *
  1286. * @return bool
  1287. */
  1288. public function needs_shipping_address() {
  1289. if ( 'no' === get_option( 'woocommerce_calc_shipping' ) ) {
  1290. return false;
  1291. }
  1292. $hide = apply_filters( 'woocommerce_order_hide_shipping_address', array( 'local_pickup' ), $this );
  1293. $needs_address = false;
  1294. foreach ( $this->get_shipping_methods() as $shipping_method ) {
  1295. $shipping_method_id = $shipping_method->get_method_id();
  1296. if ( ! in_array( $shipping_method_id, $hide, true ) ) {
  1297. $needs_address = true;
  1298. break;
  1299. }
  1300. }
  1301. return apply_filters( 'woocommerce_order_needs_shipping_address', $needs_address, $hide, $this );
  1302. }
  1303. /**
  1304. * Returns true if the order contains a downloadable product.
  1305. *
  1306. * @return bool
  1307. */
  1308. public function has_downloadable_item() {
  1309. foreach ( $this->get_items() as $item ) {
  1310. if ( $item->is_type( 'line_item' ) ) {
  1311. $product = $item->get_product();
  1312. if ( $product && $product->has_file() ) {
  1313. return true;
  1314. }
  1315. }
  1316. }
  1317. return false;
  1318. }
  1319. /**
  1320. * Get downloads from all line items for this order.
  1321. *
  1322. * @since 3.2.0
  1323. * @return array
  1324. */
  1325. public function get_downloadable_items() {
  1326. $downloads = array();
  1327. foreach ( $this->get_items() as $item ) {
  1328. if ( ! is_object( $item ) ) {
  1329. continue;
  1330. }
  1331. // Check item refunds.
  1332. $refunded_qty = abs( $this->get_qty_refunded_for_item( $item->get_id() ) );
  1333. if ( $refunded_qty && $item->get_quantity() === $refunded_qty ) {
  1334. continue;
  1335. }
  1336. if ( $item->is_type( 'line_item' ) ) {
  1337. $item_downloads = $item->get_item_downloads();
  1338. $product = $item->get_product();
  1339. if ( $product && $item_downloads ) {
  1340. foreach ( $item_downloads as $file ) {
  1341. $downloads[] = array(
  1342. 'download_url' => $file['download_url'],
  1343. 'download_id' => $file['id'],
  1344. 'product_id' => $product->get_id(),
  1345. 'product_name' => $product->get_name(),
  1346. 'product_url' => $product->is_visible() ? $product->get_permalink() : '', // Since 3.3.0.
  1347. 'download_name' => $file['name'],
  1348. 'order_id' => $this->get_id(),
  1349. 'order_key' => $this->get_order_key(),
  1350. 'downloads_remaining' => $file['downloads_remaining'],
  1351. 'access_expires' => $file['access_expires'],
  1352. 'file' => array(
  1353. 'name' => $file['name'],
  1354. 'file' => $file['file'],
  1355. ),
  1356. );
  1357. }
  1358. }
  1359. }
  1360. }
  1361. return apply_filters( 'woocommerce_order_get_downloadable_items', $downloads, $this );
  1362. }
  1363. /**
  1364. * Checks if an order needs payment, based on status and order total.
  1365. *
  1366. * @return bool
  1367. */
  1368. public function needs_payment() {
  1369. $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $this );
  1370. return apply_filters( 'woocommerce_order_needs_payment', ( $this->has_status( $valid_order_statuses ) && $this->get_total() > 0 ), $this, $valid_order_statuses );
  1371. }
  1372. /**
  1373. * See if the order needs processing before it can be completed.
  1374. *
  1375. * Orders which only contain virtual, downloadable items do not need admin
  1376. * intervention.
  1377. *
  1378. * Uses a transient so these calls are not repeated multiple times, and because
  1379. * once the order is processed this code/transient does not need to persist.
  1380. *
  1381. * @since 3.0.0
  1382. * @return bool
  1383. */
  1384. public function needs_processing() {
  1385. $transient_name = 'wc_order_' . $this->get_id() . '_needs_processing';
  1386. $needs_processing = get_transient( $transient_name );
  1387. if ( false === $needs_processing ) {
  1388. $needs_processing = 0;
  1389. if ( count( $this->get_items() ) > 0 ) {
  1390. foreach ( $this->get_items() as $item ) {
  1391. if ( $item->is_type( 'line_item' ) ) {
  1392. $product = $item->get_product();
  1393. if ( ! $product ) {
  1394. continue;
  1395. }
  1396. $virtual_downloadable_item = $product->is_downloadable() && $product->is_virtual();
  1397. if ( apply_filters( 'woocommerce_order_item_needs_processing', ! $virtual_downloadable_item, $product, $this->get_id() ) ) {
  1398. $needs_processing = 1;
  1399. break;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. set_transient( $transient_name, $needs_processing, DAY_IN_SECONDS );
  1405. }
  1406. return 1 === absint( $needs_processing );
  1407. }
  1408. /*
  1409. |--------------------------------------------------------------------------
  1410. | URLs and Endpoints
  1411. |--------------------------------------------------------------------------
  1412. */
  1413. /**
  1414. * Generates a URL so that a customer can pay for their (unpaid - pending) order. Pass 'true' for the checkout version which doesn't offer gateway choices.
  1415. *
  1416. * @param bool $on_checkout If on checkout.
  1417. * @return string
  1418. */
  1419. public function get_checkout_payment_url( $on_checkout = false ) {
  1420. $pay_url = wc_get_endpoint_url( 'order-pay', $this->get_id(), wc_get_checkout_url() );
  1421. if ( $on_checkout ) {
  1422. $pay_url = add_query_arg( 'key', $this->get_order_key(), $pay_url );
  1423. } else {
  1424. $pay_url = add_query_arg(
  1425. array(
  1426. 'pay_for_order' => 'true',
  1427. 'key' => $this->get_order_key(),
  1428. ),
  1429. $pay_url
  1430. );
  1431. }
  1432. return apply_filters( 'woocommerce_get_checkout_payment_url', $pay_url, $this );
  1433. }
  1434. /**
  1435. * Generates a URL for the thanks page (order received).
  1436. *
  1437. * @return string
  1438. */
  1439. public function get_checkout_order_received_url() {
  1440. $order_received_url = wc_get_endpoint_url( 'order-received', $this->get_id(), wc_get_checkout_url() );
  1441. $order_received_url = add_query_arg( 'key', $this->get_order_key(), $order_received_url );
  1442. return apply_filters( 'woocommerce_get_checkout_order_received_url', $order_received_url, $this );
  1443. }
  1444. /**
  1445. * Generates a URL so that a customer can cancel their (unpaid - pending) order.
  1446. *
  1447. * @param string $redirect Redirect URL.
  1448. * @return string
  1449. */
  1450. public function get_cancel_order_url( $redirect = '' ) {
  1451. return apply_filters(
  1452. 'woocommerce_get_cancel_order_url',
  1453. wp_nonce_url(
  1454. add_query_arg(
  1455. array(
  1456. 'cancel_order' => 'true',
  1457. 'order' => $this->get_order_key(),
  1458. 'order_id' => $this->get_id(),
  1459. 'redirect' => $redirect,
  1460. ),
  1461. $this->get_cancel_endpoint()
  1462. ),
  1463. 'woocommerce-cancel_order'
  1464. )
  1465. );
  1466. }
  1467. /**
  1468. * Generates a raw (unescaped) cancel-order URL for use by payment gateways.
  1469. *
  1470. * @param string $redirect Redirect URL.
  1471. * @return string The unescaped cancel-order URL.
  1472. */
  1473. public function get_cancel_order_url_raw( $redirect = '' ) {
  1474. return apply_filters(
  1475. 'woocommerce_get_cancel_order_url_raw',
  1476. add_query_arg(
  1477. array(
  1478. 'cancel_order' => 'true',
  1479. 'order' => $this->get_order_key(),
  1480. 'order_id' => $this->get_id(),
  1481. 'redirect' => $redirect,
  1482. '_wpnonce' => wp_create_nonce( 'woocommerce-cancel_order' ),
  1483. ),
  1484. $this->get_cancel_endpoint()
  1485. )
  1486. );
  1487. }
  1488. /**
  1489. * Helper method to return the cancel endpoint.
  1490. *
  1491. * @return string the cancel endpoint; either the cart page or the home page.
  1492. */
  1493. public function get_cancel_endpoint() {
  1494. $cancel_endpoint = wc_get_cart_url();
  1495. if ( ! $cancel_endpoint ) {
  1496. $cancel_endpoint = home_url();
  1497. }
  1498. if ( false === strpos( $cancel_endpoint, '?' ) ) {
  1499. $cancel_endpoint = trailingslashit( $cancel_endpoint );
  1500. }
  1501. return $cancel_endpoint;
  1502. }
  1503. /**
  1504. * Generates a URL to view an order from the my account page.
  1505. *
  1506. * @return string
  1507. */
  1508. public function get_view_order_url() {
  1509. return apply_filters( 'woocommerce_get_view_order_url', wc_get_endpoint_url( 'view-order', $this->get_id(), wc_get_page_permalink( 'myaccount' ) ), $this );
  1510. }
  1511. /**
  1512. * Get's the URL to edit the order in the backend.
  1513. *
  1514. * @since 3.3.0
  1515. * @return string
  1516. */
  1517. public function get_edit_order_url() {
  1518. return apply_filters( 'woocommerce_get_edit_order_url', get_admin_url( null, 'post.php?post=' . $this->get_id() . '&action=edit' ), $this );
  1519. }
  1520. /*
  1521. |--------------------------------------------------------------------------
  1522. | Order notes.
  1523. |--------------------------------------------------------------------------
  1524. */
  1525. /**
  1526. * Adds a note (comment) to the order. Order must exist.
  1527. *
  1528. * @param string $note Note to add.
  1529. * @param int $is_customer_note Is this a note for the customer?.
  1530. * @param bool $added_by_user Was the note added by a user?.
  1531. * @return int Comment ID.
  1532. */
  1533. public function add_order_note( $note, $is_customer_note = 0, $added_by_user = false ) {
  1534. if ( ! $this->get_id() ) {
  1535. return 0;
  1536. }
  1537. if ( is_user_logged_in() && current_user_can( 'edit_shop_orders', $this->get_id() ) && $added_by_user ) {
  1538. $user = get_user_by( 'id', get_current_user_id() );
  1539. $comment_author = $user->display_name;
  1540. $comment_author_email = $user->user_email;
  1541. } else {
  1542. $comment_author = __( 'WooCommerce', 'woocommerce' );
  1543. $comment_author_email = strtolower( __( 'WooCommerce', 'woocommerce' ) ) . '@';
  1544. $comment_author_email .= isset( $_SERVER['HTTP_HOST'] ) ? str_replace( 'www.', '', sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : 'noreply.com'; // WPCS: input var ok.
  1545. $comment_author_email = sanitize_email( $comment_author_email );
  1546. }
  1547. $commentdata = apply_filters(
  1548. 'woocommerce_new_order_note_data',
  1549. array(
  1550. 'comment_post_ID' => $this->get_id(),
  1551. 'comment_author' => $comment_author,
  1552. 'comment_author_email' => $comment_author_email,
  1553. 'comment_author_url' => '',
  1554. 'comment_content' => $note,
  1555. 'comment_agent' => 'WooCommerce',
  1556. 'comment_type' => 'order_note',
  1557. 'comment_parent' => 0,
  1558. 'comment_approved' => 1,
  1559. ),
  1560. array(
  1561. 'order_id' => $this->get_id(),
  1562. 'is_customer_note' => $is_customer_note,
  1563. )
  1564. );
  1565. $comment_id = wp_insert_comment( $commentdata );
  1566. if ( $is_customer_note ) {
  1567. add_comment_meta( $comment_id, 'is_customer_note', 1 );
  1568. do_action(
  1569. 'woocommerce_new_customer_note',
  1570. array(
  1571. 'order_id' => $this->get_id(),
  1572. 'customer_note' => $commentdata['comment_content'],
  1573. )
  1574. );
  1575. }
  1576. /**
  1577. * Action hook fired after an order note is added.
  1578. *
  1579. * @param int $order_note_id Order note ID.
  1580. * @param WC_Order $order Order data.
  1581. *
  1582. * @since 4.4.0
  1583. */
  1584. do_action( 'woocommerce_order_note_added', $comment_id, $this );
  1585. return $comment_id;
  1586. }
  1587. /**
  1588. * Add an order note for status transition
  1589. *
  1590. * @since 3.9.0
  1591. * @uses WC_Order::add_order_note()
  1592. * @param string $note Note to be added giving status transition from and to details.
  1593. * @param bool $transition Details of the status transition.
  1594. * @return int Comment ID.
  1595. */
  1596. private function add_status_transition_note( $note, $transition ) {
  1597. return $this->add_order_note( trim( $transition['note'] . ' ' . $note ), 0, $transition['manual'] );
  1598. }
  1599. /**
  1600. * List order notes (public) for the customer.
  1601. *
  1602. * @return array
  1603. */
  1604. public function get_customer_order_notes() {
  1605. $notes = array();
  1606. $args = array(
  1607. 'post_id' => $this->get_id(),
  1608. 'approve' => 'approve',
  1609. 'type' => '',
  1610. );
  1611. remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
  1612. $comments = get_comments( $args );
  1613. foreach ( $comments as $comment ) {
  1614. if ( ! get_comment_meta( $comment->comment_ID, 'is_customer_note', true ) ) {
  1615. continue;
  1616. }
  1617. $comment->comment_content = make_clickable( $comment->comment_content );
  1618. $notes[] = $comment;
  1619. }
  1620. add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ) );
  1621. return $notes;
  1622. }
  1623. /*
  1624. |--------------------------------------------------------------------------
  1625. | Refunds
  1626. |--------------------------------------------------------------------------
  1627. */
  1628. /**
  1629. * Get order refunds.
  1630. *
  1631. * @since 2.2
  1632. * @return array of WC_Order_Refund objects
  1633. */
  1634. public function get_refunds() {
  1635. $cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'refunds' . $this->get_id();
  1636. $cached_data = wp_cache_get( $cache_key, $this->cache_group );
  1637. if ( false !== $cached_data ) {
  1638. return $cached_data;
  1639. }
  1640. $this->refunds = wc_get_orders(
  1641. array(
  1642. 'type' => 'shop_order_refund',
  1643. 'parent' => $this->get_id(),
  1644. 'limit' => -1,
  1645. )
  1646. );
  1647. wp_cache_set( $cache_key, $this->refunds, $this->cache_group );
  1648. return $this->refunds;
  1649. }
  1650. /**
  1651. * Get amount already refunded.
  1652. *
  1653. * @since 2.2
  1654. * @return string
  1655. */
  1656. public function get_total_refunded() {
  1657. $cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'total_refunded' . $this->get_id();
  1658. $cached_data = wp_cache_get( $cache_key, $this->cache_group );
  1659. if ( false !== $cached_data ) {
  1660. return $cached_data;
  1661. }
  1662. $total_refunded = $this->data_store->get_total_refunded( $this );
  1663. wp_cache_set( $cache_key, $total_refunded, $this->cache_group );
  1664. return $total_refunded;
  1665. }
  1666. /**
  1667. * Get the total tax refunded.
  1668. *
  1669. * @since 2.3
  1670. * @return float
  1671. */
  1672. public function get_total_tax_refunded() {
  1673. $cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'total_tax_refunded' . $this->get_id();
  1674. $cached_data = wp_cache_get( $cache_key, $this->cache_group );
  1675. if ( false !== $cached_data ) {
  1676. return $cached_data;
  1677. }
  1678. $total_refunded = $this->data_store->get_total_tax_refunded( $this );
  1679. wp_cache_set( $cache_key, $total_refunded, $this->cache_group );
  1680. return $total_refunded;
  1681. }
  1682. /**
  1683. * Get the total shipping refunded.
  1684. *
  1685. * @since 2.4
  1686. * @return float
  1687. */
  1688. public function get_total_shipping_refunded() {
  1689. $cache_key = WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'total_shipping_refunded' . $this->get_id();
  1690. $cached_data = wp_cache_get( $cache_key, $this->cache_group );
  1691. if ( false !== $cached_data ) {
  1692. return $cached_data;
  1693. }
  1694. $total_refunded = $this->data_store->get_total_shipping_refunded( $this );
  1695. wp_cache_set( $cache_key, $total_refunded, $this->cache_group );
  1696. return $total_refunded;
  1697. }
  1698. /**
  1699. * Gets the count of order items of a certain type that have been refunded.
  1700. *
  1701. * @since 2.4.0
  1702. * @param string $item_type Item type.
  1703. * @return string
  1704. */
  1705. public function get_item_count_refunded( $item_type = '' ) {
  1706. if ( empty( $item_type ) ) {
  1707. $item_type = array( 'line_item' );
  1708. }
  1709. if ( ! is_array( $item_type ) ) {
  1710. $item_type = array( $item_type );
  1711. }
  1712. $count = 0;
  1713. foreach ( $this->get_refunds() as $refund ) {
  1714. foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
  1715. $count += abs( $refunded_item->get_quantity() );
  1716. }
  1717. }
  1718. return apply_filters( 'woocommerce_get_item_count_refunded', $count, $item_type, $this );
  1719. }
  1720. /**
  1721. * Get the total number of items refunded.
  1722. *
  1723. * @since 2.4.0
  1724. *
  1725. * @param string $item_type Type of the item we're checking, if not a line_item.
  1726. * @return int
  1727. */
  1728. public function get_total_qty_refunded( $item_type = 'line_item' ) {
  1729. $qty = 0;
  1730. foreach ( $this->get_refunds() as $refund ) {
  1731. foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
  1732. $qty += $refunded_item->get_quantity();
  1733. }
  1734. }
  1735. return $qty;
  1736. }
  1737. /**
  1738. * Get the refunded amount for a line item.
  1739. *
  1740. * @param int $item_id ID of the item we're checking.
  1741. * @param string $item_type Type of the item we're checking, if not a line_item.
  1742. * @return int
  1743. */
  1744. public function get_qty_refunded_for_item( $item_id, $item_type = 'line_item' ) {
  1745. $qty = 0;
  1746. foreach ( $this->get_refunds() as $refund ) {
  1747. foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
  1748. if ( absint( $refunded_item->get_meta( '_refunded_item_id' ) ) === $item_id ) {
  1749. $qty += $refunded_item->get_quantity();
  1750. }
  1751. }
  1752. }
  1753. return $qty;
  1754. }
  1755. /**
  1756. * Get the refunded amount for a line item.
  1757. *
  1758. * @param int $item_id ID of the item we're checking.
  1759. * @param string $item_type Type of the item we're checking, if not a line_item.
  1760. * @return int
  1761. */
  1762. public function get_total_refunded_for_item( $item_id, $item_type = 'line_item' ) {
  1763. $total = 0;
  1764. foreach ( $this->get_refunds() as $refund ) {
  1765. foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
  1766. if ( absint( $refunded_item->get_meta( '_refunded_item_id' ) ) === $item_id ) {
  1767. $total += $refunded_item->get_total();
  1768. }
  1769. }
  1770. }
  1771. return $total * -1;
  1772. }
  1773. /**
  1774. * Get the refunded tax amount for a line item.
  1775. *
  1776. * @param int $item_id ID of the item we're checking.
  1777. * @param int $tax_id ID of the tax we're checking.
  1778. * @param string $item_type Type of the item we're checking, if not a line_item.
  1779. * @return double
  1780. */
  1781. public function get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' ) {
  1782. $total = 0;
  1783. foreach ( $this->get_refunds() as $refund ) {
  1784. foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
  1785. $refunded_item_id = (int) $refunded_item->get_meta( '_refunded_item_id' );
  1786. if ( $refunded_item_id === $item_id ) {
  1787. $taxes = $refunded_item->get_taxes();
  1788. $total += isset( $taxes['total'][ $tax_id ] ) ? (float) $taxes['total'][ $tax_id ] : 0;
  1789. break;
  1790. }
  1791. }
  1792. }
  1793. return wc_round_tax_total( $total ) * -1;
  1794. }
  1795. /**
  1796. * Get total tax refunded by rate ID.
  1797. *
  1798. * @param int $rate_id Rate ID.
  1799. * @return float
  1800. */
  1801. public function get_total_tax_refunded_by_rate_id( $rate_id ) {
  1802. $total = 0;
  1803. foreach ( $this->get_refunds() as $refund ) {
  1804. foreach ( $refund->get_items( 'tax' ) as $refunded_item ) {
  1805. if ( absint( $refunded_item->get_rate_id() ) === $rate_id ) {
  1806. $total += abs( $refunded_item->get_tax_total() ) + abs( $refunded_item->get_shipping_tax_total() );
  1807. }
  1808. }
  1809. }
  1810. return $total;
  1811. }
  1812. /**
  1813. * How much money is left to refund?
  1814. *
  1815. * @return string
  1816. */
  1817. public function get_remaining_refund_amount() {
  1818. return wc_format_decimal( $this->get_total() - $this->get_total_refunded(), wc_get_price_decimals() );
  1819. }
  1820. /**
  1821. * How many items are left to refund?
  1822. *
  1823. * @return int
  1824. */
  1825. public function get_remaining_refund_items() {
  1826. return absint( $this->get_item_count() - $this->get_item_count_refunded() );
  1827. }
  1828. /**
  1829. * Add total row for the payment method.
  1830. *
  1831. * @param array $total_rows Total rows.
  1832. * @param string $tax_display Tax to display.
  1833. */
  1834. protected function add_order_item_totals_payment_method_row( &$total_rows, $tax_display ) {
  1835. if ( $this->get_total() > 0 && $this->get_payment_method_title() && 'other' !== $this->get_payment_method_title() ) {
  1836. $total_rows['payment_method'] = array(
  1837. 'label' => __( 'Payment method:', 'woocommerce' ),
  1838. 'value' => $this->get_payment_method_title(),
  1839. );
  1840. }
  1841. }
  1842. /**
  1843. * Add total row for refunds.
  1844. *
  1845. * @param array $total_rows Total rows.
  1846. * @param string $tax_display Tax to display.
  1847. */
  1848. protected function add_order_item_totals_refund_rows( &$total_rows, $tax_display ) {
  1849. $refunds = $this->get_refunds();
  1850. if ( $refunds ) {
  1851. foreach ( $refunds as $id => $refund ) {
  1852. $total_rows[ 'refund_' . $id ] = array(
  1853. 'label' => $refund->get_reason() ? $refund->get_reason() : __( 'Refund', 'woocommerce' ) . ':',
  1854. 'value' => wc_price( '-' . $refund->get_amount(), array( 'currency' => $this->get_currency() ) ),
  1855. );
  1856. }
  1857. }
  1858. }
  1859. /**
  1860. * Get totals for display on pages and in emails.
  1861. *
  1862. * @param string $tax_display Tax to display.
  1863. * @return array
  1864. */
  1865. public function get_order_item_totals( $tax_display = '' ) {
  1866. $tax_display = $tax_display ? $tax_display : get_option( 'woocommerce_tax_display_cart' );
  1867. $total_rows = array();
  1868. $this->add_order_item_totals_subtotal_row( $total_rows, $tax_display );
  1869. $this->add_order_item_totals_discount_row( $total_rows, $tax_display );
  1870. $this->add_order_item_totals_shipping_row( $total_rows, $tax_display );
  1871. $this->add_order_item_totals_fee_rows( $total_rows, $tax_display );
  1872. $this->add_order_item_totals_tax_rows( $total_rows, $tax_display );
  1873. $this->add_order_item_totals_payment_method_row( $total_rows, $tax_display );
  1874. $this->add_order_item_totals_refund_rows( $total_rows, $tax_display );
  1875. $this->add_order_item_totals_total_row( $total_rows, $tax_display );
  1876. return apply_filters( 'woocommerce_get_order_item_totals', $total_rows, $this, $tax_display );
  1877. }
  1878. /**
  1879. * Check if order has been created via admin, checkout, or in another way.
  1880. *
  1881. * @since 4.0.0
  1882. * @param string $modus Way of creating the order to test for.
  1883. * @return bool
  1884. */
  1885. public function is_created_via( $modus ) {
  1886. return apply_filters( 'woocommerce_order_is_created_via', $modus === $this->get_created_via(), $this, $modus );
  1887. }
  1888. }