Geen omschrijving

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) {
  145. $this->set_total_tax( array_sum( $tax_data['total'] ) );
  146. $this->set_subtotal_tax( array_sum( $tax_data['subtotal'] ) );
  147. } else {
  148. $this->set_total_tax( array_sum( array_map( 'wc_round_tax_total', $tax_data['total'] ) ) );
  149. $this->set_subtotal_tax( array_sum( array_map( 'wc_round_tax_total', $tax_data['subtotal'] ) ) );
  150. }
  151. }
  152. /**
  153. * Set variation data (stored as meta data - write only).
  154. *
  155. * @param array $data Key/Value pairs.
  156. */
  157. public function set_variation( $data = array() ) {
  158. if ( is_array( $data ) ) {
  159. foreach ( $data as $key => $value ) {
  160. $this->add_meta_data( str_replace( 'attribute_', '', $key ), $value, true );
  161. }
  162. }
  163. }
  164. /**
  165. * Set properties based on passed in product object.
  166. *
  167. * @param WC_Product $product Product instance.
  168. */
  169. public function set_product( $product ) {
  170. if ( ! is_a( $product, 'WC_Product' ) ) {
  171. $this->error( 'order_item_product_invalid_product', __( 'Invalid product', 'woocommerce' ) );
  172. }
  173. if ( $product->is_type( 'variation' ) ) {
  174. $this->set_product_id( $product->get_parent_id() );
  175. $this->set_variation_id( $product->get_id() );
  176. $this->set_variation( is_callable( array( $product, 'get_variation_attributes' ) ) ? $product->get_variation_attributes() : array() );
  177. } else {
  178. $this->set_product_id( $product->get_id() );
  179. }
  180. $this->set_name( $product->get_name() );
  181. $this->set_tax_class( $product->get_tax_class() );
  182. }
  183. /**
  184. * Set meta data for backordered products.
  185. */
  186. public function set_backorder_meta() {
  187. $product = $this->get_product();
  188. if ( $product && $product->backorders_require_notification() && $product->is_on_backorder( $this->get_quantity() ) ) {
  189. $this->add_meta_data( apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ), $this ), $this->get_quantity() - max( 0, $product->get_stock_quantity() ), true );
  190. }
  191. }
  192. /*
  193. |--------------------------------------------------------------------------
  194. | Getters
  195. |--------------------------------------------------------------------------
  196. */
  197. /**
  198. * Get order item type.
  199. *
  200. * @return string
  201. */
  202. public function get_type() {
  203. return 'line_item';
  204. }
  205. /**
  206. * Get product ID.
  207. *
  208. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  209. * @return int
  210. */
  211. public function get_product_id( $context = 'view' ) {
  212. return $this->get_prop( 'product_id', $context );
  213. }
  214. /**
  215. * Get variation ID.
  216. *
  217. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  218. * @return int
  219. */
  220. public function get_variation_id( $context = 'view' ) {
  221. return $this->get_prop( 'variation_id', $context );
  222. }
  223. /**
  224. * Get quantity.
  225. *
  226. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  227. * @return int
  228. */
  229. public function get_quantity( $context = 'view' ) {
  230. return $this->get_prop( 'quantity', $context );
  231. }
  232. /**
  233. * Get tax class.
  234. *
  235. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  236. * @return string
  237. */
  238. public function get_tax_class( $context = 'view' ) {
  239. return $this->get_prop( 'tax_class', $context );
  240. }
  241. /**
  242. * Get subtotal.
  243. *
  244. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  245. * @return string
  246. */
  247. public function get_subtotal( $context = 'view' ) {
  248. return $this->get_prop( 'subtotal', $context );
  249. }
  250. /**
  251. * Get subtotal tax.
  252. *
  253. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  254. * @return string
  255. */
  256. public function get_subtotal_tax( $context = 'view' ) {
  257. return $this->get_prop( 'subtotal_tax', $context );
  258. }
  259. /**
  260. * Get total.
  261. *
  262. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  263. * @return string
  264. */
  265. public function get_total( $context = 'view' ) {
  266. return $this->get_prop( 'total', $context );
  267. }
  268. /**
  269. * Get total tax.
  270. *
  271. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  272. * @return string
  273. */
  274. public function get_total_tax( $context = 'view' ) {
  275. return $this->get_prop( 'total_tax', $context );
  276. }
  277. /**
  278. * Get taxes.
  279. *
  280. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  281. * @return array
  282. */
  283. public function get_taxes( $context = 'view' ) {
  284. return $this->get_prop( 'taxes', $context );
  285. }
  286. /**
  287. * Get the associated product.
  288. *
  289. * @return WC_Product|bool
  290. */
  291. public function get_product() {
  292. if ( $this->get_variation_id() ) {
  293. $product = wc_get_product( $this->get_variation_id() );
  294. } else {
  295. $product = wc_get_product( $this->get_product_id() );
  296. }
  297. // Backwards compatible filter from WC_Order::get_product_from_item().
  298. if ( has_filter( 'woocommerce_get_product_from_item' ) ) {
  299. $product = apply_filters( 'woocommerce_get_product_from_item', $product, $this, $this->get_order() );
  300. }
  301. return apply_filters( 'woocommerce_order_item_product', $product, $this );
  302. }
  303. /**
  304. * Get the Download URL.
  305. *
  306. * @param int $download_id Download ID.
  307. * @return string
  308. */
  309. public function get_item_download_url( $download_id ) {
  310. $order = $this->get_order();
  311. return $order ? add_query_arg(
  312. array(
  313. 'download_file' => $this->get_variation_id() ? $this->get_variation_id() : $this->get_product_id(),
  314. 'order' => $order->get_order_key(),
  315. 'email' => rawurlencode( $order->get_billing_email() ),
  316. 'key' => $download_id,
  317. ),
  318. trailingslashit( home_url() )
  319. ) : '';
  320. }
  321. /**
  322. * Get any associated downloadable files.
  323. *
  324. * @return array
  325. */
  326. public function get_item_downloads() {
  327. $files = array();
  328. $product = $this->get_product();
  329. $order = $this->get_order();
  330. $product_id = $this->get_variation_id() ? $this->get_variation_id() : $this->get_product_id();
  331. if ( $product && $order && $product->is_downloadable() && $order->is_download_permitted() ) {
  332. $email_hash = function_exists( 'hash' ) ? hash( 'sha256', $order->get_billing_email() ) : sha1( $order->get_billing_email() );
  333. $data_store = WC_Data_Store::load( 'customer-download' );
  334. $customer_downloads = $data_store->get_downloads(
  335. array(
  336. 'user_email' => $order->get_billing_email(),
  337. 'order_id' => $order->get_id(),
  338. 'product_id' => $product_id,
  339. )
  340. );
  341. foreach ( $customer_downloads as $customer_download ) {
  342. $download_id = $customer_download->get_download_id();
  343. if ( $product->has_file( $download_id ) ) {
  344. $file = $product->get_file( $download_id );
  345. $files[ $download_id ] = $file->get_data();
  346. $files[ $download_id ]['downloads_remaining'] = $customer_download->get_downloads_remaining();
  347. $files[ $download_id ]['access_expires'] = $customer_download->get_access_expires();
  348. $files[ $download_id ]['download_url'] = add_query_arg(
  349. array(
  350. 'download_file' => $product_id,
  351. 'order' => $order->get_order_key(),
  352. 'uid' => $email_hash,
  353. 'key' => $download_id,
  354. ),
  355. trailingslashit( home_url() )
  356. );
  357. }
  358. }
  359. }
  360. return apply_filters( 'woocommerce_get_item_downloads', $files, $this, $order );
  361. }
  362. /**
  363. * Get tax status.
  364. *
  365. * @return string
  366. */
  367. public function get_tax_status() {
  368. $product = $this->get_product();
  369. return $product ? $product->get_tax_status() : 'taxable';
  370. }
  371. /*
  372. |--------------------------------------------------------------------------
  373. | Array Access Methods
  374. |--------------------------------------------------------------------------
  375. |
  376. | For backwards compatibility with legacy arrays.
  377. |
  378. */
  379. /**
  380. * OffsetGet for ArrayAccess/Backwards compatibility.
  381. *
  382. * @param string $offset Offset.
  383. * @return mixed
  384. */
  385. public function offsetGet( $offset ) {
  386. if ( 'line_subtotal' === $offset ) {
  387. $offset = 'subtotal';
  388. } elseif ( 'line_subtotal_tax' === $offset ) {
  389. $offset = 'subtotal_tax';
  390. } elseif ( 'line_total' === $offset ) {
  391. $offset = 'total';
  392. } elseif ( 'line_tax' === $offset ) {
  393. $offset = 'total_tax';
  394. } elseif ( 'line_tax_data' === $offset ) {
  395. $offset = 'taxes';
  396. } elseif ( 'qty' === $offset ) {
  397. $offset = 'quantity';
  398. }
  399. return parent::offsetGet( $offset );
  400. }
  401. /**
  402. * OffsetSet for ArrayAccess/Backwards compatibility.
  403. *
  404. * @deprecated 4.4.0
  405. * @param string $offset Offset.
  406. * @param mixed $value Value.
  407. */
  408. public function offsetSet( $offset, $value ) {
  409. wc_deprecated_function( 'WC_Order_Item_Product::offsetSet', '4.4.0', '' );
  410. if ( 'line_subtotal' === $offset ) {
  411. $offset = 'subtotal';
  412. } elseif ( 'line_subtotal_tax' === $offset ) {
  413. $offset = 'subtotal_tax';
  414. } elseif ( 'line_total' === $offset ) {
  415. $offset = 'total';
  416. } elseif ( 'line_tax' === $offset ) {
  417. $offset = 'total_tax';
  418. } elseif ( 'line_tax_data' === $offset ) {
  419. $offset = 'taxes';
  420. } elseif ( 'qty' === $offset ) {
  421. $offset = 'quantity';
  422. }
  423. parent::offsetSet( $offset, $value );
  424. }
  425. /**
  426. * OffsetExists for ArrayAccess.
  427. *
  428. * @param string $offset Offset.
  429. * @return bool
  430. */
  431. public function offsetExists( $offset ) {
  432. if ( in_array( $offset, array( 'line_subtotal', 'line_subtotal_tax', 'line_total', 'line_tax', 'line_tax_data', 'item_meta_array', 'item_meta', 'qty' ), true ) ) {
  433. return true;
  434. }
  435. return parent::offsetExists( $offset );
  436. }
  437. }