1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Order refund. Refunds are based on orders (essentially negative orders) and |
8
|
|
|
* contain much of the same data. |
9
|
|
|
* |
10
|
|
|
* @version 2.7.0 |
11
|
|
|
* @package WooCommerce/Classes |
12
|
|
|
* @category Class |
13
|
|
|
* @author WooThemes |
14
|
|
|
*/ |
15
|
|
|
class WC_Order_Refund extends WC_Abstract_Order { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Extend the abstract _data properties and then read the order object. |
19
|
|
|
* @param int|object|WC_Order $read Order to init. |
20
|
|
|
*/ |
21
|
|
|
public function __construct( $read = 0 ) { |
22
|
|
|
// Extend order data |
23
|
|
|
$this->_data = array_merge( $this->_data, array( |
24
|
|
|
'amount' => '', |
25
|
|
|
'reason' => '', |
26
|
|
|
'refunded_by' => 0, |
27
|
|
|
) ); |
28
|
|
|
parent::__construct( $read ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Data stored in meta keys, but not considered "meta" for an order. |
33
|
|
|
* @since 2.7.0 |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $_internal_meta_keys = array( |
37
|
|
|
'_order_currency', |
38
|
|
|
'_cart_discount', |
39
|
|
|
'_refund_amount', |
40
|
|
|
'_refunded_by', |
41
|
|
|
'_refund_reason', |
42
|
|
|
'_cart_discount_tax', |
43
|
|
|
'_order_shipping', |
44
|
|
|
'_order_shipping_tax', |
45
|
|
|
'_order_tax', |
46
|
|
|
'_order_total', |
47
|
|
|
'_order_version', |
48
|
|
|
'_prices_include_tax', |
49
|
|
|
'_payment_tokens', |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Insert data into the database. |
54
|
|
|
* @since 2.7.0 |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function create() { |
|
|
|
|
57
|
|
|
parent::create(); |
58
|
|
|
|
59
|
|
|
// Store additonal order data |
60
|
|
|
if ( $this->get_id() ) { |
61
|
|
|
$this->update_post_meta( '_refund_amount', $this->get_amount() ); |
62
|
|
|
$this->update_post_meta( '_refunded_by', $this->get_refunded_by() ); |
63
|
|
|
$this->update_post_meta( '_refund_reason', $this->get_reason() ); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Read from the database. |
69
|
|
|
* @since 2.7.0 |
70
|
|
|
* @param int $id ID of object to read. |
71
|
|
|
*/ |
72
|
|
|
public function read( $id ) { |
73
|
|
|
parent::read( $id ); |
74
|
|
|
|
75
|
|
|
if ( ! $this->get_id() ) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$post_object = get_post( $id ); |
80
|
|
|
|
81
|
|
|
$this->set_props( array( |
82
|
|
|
'amount' => get_post_meta( $this->get_id(), '_refund_amount', true ), |
83
|
|
|
'refunded_by' => metadata_exists( 'post', $this->get_id(), '_refunded_by' ) ? get_post_meta( $this->get_id(), '_refunded_by', true ) : absint( $post_object->post_author ), |
84
|
|
|
'reason' => metadata_exists( 'post', $this->get_id(), '_refund_reason' ) ? get_post_meta( $this->get_id(), '_refund_reason', true ) : $post_object->post_excerpt, |
85
|
|
|
) ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Update data in the database. |
90
|
|
|
* @since 2.7.0 |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function update() { |
|
|
|
|
93
|
|
|
parent::update(); |
94
|
|
|
|
95
|
|
|
// Store additonal order data |
96
|
|
|
$this->update_post_meta( '_refund_amount', $this->get_amount() ); |
97
|
|
|
$this->update_post_meta( '_refunded_by', $this->get_refunded_by() ); |
98
|
|
|
$this->update_post_meta( '_refund_reason', $this->get_reason() ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete data from the database. |
103
|
|
|
* @since 2.7.0 |
104
|
|
|
*/ |
105
|
|
|
public function delete() { |
106
|
|
|
wp_delete_post( $this->get_id(), true ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get internal type (post type.) |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function get_type() { |
114
|
|
|
return 'shop_order_refund'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get status - always completed for refunds. |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function get_status() { |
122
|
|
|
return 'completed'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get a title for the new post type. |
127
|
|
|
*/ |
128
|
|
|
protected function get_post_title() { |
129
|
|
|
return sprintf( __( 'Refund – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set refunded amount. |
134
|
|
|
* @param string $value |
135
|
|
|
* @throws WC_Data_Exception |
136
|
|
|
*/ |
137
|
|
|
public function set_amount( $value ) { |
138
|
|
|
$this->_data['amount'] = wc_format_decimal( $value ); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get refunded amount. |
143
|
|
|
* @return int|float |
144
|
|
|
*/ |
145
|
|
|
public function get_amount() { |
146
|
|
|
return apply_filters( 'woocommerce_refund_amount', (double) $this->_data['amount'], $this ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get formatted refunded amount. |
151
|
|
|
* @since 2.4 |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function get_formatted_refund_amount() { |
155
|
|
|
return apply_filters( 'woocommerce_formatted_refund_amount', wc_price( $this->get_amount(), array( 'currency' => $this->get_currency() ) ), $this ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set refund reason. |
160
|
|
|
* @param string $value |
161
|
|
|
* @throws WC_Data_Exception |
162
|
|
|
*/ |
163
|
|
|
public function set_reason( $value ) { |
164
|
|
|
$this->_data['reason'] = $value; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get refund reason. |
169
|
|
|
* @since 2.2 |
170
|
|
|
* @return int|float |
171
|
|
|
*/ |
172
|
|
|
public function get_reason() { |
173
|
|
|
return apply_filters( 'woocommerce_refund_reason', $this->_data['reason'], $this ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set refunded by. |
178
|
|
|
* @param int $value |
179
|
|
|
* @throws WC_Data_Exception |
180
|
|
|
*/ |
181
|
|
|
public function set_refunded_by( $value ) { |
182
|
|
|
$this->_data['refunded_by'] = absint( $value ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get ID of user who did the refund. |
187
|
|
|
* @since 2.7 |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
|
|
public function get_refunded_by() { |
191
|
|
|
return absint( $this->_data['refunded_by'] ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Magic __get method for backwards compatibility. |
196
|
|
|
* @param string $key |
197
|
|
|
* @return mixed |
198
|
|
|
*/ |
199
|
|
|
public function __get( $key ) { |
200
|
|
|
_doing_it_wrong( $key, 'Refund properties should not be accessed directly.', '2.7' ); |
201
|
|
|
/** |
202
|
|
|
* Maps legacy vars to new getters. |
203
|
|
|
*/ |
204
|
|
|
if ( 'reason' === $key ) { |
205
|
|
|
return $this->get_reason(); |
206
|
|
|
} elseif ( 'refund_amount' === $key ) { |
207
|
|
|
return $this->get_amount(); |
208
|
|
|
} |
209
|
|
|
return parent::__get( $key ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Gets an refund from the database. |
214
|
|
|
* @deprecated 2.7 |
215
|
|
|
* @param int $id (default: 0). |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
public function get_refund( $id = 0 ) { |
|
|
|
|
219
|
|
|
_deprecated_function( 'get_refund', '2.7', 'read' ); |
220
|
|
|
if ( ! $id ) { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
if ( $result = get_post( $id ) ) { |
224
|
|
|
$this->populate( $result ); |
|
|
|
|
225
|
|
|
return true; |
226
|
|
|
} |
227
|
|
|
return false; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get refund amount. |
232
|
|
|
* @deprecated 2.7 |
233
|
|
|
* @return int|float |
234
|
|
|
*/ |
235
|
|
|
public function get_refund_amount() { |
236
|
|
|
_deprecated_function( 'get_refund_amount', '2.7', 'get_amount' ); |
237
|
|
|
return $this->get_amount(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Get refund reason. |
242
|
|
|
* @deprecated 2.7 |
243
|
|
|
* @return int|float |
244
|
|
|
*/ |
245
|
|
|
public function get_refund_reason() { |
246
|
|
|
_deprecated_function( 'get_refund_reason', '2.7', 'get_reason' ); |
247
|
|
|
return $this->get_reason(); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.