class-wc-order-refund.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Order refund. Refunds are based on orders (essentially negative orders) and
  4. * contain much of the same data.
  5. *
  6. * @version 3.0.0
  7. * @package WooCommerce/Classes
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Order refund class.
  12. */
  13. class WC_Order_Refund extends WC_Abstract_Order {
  14. /**
  15. * Which data store to load.
  16. *
  17. * @var string
  18. */
  19. protected $data_store_name = 'order-refund';
  20. /**
  21. * This is the name of this object type.
  22. *
  23. * @var string
  24. */
  25. protected $object_type = 'order_refund';
  26. /**
  27. * Stores product data.
  28. *
  29. * @var array
  30. */
  31. protected $extra_data = array(
  32. 'amount' => '',
  33. 'reason' => '',
  34. 'refunded_by' => 0,
  35. 'refunded_payment' => false,
  36. );
  37. /**
  38. * Get internal type (post type.)
  39. *
  40. * @return string
  41. */
  42. public function get_type() {
  43. return 'shop_order_refund';
  44. }
  45. /**
  46. * Get status - always completed for refunds.
  47. *
  48. * @param string $context What the value is for. Valid values are view and edit.
  49. * @return string
  50. */
  51. public function get_status( $context = 'view' ) {
  52. return 'completed';
  53. }
  54. /**
  55. * Get a title for the new post type.
  56. */
  57. public function get_post_title() {
  58. // @codingStandardsIgnoreStart
  59. return sprintf( __( 'Refund &ndash; %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) );
  60. // @codingStandardsIgnoreEnd
  61. }
  62. /**
  63. * Get refunded amount.
  64. *
  65. * @param string $context What the value is for. Valid values are view and edit.
  66. * @return int|float
  67. */
  68. public function get_amount( $context = 'view' ) {
  69. return $this->get_prop( 'amount', $context );
  70. }
  71. /**
  72. * Get refund reason.
  73. *
  74. * @since 2.2
  75. * @param string $context What the value is for. Valid values are view and edit.
  76. * @return int|float
  77. */
  78. public function get_reason( $context = 'view' ) {
  79. return $this->get_prop( 'reason', $context );
  80. }
  81. /**
  82. * Get ID of user who did the refund.
  83. *
  84. * @since 3.0
  85. * @param string $context What the value is for. Valid values are view and edit.
  86. * @return int
  87. */
  88. public function get_refunded_by( $context = 'view' ) {
  89. return $this->get_prop( 'refunded_by', $context );
  90. }
  91. /**
  92. * Return if the payment was refunded via API.
  93. *
  94. * @since 3.3
  95. * @param string $context What the value is for. Valid values are view and edit.
  96. * @return bool
  97. */
  98. public function get_refunded_payment( $context = 'view' ) {
  99. return $this->get_prop( 'refunded_payment', $context );
  100. }
  101. /**
  102. * Get formatted refunded amount.
  103. *
  104. * @since 2.4
  105. * @return string
  106. */
  107. public function get_formatted_refund_amount() {
  108. return apply_filters( 'woocommerce_formatted_refund_amount', wc_price( $this->get_amount(), array( 'currency' => $this->get_currency() ) ), $this );
  109. }
  110. /**
  111. * Set refunded amount.
  112. *
  113. * @param string $value Value to set.
  114. * @throws WC_Data_Exception Exception if the amount is invalid.
  115. */
  116. public function set_amount( $value ) {
  117. $this->set_prop( 'amount', wc_format_decimal( $value ) );
  118. }
  119. /**
  120. * Set refund reason.
  121. *
  122. * @param string $value Value to set.
  123. * @throws WC_Data_Exception Exception if the amount is invalid.
  124. */
  125. public function set_reason( $value ) {
  126. $this->set_prop( 'reason', $value );
  127. }
  128. /**
  129. * Set refunded by.
  130. *
  131. * @param int $value Value to set.
  132. * @throws WC_Data_Exception Exception if the amount is invalid.
  133. */
  134. public function set_refunded_by( $value ) {
  135. $this->set_prop( 'refunded_by', absint( $value ) );
  136. }
  137. /**
  138. * Set if the payment was refunded via API.
  139. *
  140. * @since 3.3
  141. * @param bool $value Value to set.
  142. */
  143. public function set_refunded_payment( $value ) {
  144. $this->set_prop( 'refunded_payment', (bool) $value );
  145. }
  146. /**
  147. * Magic __get method for backwards compatibility.
  148. *
  149. * @param string $key Value to get.
  150. * @return mixed
  151. */
  152. public function __get( $key ) {
  153. wc_doing_it_wrong( $key, 'Refund properties should not be accessed directly.', '3.0' );
  154. /**
  155. * Maps legacy vars to new getters.
  156. */
  157. if ( 'reason' === $key ) {
  158. return $this->get_reason();
  159. } elseif ( 'refund_amount' === $key ) {
  160. return $this->get_amount();
  161. }
  162. return parent::__get( $key );
  163. }
  164. /**
  165. * Gets an refund from the database.
  166. *
  167. * @deprecated 3.0
  168. * @param int $id (default: 0).
  169. * @return bool
  170. */
  171. public function get_refund( $id = 0 ) {
  172. wc_deprecated_function( 'get_refund', '3.0', 'read' );
  173. if ( ! $id ) {
  174. return false;
  175. }
  176. $result = get_post( $id );
  177. if ( $result ) {
  178. $this->populate( $result );
  179. return true;
  180. }
  181. return false;
  182. }
  183. /**
  184. * Get refund amount.
  185. *
  186. * @deprecated 3.0
  187. * @return int|float
  188. */
  189. public function get_refund_amount() {
  190. wc_deprecated_function( 'get_refund_amount', '3.0', 'get_amount' );
  191. return $this->get_amount();
  192. }
  193. /**
  194. * Get refund reason.
  195. *
  196. * @deprecated 3.0
  197. * @return int|float
  198. */
  199. public function get_refund_reason() {
  200. wc_deprecated_function( 'get_refund_reason', '3.0', 'get_reason' );
  201. return $this->get_reason();
  202. }
  203. }