class-wc-order-item-product.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /**
  3. * Order Line Item (product)
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.0.0
  7. * @since 3.0.0
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Order item product class.
  12. */
  13. class WC_Order_Item_Product extends WC_Order_Item {
  14. /**
  15. * Order Data array. This is the core order data exposed in APIs since 3.0.0.
  16. *
  17. * @since 3.0.0
  18. * @var array
  19. */
  20. protected $extra_data = array(
  21. 'product_id' => 0,
  22. 'variation_id' => 0,
  23. 'quantity' => 1,
  24. 'tax_class' => '',
  25. 'subtotal' => 0,
  26. 'subtotal_tax' => 0,
  27. 'total' => 0,
  28. 'total_tax' => 0,
  29. 'taxes' => array(
  30. 'subtotal' => array(),
  31. 'total' => array(),
  32. ),
  33. );
  34. /*
  35. |--------------------------------------------------------------------------
  36. | Setters
  37. |--------------------------------------------------------------------------
  38. */
  39. /**
  40. * Set quantity.
  41. *
  42. * @param int $value Quantity.
  43. */
  44. public function set_quantity( $value ) {
  45. $this->set_prop( 'quantity', wc_stock_amount( $value ) );
  46. }
  47. /**
  48. * Set tax class.
  49. *
  50. * @param string $value Tax class.
  51. */
  52. public function set_tax_class( $value ) {
  53. if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs(), true ) ) {
  54. $this->error( 'order_item_product_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
  55. }
  56. $this->set_prop( 'tax_class', $value );
  57. }
  58. /**
  59. * Set Product ID
  60. *
  61. * @param int $value Product ID.
  62. */
  63. public function set_product_id( $value ) {
  64. if ( $value > 0 && 'product' !== get_post_type( absint( $value ) ) ) {
  65. $this->error( 'order_item_product_invalid_product_id', __( 'Invalid product ID', 'woocommerce' ) );
  66. }
  67. $this->set_prop( 'product_id', absint( $value ) );
  68. }
  69. /**
  70. * Set variation ID.
  71. *
  72. * @param int $value Variation ID.
  73. */
  74. public function set_variation_id( $value ) {
  75. if ( $value > 0 && 'product_variation' !== get_post_type( $value ) ) {
  76. $this->error( 'order_item_product_invalid_variation_id', __( 'Invalid variation ID', 'woocommerce' ) );
  77. }
  78. $this->set_prop( 'variation_id', absint( $value ) );
  79. }
  80. /**
  81. * Line subtotal (before discounts).
  82. *
  83. * @param string $value Subtotal.
  84. */
  85. public function set_subtotal( $value ) {
  86. $value = wc_format_decimal( $value );
  87. if ( ! is_numeric( $value ) ) {
  88. $value = 0;
  89. }
  90. $this->set_prop( 'subtotal', $value );
  91. }
  92. /**
  93. * Line total (after discounts).
  94. *
  95. * @param string $value Total.
  96. */
  97. public function set_total( $value ) {
  98. $value = wc_format_decimal( $value );
  99. if ( ! is_numeric( $value ) ) {
  100. $value = 0;
  101. }
  102. $this->set_prop( 'total', $value );
  103. // Subtotal cannot be less than total.
  104. if ( '' === $this->get_subtotal() || $this->get_subtotal() < $this->get_total() ) {
  105. $this->set_subtotal( $value );
  106. }
  107. }
  108. /**
  109. * Line subtotal tax (before discounts).
  110. *
  111. * @param string $value Subtotal tax.
  112. */
  113. public function set_subtotal_tax( $value ) {
  114. $this->set_prop( 'subtotal_tax', wc_format_decimal( $value ) );
  115. }
  116. /**
  117. * Line total tax (after discounts).
  118. *
  119. * @param string $value Total tax.
  120. */
  121. public function set_total_tax( $value ) {
  122. $this->set_prop( 'total_tax', wc_format_decimal( $value ) );
  123. }
  124. /**
  125. * Set line taxes and totals for passed in taxes.
  126. *
  127. * @param array $raw_tax_data Raw tax data.
  128. */
  129. public function set_taxes( $raw_tax_data ) {
  130. $raw_tax_data = maybe_unserialize( $raw_tax_data );
  131. $tax_data = array(
  132. 'total' => array(),
  133. 'subtotal' => array(),
  134. );
  135. if ( ! empty( $raw_tax_data['total'] ) && ! empty( $raw_tax_data['subtotal'] ) ) {
  136. $tax_data['subtotal'] = array_map( 'wc_format_decimal', $raw_tax_data['subtotal'] );
  137. $tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
  138. // Subtotal cannot be less than total!
  139. if ( array_sum( $tax_data['subtotal'] ) < array_sum( $tax_data['total'] ) ) {
  140. $tax_data['subtotal'] = $tax_data['total'];
  141. }
  142. }
  143. $this->set_prop( 'taxes', $tax_data );
  144. $this->set_total_tax( array_sum( $tax_data['total'] ) );
  145. $this->set_subtotal_tax( array_sum( $tax_data['subtotal'] ) );
  146. }
  147. /**
  148. * Set variation data (stored as meta data - write only).
  149. *
  150. * @param array $data Key/Value pairs.
  151. */
  152. public function set_variation( $data = array() ) {
  153. if ( is_array( $data ) ) {
  154. foreach ( $data as $key => $value ) {
  155. $this->add_meta_data( str_replace( 'attribute_', '', $key ), $value, true );
  156. }
  157. }
  158. }
  159. /**
  160. * Set properties based on passed in product object.
  161. *
  162. * @param WC_Product $product Product instance.
  163. */
  164. public function set_product( $product ) {
  165. if ( ! is_a( $product, 'WC_Product' ) ) {
  166. $this->error( 'order_item_product_invalid_product', __( 'Invalid product', 'woocommerce' ) );
  167. }
  168. if ( $product->is_type( 'variation' ) ) {
  169. $this->set_product_id( $product->get_parent_id() );
  170. $this->set_variation_id( $product->get_id() );
  171. $this->set_variation( is_callable( array( $product, 'get_variation_attributes' ) ) ? $product->get_variation_attributes() : array() );
  172. } else {
  173. $this->set_product_id( $product->get_id() );
  174. }
  175. $this->set_name( $product->get_name() );
  176. $this->set_tax_class( $product->get_tax_class() );
  177. }
  178. /**
  179. * Set meta data for backordered products.
  180. */
  181. public function set_backorder_meta() {
  182. $product = $this->get_product();
  183. if ( $product && $product->backorders_require_notification() && $product->is_on_backorder( $this->get_quantity() ) ) {
  184. $this->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ), $this ), $this->get_quantity() - max( 0, $product->get_stock_quantity() ), true );
  185. }
  186. }
  187. /*
  188. |--------------------------------------------------------------------------
  189. | Getters
  190. |--------------------------------------------------------------------------
  191. */
  192. /**
  193. * Get order item type.
  194. *
  195. * @return string
  196. */
  197. public function get_type() {
  198. return 'line_item';
  199. }
  200. /**
  201. * Get product ID.
  202. *
  203. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  204. * @return int
  205. */
  206. public function get_product_id( $context = 'view' ) {
  207. return $this->get_prop( 'product_id', $context );
  208. }
  209. /**
  210. * Get variation ID.
  211. *
  212. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  213. * @return int
  214. */
  215. public function get_variation_id( $context = 'view' ) {
  216. return $this->get_prop( 'variation_id', $context );
  217. }
  218. /**
  219. * Get quantity.
  220. *
  221. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  222. * @return int
  223. */
  224. public function get_quantity( $context = 'view' ) {
  225. return $this->get_prop( 'quantity', $context );
  226. }
  227. /**
  228. * Get tax class.
  229. *
  230. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  231. * @return string
  232. */
  233. public function get_tax_class( $context = 'view' ) {
  234. return $this->get_prop( 'tax_class', $context );
  235. }
  236. /**
  237. * Get subtotal.
  238. *
  239. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  240. * @return string
  241. */
  242. public function get_subtotal( $context = 'view' ) {
  243. return $this->get_prop( 'subtotal', $context );
  244. }
  245. /**
  246. * Get subtotal tax.
  247. *
  248. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  249. * @return string
  250. */
  251. public function get_subtotal_tax( $context = 'view' ) {
  252. return $this->get_prop( 'subtotal_tax', $context );
  253. }
  254. /**
  255. * Get total.
  256. *
  257. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  258. * @return string
  259. */
  260. public function get_total( $context = 'view' ) {
  261. return $this->get_prop( 'total', $context );
  262. }
  263. /**
  264. * Get total tax.
  265. *
  266. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  267. * @return string
  268. */
  269. public function get_total_tax( $context = 'view' ) {
  270. return $this->get_prop( 'total_tax', $context );
  271. }
  272. /**
  273. * Get taxes.
  274. *
  275. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  276. * @return array
  277. */
  278. public function get_taxes( $context = 'view' ) {
  279. return $this->get_prop( 'taxes', $context );
  280. }
  281. /**
  282. * Get the associated product.
  283. *
  284. * @return WC_Product|bool
  285. */
  286. public function get_product() {
  287. if ( $this->get_variation_id() ) {
  288. $product = wc_get_product( $this->get_variation_id() );
  289. } else {
  290. $product = wc_get_product( $this->get_product_id() );
  291. }
  292. // Backwards compatible filter from WC_Order::get_product_from_item().
  293. if ( has_filter( 'woocommerce_get_product_from_item' ) ) {
  294. $product = apply_filters( 'woocommerce_get_product_from_item', $product, $this, $this->get_order() );
  295. }
  296. return apply_filters( 'woocommerce_order_item_product', $product, $this );
  297. }
  298. /**
  299. * Get the Download URL.
  300. *
  301. * @param int $download_id Download ID.
  302. * @return string
  303. */
  304. public function get_item_download_url( $download_id ) {
  305. $order = $this->get_order();
  306. return $order ? add_query_arg(
  307. array(
  308. 'download_file' => $this->get_variation_id() ? $this->get_variation_id() : $this->get_product_id(),
  309. 'order' => $order->get_order_key(),
  310. 'email' => rawurlencode( $order->get_billing_email() ),
  311. 'key' => $download_id,
  312. ), trailingslashit( home_url() )
  313. ) : '';
  314. }
  315. /**
  316. * Get any associated downloadable files.
  317. *
  318. * @return array
  319. */
  320. public function get_item_downloads() {
  321. $files = array();
  322. $product = $this->get_product();
  323. $order = $this->get_order();
  324. $product_id = $this->get_variation_id() ? $this->get_variation_id() : $this->get_product_id();
  325. $email_hash = function_exists( 'hash' ) ? hash( 'sha256', $order->get_billing_email() ) : sha1( $order->get_billing_email() );
  326. if ( $product && $order && $product->is_downloadable() && $order->is_download_permitted() ) {
  327. $data_store = WC_Data_Store::load( 'customer-download' );
  328. $customer_downloads = $data_store->get_downloads(
  329. array(
  330. 'user_email' => $order->get_billing_email(),
  331. 'order_id' => $order->get_id(),
  332. 'product_id' => $product_id,
  333. )
  334. );
  335. foreach ( $customer_downloads as $customer_download ) {
  336. $download_id = $customer_download->get_download_id();
  337. if ( $product->has_file( $download_id ) ) {
  338. $file = $product->get_file( $download_id );
  339. $files[ $download_id ] = $file->get_data();
  340. $files[ $download_id ]['downloads_remaining'] = $customer_download->get_downloads_remaining();
  341. $files[ $download_id ]['access_expires'] = $customer_download->get_access_expires();
  342. $files[ $download_id ]['download_url'] = add_query_arg(
  343. array(
  344. 'download_file' => $product_id,
  345. 'order' => $order->get_order_key(),
  346. 'uid' => $email_hash,
  347. 'key' => $download_id,
  348. ), trailingslashit( home_url() )
  349. );
  350. }
  351. }
  352. }
  353. return apply_filters( 'woocommerce_get_item_downloads', $files, $this, $order );
  354. }
  355. /**
  356. * Get tax status.
  357. *
  358. * @return string
  359. */
  360. public function get_tax_status() {
  361. $product = $this->get_product();
  362. return $product ? $product->get_tax_status() : 'taxable';
  363. }
  364. /*
  365. |--------------------------------------------------------------------------
  366. | Array Access Methods
  367. |--------------------------------------------------------------------------
  368. |
  369. | For backwards compatibility with legacy arrays.
  370. |
  371. */
  372. /**
  373. * OffsetGet for ArrayAccess/Backwards compatibility.
  374. *
  375. * @deprecated Add deprecation notices in future release.
  376. * @param string $offset Offset.
  377. * @return mixed
  378. */
  379. public function offsetGet( $offset ) {
  380. if ( 'line_subtotal' === $offset ) {
  381. $offset = 'subtotal';
  382. } elseif ( 'line_subtotal_tax' === $offset ) {
  383. $offset = 'subtotal_tax';
  384. } elseif ( 'line_total' === $offset ) {
  385. $offset = 'total';
  386. } elseif ( 'line_tax' === $offset ) {
  387. $offset = 'total_tax';
  388. } elseif ( 'line_tax_data' === $offset ) {
  389. $offset = 'taxes';
  390. } elseif ( 'qty' === $offset ) {
  391. $offset = 'quantity';
  392. }
  393. return parent::offsetGet( $offset );
  394. }
  395. /**
  396. * OffsetSet for ArrayAccess/Backwards compatibility.
  397. *
  398. * @deprecated Add deprecation notices in future release.
  399. * @param string $offset Offset.
  400. * @param mixed $value Value.
  401. */
  402. public function offsetSet( $offset, $value ) {
  403. if ( 'line_subtotal' === $offset ) {
  404. $offset = 'subtotal';
  405. } elseif ( 'line_subtotal_tax' === $offset ) {
  406. $offset = 'subtotal_tax';
  407. } elseif ( 'line_total' === $offset ) {
  408. $offset = 'total';
  409. } elseif ( 'line_tax' === $offset ) {
  410. $offset = 'total_tax';
  411. } elseif ( 'line_tax_data' === $offset ) {
  412. $offset = 'taxes';
  413. } elseif ( 'qty' === $offset ) {
  414. $offset = 'quantity';
  415. }
  416. parent::offsetSet( $offset, $value );
  417. }
  418. /**
  419. * OffsetExists for ArrayAccess.
  420. *
  421. * @param string $offset Offset.
  422. * @return bool
  423. */
  424. public function offsetExists( $offset ) {
  425. if ( in_array( $offset, array( 'line_subtotal', 'line_subtotal_tax', 'line_total', 'line_tax', 'line_tax_data', 'item_meta_array', 'item_meta', 'qty' ), true ) ) {
  426. return true;
  427. }
  428. return parent::offsetExists( $offset );
  429. }
  430. }