暂无描述

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_format_coupon_code( $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 4.4.0
  124. * @param string $offset Offset.
  125. * @return mixed
  126. */
  127. public function offsetGet( $offset ) {
  128. wc_deprecated_function( 'WC_Order_Item_Coupon::offsetGet', '4.4.0', '' );
  129. if ( 'discount_amount' === $offset ) {
  130. $offset = 'discount';
  131. } elseif ( 'discount_amount_tax' === $offset ) {
  132. $offset = 'discount_tax';
  133. }
  134. return parent::offsetGet( $offset );
  135. }
  136. /**
  137. * OffsetSet for ArrayAccess/Backwards compatibility.
  138. *
  139. * @deprecated 4.4.0
  140. * @param string $offset Offset.
  141. * @param mixed $value Value.
  142. */
  143. public function offsetSet( $offset, $value ) {
  144. wc_deprecated_function( 'WC_Order_Item_Coupon::offsetSet', '4.4.0', '' );
  145. if ( 'discount_amount' === $offset ) {
  146. $offset = 'discount';
  147. } elseif ( 'discount_amount_tax' === $offset ) {
  148. $offset = 'discount_tax';
  149. }
  150. parent::offsetSet( $offset, $value );
  151. }
  152. /**
  153. * OffsetExists for ArrayAccess.
  154. *
  155. * @param string $offset Offset.
  156. * @return bool
  157. */
  158. public function offsetExists( $offset ) {
  159. if ( in_array( $offset, array( 'discount_amount', 'discount_amount_tax' ), true ) ) {
  160. return true;
  161. }
  162. return parent::offsetExists( $offset );
  163. }
  164. }