abstract-wc-legacy-order.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * Legacy Abstract Order
  7. *
  8. * Legacy and deprecated functions are here to keep the WC_Abstract_Order clean.
  9. * This class will be removed in future versions.
  10. *
  11. * @version 3.0.0
  12. * @package WooCommerce/Abstracts
  13. * @category Abstract Class
  14. * @author WooThemes
  15. */
  16. abstract class WC_Abstract_Legacy_Order extends WC_Data {
  17. /**
  18. * Add coupon code to the order.
  19. * @param string|array $code
  20. * @param int $discount tax amount.
  21. * @param int $discount_tax amount.
  22. * @return int order item ID
  23. * @throws WC_Data_Exception
  24. */
  25. public function add_coupon( $code = array(), $discount = 0, $discount_tax = 0 ) {
  26. wc_deprecated_function( 'WC_Order::add_coupon', '3.0', 'a new WC_Order_Item_Coupon object and add to order with WC_Order::add_item()' );
  27. $item = new WC_Order_Item_Coupon();
  28. $item->set_props( array(
  29. 'code' => $code,
  30. 'discount' => $discount,
  31. 'discount_tax' => $discount_tax,
  32. 'order_id' => $this->get_id(),
  33. ) );
  34. $item->save();
  35. $this->add_item( $item );
  36. wc_do_deprecated_action( 'woocommerce_order_add_coupon', array( $this->get_id(), $item->get_id(), $code, $discount, $discount_tax ), '3.0', 'woocommerce_new_order_item action instead.' );
  37. return $item->get_id();
  38. }
  39. /**
  40. * Add a tax row to the order.
  41. * @param int $tax_rate_id
  42. * @param int $tax_amount amount of tax.
  43. * @param int $shipping_tax_amount shipping amount.
  44. * @return int order item ID
  45. * @throws WC_Data_Exception
  46. */
  47. public function add_tax( $tax_rate_id, $tax_amount = 0, $shipping_tax_amount = 0 ) {
  48. wc_deprecated_function( 'WC_Order::add_tax', '3.0', 'a new WC_Order_Item_Tax object and add to order with WC_Order::add_item()' );
  49. $item = new WC_Order_Item_Tax();
  50. $item->set_props( array(
  51. 'rate_id' => $tax_rate_id,
  52. 'tax_total' => $tax_amount,
  53. 'shipping_tax_total' => $shipping_tax_amount,
  54. ) );
  55. $item->set_rate( $tax_rate_id );
  56. $item->set_order_id( $this->get_id() );
  57. $item->save();
  58. $this->add_item( $item );
  59. wc_do_deprecated_action( 'woocommerce_order_add_tax', array( $this->get_id(), $item->get_id(), $tax_rate_id, $tax_amount, $shipping_tax_amount ), '3.0', 'woocommerce_new_order_item action instead.' );
  60. return $item->get_id();
  61. }
  62. /**
  63. * Add a shipping row to the order.
  64. * @param WC_Shipping_Rate shipping_rate
  65. * @return int order item ID
  66. * @throws WC_Data_Exception
  67. */
  68. public function add_shipping( $shipping_rate ) {
  69. wc_deprecated_function( 'WC_Order::add_shipping', '3.0', 'a new WC_Order_Item_Shipping object and add to order with WC_Order::add_item()' );
  70. $item = new WC_Order_Item_Shipping();
  71. $item->set_props( array(
  72. 'method_title' => $shipping_rate->label,
  73. 'method_id' => $shipping_rate->id,
  74. 'total' => wc_format_decimal( $shipping_rate->cost ),
  75. 'taxes' => $shipping_rate->taxes,
  76. 'order_id' => $this->get_id(),
  77. ) );
  78. foreach ( $shipping_rate->get_meta_data() as $key => $value ) {
  79. $item->add_meta_data( $key, $value, true );
  80. }
  81. $item->save();
  82. $this->add_item( $item );
  83. wc_do_deprecated_action( 'woocommerce_order_add_shipping', array( $this->get_id(), $item->get_id(), $shipping_rate ), '3.0', 'woocommerce_new_order_item action instead.' );
  84. return $item->get_id();
  85. }
  86. /**
  87. * Add a fee to the order.
  88. * Order must be saved prior to adding items.
  89. *
  90. * Fee is an amount of money charged for a particular piece of work
  91. * or for a particular right or service, and not supposed to be negative.
  92. *
  93. * @throws WC_Data_Exception
  94. * @param object $fee Fee data.
  95. * @return int Updated order item ID.
  96. */
  97. public function add_fee( $fee ) {
  98. wc_deprecated_function( 'WC_Order::add_fee', '3.0', 'a new WC_Order_Item_Fee object and add to order with WC_Order::add_item()' );
  99. $item = new WC_Order_Item_Fee();
  100. $item->set_props( array(
  101. 'name' => $fee->name,
  102. 'tax_class' => $fee->taxable ? $fee->tax_class : 0,
  103. 'total' => $fee->amount,
  104. 'total_tax' => $fee->tax,
  105. 'taxes' => array(
  106. 'total' => $fee->tax_data,
  107. ),
  108. 'order_id' => $this->get_id(),
  109. ) );
  110. $item->save();
  111. $this->add_item( $item );
  112. wc_do_deprecated_action( 'woocommerce_order_add_fee', array( $this->get_id(), $item->get_id(), $fee ), '3.0', 'woocommerce_new_order_item action instead.' );
  113. return $item->get_id();
  114. }
  115. /**
  116. * Update a line item for the order.
  117. *
  118. * Note this does not update order totals.
  119. *
  120. * @param object|int $item order item ID or item object.
  121. * @param WC_Product $product
  122. * @param array $args data to update.
  123. * @return int updated order item ID
  124. * @throws WC_Data_Exception
  125. */
  126. public function update_product( $item, $product, $args ) {
  127. wc_deprecated_function( 'WC_Order::update_product', '3.0', 'an interaction with the WC_Order_Item_Product class' );
  128. if ( is_numeric( $item ) ) {
  129. $item = $this->get_item( $item );
  130. }
  131. if ( ! is_object( $item ) || ! $item->is_type( 'line_item' ) ) {
  132. return false;
  133. }
  134. if ( ! $this->get_id() ) {
  135. $this->save(); // Order must exist
  136. }
  137. // BW compatibility with old args
  138. if ( isset( $args['totals'] ) ) {
  139. foreach ( $args['totals'] as $key => $value ) {
  140. if ( 'tax' === $key ) {
  141. $args['total_tax'] = $value;
  142. } elseif ( 'tax_data' === $key ) {
  143. $args['taxes'] = $value;
  144. } else {
  145. $args[ $key ] = $value;
  146. }
  147. }
  148. }
  149. // Handle qty if set.
  150. if ( isset( $args['qty'] ) ) {
  151. if ( $product->backorders_require_notification() && $product->is_on_backorder( $args['qty'] ) ) {
  152. $item->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ), $item ), $args['qty'] - max( 0, $product->get_stock_quantity() ), true );
  153. }
  154. $args['subtotal'] = $args['subtotal'] ? $args['subtotal'] : wc_get_price_excluding_tax( $product, array( 'qty' => $args['qty'] ) );
  155. $args['total'] = $args['total'] ? $args['total'] : wc_get_price_excluding_tax( $product, array( 'qty' => $args['qty'] ) );
  156. }
  157. $item->set_order_id( $this->get_id() );
  158. $item->set_props( $args );
  159. $item->save();
  160. do_action( 'woocommerce_order_edit_product', $this->get_id(), $item->get_id(), $args, $product );
  161. return $item->get_id();
  162. }
  163. /**
  164. * Update coupon for order. Note this does not update order totals.
  165. * @param object|int $item
  166. * @param array $args
  167. * @return int updated order item ID
  168. * @throws WC_Data_Exception
  169. */
  170. public function update_coupon( $item, $args ) {
  171. wc_deprecated_function( 'WC_Order::update_coupon', '3.0', 'an interaction with the WC_Order_Item_Coupon class' );
  172. if ( is_numeric( $item ) ) {
  173. $item = $this->get_item( $item );
  174. }
  175. if ( ! is_object( $item ) || ! $item->is_type( 'coupon' ) ) {
  176. return false;
  177. }
  178. if ( ! $this->get_id() ) {
  179. $this->save(); // Order must exist
  180. }
  181. // BW compatibility for old args
  182. if ( isset( $args['discount_amount'] ) ) {
  183. $args['discount'] = $args['discount_amount'];
  184. }
  185. if ( isset( $args['discount_amount_tax'] ) ) {
  186. $args['discount_tax'] = $args['discount_amount_tax'];
  187. }
  188. $item->set_order_id( $this->get_id() );
  189. $item->set_props( $args );
  190. $item->save();
  191. do_action( 'woocommerce_order_update_coupon', $this->get_id(), $item->get_id(), $args );
  192. return $item->get_id();
  193. }
  194. /**
  195. * Update shipping method for order.
  196. *
  197. * Note this does not update the order total.
  198. *
  199. * @param object|int $item
  200. * @param array $args
  201. * @return int updated order item ID
  202. * @throws WC_Data_Exception
  203. */
  204. public function update_shipping( $item, $args ) {
  205. wc_deprecated_function( 'WC_Order::update_shipping', '3.0', 'an interaction with the WC_Order_Item_Shipping class' );
  206. if ( is_numeric( $item ) ) {
  207. $item = $this->get_item( $item );
  208. }
  209. if ( ! is_object( $item ) || ! $item->is_type( 'shipping' ) ) {
  210. return false;
  211. }
  212. if ( ! $this->get_id() ) {
  213. $this->save(); // Order must exist
  214. }
  215. // BW compatibility for old args
  216. if ( isset( $args['cost'] ) ) {
  217. $args['total'] = $args['cost'];
  218. }
  219. $item->set_order_id( $this->get_id() );
  220. $item->set_props( $args );
  221. $item->save();
  222. $this->calculate_shipping();
  223. do_action( 'woocommerce_order_update_shipping', $this->get_id(), $item->get_id(), $args );
  224. return $item->get_id();
  225. }
  226. /**
  227. * Update fee for order.
  228. *
  229. * Note this does not update order totals.
  230. *
  231. * @param object|int $item
  232. * @param array $args
  233. * @return int updated order item ID
  234. * @throws WC_Data_Exception
  235. */
  236. public function update_fee( $item, $args ) {
  237. wc_deprecated_function( 'WC_Order::update_fee', '3.0', 'an interaction with the WC_Order_Item_Fee class' );
  238. if ( is_numeric( $item ) ) {
  239. $item = $this->get_item( $item );
  240. }
  241. if ( ! is_object( $item ) || ! $item->is_type( 'fee' ) ) {
  242. return false;
  243. }
  244. if ( ! $this->get_id() ) {
  245. $this->save(); // Order must exist
  246. }
  247. $item->set_order_id( $this->get_id() );
  248. $item->set_props( $args );
  249. $item->save();
  250. do_action( 'woocommerce_order_update_fee', $this->get_id(), $item->get_id(), $args );
  251. return $item->get_id();
  252. }
  253. /**
  254. * Update tax line on order.
  255. * Note this does not update order totals.
  256. *
  257. * @since 3.0
  258. * @param object|int $item
  259. * @param array $args
  260. * @return int updated order item ID
  261. * @throws WC_Data_Exception
  262. */
  263. public function update_tax( $item, $args ) {
  264. wc_deprecated_function( 'WC_Order::update_tax', '3.0', 'an interaction with the WC_Order_Item_Tax class' );
  265. if ( is_numeric( $item ) ) {
  266. $item = $this->get_item( $item );
  267. }
  268. if ( ! is_object( $item ) || ! $item->is_type( 'tax' ) ) {
  269. return false;
  270. }
  271. if ( ! $this->get_id() ) {
  272. $this->save(); // Order must exist
  273. }
  274. $item->set_order_id( $this->get_id() );
  275. $item->set_props( $args );
  276. $item->save();
  277. do_action( 'woocommerce_order_update_tax', $this->get_id(), $item->get_id(), $args );
  278. return $item->get_id();
  279. }
  280. /**
  281. * Get a product (either product or variation).
  282. * @deprecated Add deprecation notices in future release. Replaced with $item->get_product()
  283. * @param object $item
  284. * @return WC_Product|bool
  285. */
  286. public function get_product_from_item( $item ) {
  287. if ( is_callable( array( $item, 'get_product' ) ) ) {
  288. $product = $item->get_product();
  289. } else {
  290. $product = false;
  291. }
  292. return apply_filters( 'woocommerce_get_product_from_item', $product, $item, $this );
  293. }
  294. /**
  295. * Set the customer address.
  296. * @param array $address Address data.
  297. * @param string $type billing or shipping.
  298. */
  299. public function set_address( $address, $type = 'billing' ) {
  300. foreach ( $address as $key => $value ) {
  301. update_post_meta( $this->get_id(), "_{$type}_" . $key, $value );
  302. if ( is_callable( array( $this, "set_{$type}_{$key}" ) ) ) {
  303. $this->{"set_{$type}_{$key}"}( $value );
  304. }
  305. }
  306. }
  307. /**
  308. * Set an order total.
  309. * @param float $amount
  310. * @param string $total_type
  311. * @return bool
  312. */
  313. public function legacy_set_total( $amount, $total_type = 'total' ) {
  314. if ( ! in_array( $total_type, array( 'shipping', 'tax', 'shipping_tax', 'total', 'cart_discount', 'cart_discount_tax' ) ) ) {
  315. return false;
  316. }
  317. switch ( $total_type ) {
  318. case 'total' :
  319. $amount = wc_format_decimal( $amount, wc_get_price_decimals() );
  320. $this->set_total( $amount );
  321. update_post_meta( $this->get_id(), '_order_total', $amount );
  322. break;
  323. case 'cart_discount' :
  324. $amount = wc_format_decimal( $amount );
  325. $this->set_discount_total( $amount );
  326. update_post_meta( $this->get_id(), '_cart_discount', $amount );
  327. break;
  328. case 'cart_discount_tax' :
  329. $amount = wc_format_decimal( $amount );
  330. $this->set_discount_tax( $amount );
  331. update_post_meta( $this->get_id(), '_cart_discount_tax', $amount );
  332. break;
  333. case 'shipping' :
  334. $amount = wc_format_decimal( $amount );
  335. $this->set_shipping_total( $amount );
  336. update_post_meta( $this->get_id(), '_order_shipping', $amount );
  337. break;
  338. case 'shipping_tax' :
  339. $amount = wc_format_decimal( $amount );
  340. $this->set_shipping_tax( $amount );
  341. update_post_meta( $this->get_id(), '_order_shipping_tax', $amount );
  342. break;
  343. case 'tax' :
  344. $amount = wc_format_decimal( $amount );
  345. $this->set_cart_tax( $amount );
  346. update_post_meta( $this->get_id(), '_order_tax', $amount );
  347. break;
  348. }
  349. return true;
  350. }
  351. /**
  352. * Magic __isset method for backwards compatibility. Handles legacy properties which could be accessed directly in the past.
  353. *
  354. * @param string $key
  355. * @return bool
  356. */
  357. public function __isset( $key ) {
  358. $legacy_props = array( 'completed_date', 'id', 'order_type', 'post', 'status', 'post_status', 'customer_note', 'customer_message', 'user_id', 'customer_user', 'prices_include_tax', 'tax_display_cart', 'display_totals_ex_tax', 'display_cart_ex_tax', 'order_date', 'modified_date', 'cart_discount', 'cart_discount_tax', 'order_shipping', 'order_shipping_tax', 'order_total', 'order_tax', 'billing_first_name', 'billing_last_name', 'billing_company', 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_state', 'billing_postcode', 'billing_country', 'billing_phone', 'billing_email', 'shipping_first_name', 'shipping_last_name', 'shipping_company', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_state', 'shipping_postcode', 'shipping_country', 'customer_ip_address', 'customer_user_agent', 'payment_method_title', 'payment_method', 'order_currency' );
  359. return $this->get_id() ? ( in_array( $key, $legacy_props ) || metadata_exists( 'post', $this->get_id(), '_' . $key ) ) : false;
  360. }
  361. /**
  362. * Magic __get method for backwards compatibility.
  363. *
  364. * @param string $key
  365. * @return mixed
  366. */
  367. public function __get( $key ) {
  368. wc_doing_it_wrong( $key, 'Order properties should not be accessed directly.', '3.0' );
  369. if ( 'completed_date' === $key ) {
  370. return $this->get_date_completed() ? gmdate( 'Y-m-d H:i:s', $this->get_date_completed()->getOffsetTimestamp() ) : '';
  371. } elseif ( 'paid_date' === $key ) {
  372. return $this->get_date_paid() ? gmdate( 'Y-m-d H:i:s', $this->get_date_paid()->getOffsetTimestamp() ) : '';
  373. } elseif ( 'modified_date' === $key ) {
  374. return $this->get_date_modified() ? gmdate( 'Y-m-d H:i:s', $this->get_date_modified()->getOffsetTimestamp() ) : '';
  375. } elseif ( 'order_date' === $key ) {
  376. return $this->get_date_created() ? gmdate( 'Y-m-d H:i:s', $this->get_date_created()->getOffsetTimestamp() ) : '';
  377. } elseif ( 'id' === $key ) {
  378. return $this->get_id();
  379. } elseif ( 'post' === $key ) {
  380. return get_post( $this->get_id() );
  381. } elseif ( 'status' === $key ) {
  382. return $this->get_status();
  383. } elseif ( 'post_status' === $key ) {
  384. return get_post_status( $this->get_id() );
  385. } elseif ( 'customer_message' === $key || 'customer_note' === $key ) {
  386. return $this->get_customer_note();
  387. } elseif ( in_array( $key, array( 'user_id', 'customer_user' ) ) ) {
  388. return $this->get_customer_id();
  389. } elseif ( 'tax_display_cart' === $key ) {
  390. return get_option( 'woocommerce_tax_display_cart' );
  391. } elseif ( 'display_totals_ex_tax' === $key ) {
  392. return 'excl' === get_option( 'woocommerce_tax_display_cart' );
  393. } elseif ( 'display_cart_ex_tax' === $key ) {
  394. return 'excl' === get_option( 'woocommerce_tax_display_cart' );
  395. } elseif ( 'cart_discount' === $key ) {
  396. return $this->get_total_discount();
  397. } elseif ( 'cart_discount_tax' === $key ) {
  398. return $this->get_discount_tax();
  399. } elseif ( 'order_tax' === $key ) {
  400. return $this->get_cart_tax();
  401. } elseif ( 'order_shipping_tax' === $key ) {
  402. return $this->get_shipping_tax();
  403. } elseif ( 'order_shipping' === $key ) {
  404. return $this->get_shipping_total();
  405. } elseif ( 'order_total' === $key ) {
  406. return $this->get_total();
  407. } elseif ( 'order_type' === $key ) {
  408. return $this->get_type();
  409. } elseif ( 'order_currency' === $key ) {
  410. return $this->get_currency();
  411. } elseif ( 'order_version' === $key ) {
  412. return $this->get_version();
  413. } elseif ( is_callable( array( $this, "get_{$key}" ) ) ) {
  414. return $this->{"get_{$key}"}();
  415. } else {
  416. return get_post_meta( $this->get_id(), '_' . $key, true );
  417. }
  418. }
  419. /**
  420. * has_meta function for order items. This is different to the WC_Data
  421. * version and should be removed in future versions.
  422. *
  423. * @deprecated
  424. *
  425. * @param int $order_item_id
  426. *
  427. * @return array of meta data.
  428. */
  429. public function has_meta( $order_item_id ) {
  430. global $wpdb;
  431. wc_deprecated_function( 'WC_Order::has_meta( $order_item_id )', '3.0', 'WC_Order_item::get_meta_data' );
  432. return $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value, meta_id, order_item_id
  433. FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d
  434. ORDER BY meta_id", absint( $order_item_id ) ), ARRAY_A );
  435. }
  436. /**
  437. * Display meta data belonging to an item.
  438. * @param array $item
  439. */
  440. public function display_item_meta( $item ) {
  441. wc_deprecated_function( 'WC_Order::display_item_meta', '3.0', 'wc_display_item_meta' );
  442. $product = $item->get_product();
  443. $item_meta = new WC_Order_Item_Meta( $item, $product );
  444. $item_meta->display();
  445. }
  446. /**
  447. * Display download links for an order item.
  448. * @param array $item
  449. */
  450. public function display_item_downloads( $item ) {
  451. wc_deprecated_function( 'WC_Order::display_item_downloads', '3.0', 'wc_display_item_downloads' );
  452. $product = $item->get_product();
  453. if ( $product && $product->exists() && $product->is_downloadable() && $this->is_download_permitted() ) {
  454. $download_files = $this->get_item_downloads( $item );
  455. $i = 0;
  456. $links = array();
  457. foreach ( $download_files as $download_id => $file ) {
  458. $i++;
  459. /* translators: 1: current item count */
  460. $prefix = count( $download_files ) > 1 ? sprintf( __( 'Download %d', 'woocommerce' ), $i ) : __( 'Download', 'woocommerce' );
  461. $links[] = '<small class="download-url">' . $prefix . ': <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a></small>' . "\n";
  462. }
  463. echo '<br/>' . implode( '<br/>', $links );
  464. }
  465. }
  466. /**
  467. * Get the Download URL.
  468. *
  469. * @param int $product_id
  470. * @param int $download_id
  471. * @return string
  472. */
  473. public function get_download_url( $product_id, $download_id ) {
  474. wc_deprecated_function( 'WC_Order::get_download_url', '3.0', 'WC_Order_Item_Product::get_item_download_url' );
  475. return add_query_arg( array(
  476. 'download_file' => $product_id,
  477. 'order' => $this->get_order_key(),
  478. 'email' => urlencode( $this->get_billing_email() ),
  479. 'key' => $download_id,
  480. ), trailingslashit( home_url() ) );
  481. }
  482. /**
  483. * Get the downloadable files for an item in this order.
  484. *
  485. * @param array $item
  486. * @return array
  487. */
  488. public function get_item_downloads( $item ) {
  489. wc_deprecated_function( 'WC_Order::get_item_downloads', '3.0', 'WC_Order_Item_Product::get_item_downloads' );
  490. if ( ! $item instanceof WC_Order_Item ) {
  491. if ( ! empty( $item['variation_id'] ) ) {
  492. $product_id = $item['variation_id'];
  493. } elseif ( ! empty( $item['product_id'] ) ) {
  494. $product_id = $item['product_id'];
  495. } else {
  496. return array();
  497. }
  498. // Create a 'virtual' order item to allow retrieving item downloads when
  499. // an array of product_id is passed instead of actual order item.
  500. $item = new WC_Order_Item_Product();
  501. $item->set_product( wc_get_product( $product_id ) );
  502. $item->set_order_id( $this->get_id() );
  503. }
  504. return $item->get_item_downloads();
  505. }
  506. /**
  507. * Gets shipping total. Alias of WC_Order::get_shipping_total().
  508. * @deprecated 3.0.0 since this is an alias only.
  509. * @return float
  510. */
  511. public function get_total_shipping() {
  512. return $this->get_shipping_total();
  513. }
  514. /**
  515. * Get order item meta.
  516. * @deprecated 3.0.0
  517. * @param mixed $order_item_id
  518. * @param string $key (default: '')
  519. * @param bool $single (default: false)
  520. * @return array|string
  521. */
  522. public function get_item_meta( $order_item_id, $key = '', $single = false ) {
  523. wc_deprecated_function( 'WC_Order::get_item_meta', '3.0', 'wc_get_order_item_meta' );
  524. return get_metadata( 'order_item', $order_item_id, $key, $single );
  525. }
  526. /**
  527. * Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta().
  528. *
  529. * @param mixed $order_item_id
  530. * @return array of objects
  531. */
  532. public function get_item_meta_array( $order_item_id ) {
  533. wc_deprecated_function( 'WC_Order::get_item_meta_array', '3.0', 'WC_Order_Item::get_meta_data() (note the format has changed)' );
  534. $item = $this->get_item( $order_item_id );
  535. $meta_data = $item->get_meta_data();
  536. $item_meta_array = array();
  537. foreach ( $meta_data as $meta ) {
  538. $item_meta_array[ $meta->id ] = $meta;
  539. }
  540. return $item_meta_array;
  541. }
  542. /**
  543. * Expand item meta into the $item array.
  544. * @deprecated 3.0.0 Item meta no longer expanded due to new order item
  545. * classes. This function now does nothing to avoid data breakage.
  546. * @param array $item before expansion.
  547. * @return array
  548. */
  549. public function expand_item_meta( $item ) {
  550. wc_deprecated_function( 'WC_Order::expand_item_meta', '3.0' );
  551. return $item;
  552. }
  553. /**
  554. * Load the order object. Called from the constructor.
  555. * @deprecated 3.0.0 Logic moved to constructor
  556. * @param int|object|WC_Order $order Order to init.
  557. */
  558. protected function init( $order ) {
  559. wc_deprecated_function( 'WC_Order::init', '3.0', 'Logic moved to constructor' );
  560. if ( is_numeric( $order ) ) {
  561. $this->set_id( $order );
  562. } elseif ( $order instanceof WC_Order ) {
  563. $this->set_id( absint( $order->get_id() ) );
  564. } elseif ( isset( $order->ID ) ) {
  565. $this->set_id( absint( $order->ID ) );
  566. }
  567. $this->set_object_read( false );
  568. $this->data_store->read( $this );
  569. }
  570. /**
  571. * Gets an order from the database.
  572. * @deprecated 3.0
  573. * @param int $id (default: 0).
  574. * @return bool
  575. */
  576. public function get_order( $id = 0 ) {
  577. wc_deprecated_function( 'WC_Order::get_order', '3.0' );
  578. if ( ! $id ) {
  579. return false;
  580. }
  581. $result = get_post( $id );
  582. if ( $result ) {
  583. $this->populate( $result );
  584. return true;
  585. }
  586. return false;
  587. }
  588. /**
  589. * Populates an order from the loaded post data.
  590. * @deprecated 3.0
  591. * @param mixed $result
  592. */
  593. public function populate( $result ) {
  594. wc_deprecated_function( 'WC_Order::populate', '3.0' );
  595. $this->set_id( $result->ID );
  596. $this->set_object_read( false );
  597. $this->data_store->read( $this );
  598. }
  599. /**
  600. * Cancel the order and restore the cart (before payment).
  601. * @deprecated 3.0.0 Moved to event handler.
  602. * @param string $note (default: '') Optional note to add.
  603. */
  604. public function cancel_order( $note = '' ) {
  605. wc_deprecated_function( 'WC_Order::cancel_order', '3.0', 'WC_Order::update_status' );
  606. WC()->session->set( 'order_awaiting_payment', false );
  607. $this->update_status( 'cancelled', $note );
  608. }
  609. /**
  610. * Record sales.
  611. * @deprecated 3.0.0
  612. */
  613. public function record_product_sales() {
  614. wc_deprecated_function( 'WC_Order::record_product_sales', '3.0', 'wc_update_total_sales_counts' );
  615. wc_update_total_sales_counts( $this->get_id() );
  616. }
  617. /**
  618. * Increase applied coupon counts.
  619. * @deprecated 3.0.0
  620. */
  621. public function increase_coupon_usage_counts() {
  622. wc_deprecated_function( 'WC_Order::increase_coupon_usage_counts', '3.0', 'wc_update_coupon_usage_counts' );
  623. wc_update_coupon_usage_counts( $this->get_id() );
  624. }
  625. /**
  626. * Decrease applied coupon counts.
  627. * @deprecated 3.0.0
  628. */
  629. public function decrease_coupon_usage_counts() {
  630. wc_deprecated_function( 'WC_Order::decrease_coupon_usage_counts', '3.0', 'wc_update_coupon_usage_counts' );
  631. wc_update_coupon_usage_counts( $this->get_id() );
  632. }
  633. /**
  634. * Reduce stock levels for all line items in the order.
  635. * @deprecated 3.0.0
  636. */
  637. public function reduce_order_stock() {
  638. wc_deprecated_function( 'WC_Order::reduce_order_stock', '3.0', 'wc_reduce_stock_levels' );
  639. wc_reduce_stock_levels( $this->get_id() );
  640. }
  641. /**
  642. * Send the stock notifications.
  643. * @deprecated 3.0.0 No longer needs to be called directly.
  644. *
  645. * @param $product
  646. * @param $new_stock
  647. * @param $qty_ordered
  648. */
  649. public function send_stock_notifications( $product, $new_stock, $qty_ordered ) {
  650. wc_deprecated_function( 'WC_Order::send_stock_notifications', '3.0' );
  651. }
  652. /**
  653. * Output items for display in html emails.
  654. * @deprecated 3.0.0 Moved to template functions.
  655. * @param array $args Items args.
  656. * @return string
  657. */
  658. public function email_order_items_table( $args = array() ) {
  659. wc_deprecated_function( 'WC_Order::email_order_items_table', '3.0', 'wc_get_email_order_items' );
  660. return wc_get_email_order_items( $this, $args );
  661. }
  662. /**
  663. * Get currency.
  664. * @deprecated 3.0.0
  665. */
  666. public function get_order_currency() {
  667. wc_deprecated_function( 'WC_Order::get_order_currency', '3.0', 'WC_Order::get_currency' );
  668. return apply_filters( 'woocommerce_get_order_currency', $this->get_currency(), $this );
  669. }
  670. }