class-wc-order-item-coupon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Order Line Item (coupon)
  4. *
  5. * @package WooCommerce/Classes
  6. * @version 3.0.0
  7. * @since 3.0.0
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Order item coupon class.
  12. */
  13. class WC_Order_Item_Coupon 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. 'code' => '',
  22. 'discount' => 0,
  23. 'discount_tax' => 0,
  24. );
  25. /*
  26. |--------------------------------------------------------------------------
  27. | Setters
  28. |--------------------------------------------------------------------------
  29. */
  30. /**
  31. * Set order item name.
  32. *
  33. * @param string $value Coupon code.
  34. */
  35. public function set_name( $value ) {
  36. return $this->set_code( $value );
  37. }
  38. /**
  39. * Set code.
  40. *
  41. * @param string $value Coupon code.
  42. */
  43. public function set_code( $value ) {
  44. $this->set_prop( 'code', wc_clean( $value ) );
  45. }
  46. /**
  47. * Set discount amount.
  48. *
  49. * @param string $value Discount.
  50. */
  51. public function set_discount( $value ) {
  52. $this->set_prop( 'discount', wc_format_decimal( $value ) );
  53. }
  54. /**
  55. * Set discounted tax amount.
  56. *
  57. * @param string $value Discount tax.
  58. */
  59. public function set_discount_tax( $value ) {
  60. $this->set_prop( 'discount_tax', wc_format_decimal( $value ) );
  61. }
  62. /*
  63. |--------------------------------------------------------------------------
  64. | Getters
  65. |--------------------------------------------------------------------------
  66. */
  67. /**
  68. * Get order item type.
  69. *
  70. * @return string
  71. */
  72. public function get_type() {
  73. return 'coupon';
  74. }
  75. /**
  76. * Get order item name.
  77. *
  78. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  79. * @return string
  80. */
  81. public function get_name( $context = 'view' ) {
  82. return $this->get_code( $context );
  83. }
  84. /**
  85. * Get coupon code.
  86. *
  87. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  88. * @return string
  89. */
  90. public function get_code( $context = 'view' ) {
  91. return $this->get_prop( 'code', $context );
  92. }
  93. /**
  94. * Get discount amount.
  95. *
  96. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  97. * @return string
  98. */
  99. public function get_discount( $context = 'view' ) {
  100. return $this->get_prop( 'discount', $context );
  101. }
  102. /**
  103. * Get discounted tax amount.
  104. *
  105. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  106. *
  107. * @return string
  108. */
  109. public function get_discount_tax( $context = 'view' ) {
  110. return $this->get_prop( 'discount_tax', $context );
  111. }
  112. /*
  113. |--------------------------------------------------------------------------
  114. | Array Access Methods
  115. |--------------------------------------------------------------------------
  116. |
  117. | For backwards compatibility with legacy arrays.
  118. |
  119. */
  120. /**
  121. * OffsetGet for ArrayAccess/Backwards compatibility.
  122. *
  123. * @deprecated Add deprecation notices in future release.
  124. * @param string $offset Offset.
  125. * @return mixed
  126. */
  127. public function offsetGet( $offset ) {
  128. if ( 'discount_amount' === $offset ) {
  129. $offset = 'discount';
  130. } elseif ( 'discount_amount_tax' === $offset ) {
  131. $offset = 'discount_tax';
  132. }
  133. return parent::offsetGet( $offset );
  134. }
  135. /**
  136. * OffsetSet for ArrayAccess/Backwards compatibility.
  137. *
  138. * @deprecated Add deprecation notices in future release.
  139. * @param string $offset Offset.
  140. * @param mixed $value Value.
  141. */
  142. public function offsetSet( $offset, $value ) {
  143. if ( 'discount_amount' === $offset ) {
  144. $offset = 'discount';
  145. } elseif ( 'discount_amount_tax' === $offset ) {
  146. $offset = 'discount_tax';
  147. }
  148. parent::offsetSet( $offset, $value );
  149. }
  150. /**
  151. * OffsetExists for ArrayAccess.
  152. *
  153. * @param string $offset Offset.
  154. * @return bool
  155. */
  156. public function offsetExists( $offset ) {
  157. if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ), true ) ) {
  158. return true;
  159. }
  160. return parent::offsetExists( $offset );
  161. }
  162. }