forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wc-order.php
302 lines (271 loc) · 9.03 KB
/
class-wc-order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Order
*
* @class WC_Order
* @version 2.2.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Order extends WC_Abstract_Order {
/** @public string Order type */
public $order_type = 'simple';
/**
* Gets order total - formatted for display.
*
* @return string
*/
public function get_formatted_order_total( $tax_display = '', $display_refunded = true ) {
$formatted_total = wc_price( $this->get_total(), array( 'currency' => $this->get_order_currency() ) );
$order_total = $this->get_total();
$total_refunded = $this->get_total_refunded();
$tax_string = '';
// Tax for inclusive prices
if ( wc_tax_enabled() && 'incl' == $tax_display ) {
$tax_string_array = array();
if ( 'itemized' == get_option( 'woocommerce_tax_total_display' ) ) {
foreach ( $this->get_tax_totals() as $code => $tax ) {
$tax_amount = ( $total_refunded && $display_refunded ) ? wc_price( WC_Tax::round( $tax->amount - $this->get_total_tax_refunded_by_rate_id( $tax->rate_id ) ), array( 'currency' => $this->get_order_currency() ) ) : $tax->formatted_amount;
$tax_string_array[] = sprintf( '%s %s', $tax_amount, $tax->label );
}
} else {
$tax_amount = ( $total_refunded && $display_refunded ) ? $this->get_total_tax() - $this->get_total_tax_refunded() : $this->get_total_tax();
$tax_string_array[] = sprintf( '%s %s', wc_price( $tax_amount, array( 'currency' => $this->get_order_currency() ) ), WC()->countries->tax_or_vat() );
}
if ( ! empty( $tax_string_array ) ) {
$tax_string = ' ' . sprintf( __( '(Includes %s)', 'woocommerce' ), implode( ', ', $tax_string_array ) );
}
}
if ( $total_refunded && $display_refunded ) {
$formatted_total = '<del>' . strip_tags( $formatted_total ) . '</del> <ins>' . wc_price( $order_total - $total_refunded, array( 'currency' => $this->get_order_currency() ) ) . $tax_string . '</ins>';
} else {
$formatted_total .= $tax_string;
}
return apply_filters( 'woocommerce_get_formatted_order_total', $formatted_total, $this );
}
/**
* Get order refunds.
* @since 2.2
* @return array of WC_Order_Refund objects
*/
public function get_refunds() {
if ( empty( $this->refunds ) && ! is_array( $this->refunds ) ) {
$refunds = array();
$refund_items = get_posts(
array(
'post_type' => 'shop_order_refund',
'post_parent' => $this->id,
'posts_per_page' => -1,
'post_status' => 'any',
'fields' => 'ids'
)
);
foreach ( $refund_items as $refund_id ) {
$refunds[] = new WC_Order_Refund( $refund_id );
}
$this->refunds = $refunds;
}
return $this->refunds;
}
/**
* Get amount already refunded.
*
* @since 2.2
* @return string
*/
public function get_total_refunded() {
global $wpdb;
$total = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( postmeta.meta_value )
FROM $wpdb->postmeta AS postmeta
INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
WHERE postmeta.meta_key = '_refund_amount'
AND postmeta.post_id = posts.ID
", $this->id ) );
return $total;
}
/**
* Get the total tax refunded.
*
* @since 2.3
* @return float
*/
public function get_total_tax_refunded() {
global $wpdb;
$total = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( order_itemmeta.meta_value )
FROM {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'tax' )
WHERE order_itemmeta.order_item_id = order_items.order_item_id
AND order_itemmeta.meta_key IN ('tax_amount', 'shipping_tax_amount')
", $this->id ) );
return abs( $total );
}
/**
* Get the total shipping refunded.
*
* @since 2.4
* @return float
*/
public function get_total_shipping_refunded() {
global $wpdb;
$total = $wpdb->get_var( $wpdb->prepare( "
SELECT SUM( order_itemmeta.meta_value )
FROM {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'shipping' )
WHERE order_itemmeta.order_item_id = order_items.order_item_id
AND order_itemmeta.meta_key IN ('cost')
", $this->id ) );
return abs( $total );
}
/**
* Gets the count of order items of a certain type that have been refunded.
* @since 2.4.0
* @param string $item_type
* @return string
*/
public function get_item_count_refunded( $item_type = '' ) {
if ( empty( $item_type ) ) {
$item_type = array( 'line_item' );
}
if ( ! is_array( $item_type ) ) {
$item_type = array( $item_type );
}
$count = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
$count += empty( $refunded_item['qty'] ) ? 0 : $refunded_item['qty'];
}
}
return apply_filters( 'woocommerce_get_item_count_refunded', $count, $item_type, $this );
}
/**
* Get the total number of items refunded.
*
* @since 2.4.0
* @param string $item_type type of the item we're checking, if not a line_item
* @return integer
*/
public function get_total_qty_refunded( $item_type = 'line_item' ) {
$qty = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
$qty += $refunded_item['qty'];
}
}
return $qty;
}
/**
* Get the refunded amount for a line item.
*
* @param int $item_id ID of the item we're checking
* @param string $item_type type of the item we're checking, if not a line_item
* @return integer
*/
public function get_qty_refunded_for_item( $item_id, $item_type = 'line_item' ) {
$qty = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
if ( isset( $refunded_item['refunded_item_id'] ) && $refunded_item['refunded_item_id'] == $item_id ) {
$qty += $refunded_item['qty'];
}
}
}
return $qty;
}
/**
* Get the refunded amount for a line item.
*
* @param int $item_id ID of the item we're checking
* @param string $item_type type of the item we're checking, if not a line_item
* @return integer
*/
public function get_total_refunded_for_item( $item_id, $item_type = 'line_item' ) {
$total = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
if ( isset( $refunded_item['refunded_item_id'] ) && $refunded_item['refunded_item_id'] == $item_id ) {
switch ( $item_type ) {
case 'shipping' :
$total += $refunded_item['cost'];
break;
default :
$total += $refunded_item['line_total'];
break;
}
}
}
}
return $total * -1;
}
/**
* Get the refunded amount for a line item.
*
* @param int $item_id ID of the item we're checking
* @param int $tax_id ID of the tax we're checking
* @param string $item_type type of the item we're checking, if not a line_item
* @return double
*/
public function get_tax_refunded_for_item( $item_id, $tax_id, $item_type = 'line_item' ) {
$total = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( $item_type ) as $refunded_item ) {
if ( isset( $refunded_item['refunded_item_id'] ) && $refunded_item['refunded_item_id'] == $item_id ) {
switch ( $item_type ) {
case 'shipping' :
$tax_data = maybe_unserialize( $refunded_item['taxes'] );
if ( isset( $tax_data[ $tax_id ] ) ) {
$total += $tax_data[ $tax_id ];
}
break;
default :
$tax_data = maybe_unserialize( $refunded_item['line_tax_data'] );
if ( isset( $tax_data['total'][ $tax_id ] ) ) {
$total += $tax_data['total'][ $tax_id ];
}
break;
}
}
}
}
return wc_round_tax_total( $total ) * -1;
}
/**
* Get total tax refunded by rate ID.
*
* @param int $rate_id
*
* @return float
*/
public function get_total_tax_refunded_by_rate_id( $rate_id ) {
$total = 0;
foreach ( $this->get_refunds() as $refund ) {
foreach ( $refund->get_items( 'tax' ) as $refunded_item ) {
if ( isset( $refunded_item['rate_id'] ) && $refunded_item['rate_id'] == $rate_id ) {
$total += abs( $refunded_item['tax_amount'] ) + abs( $refunded_item['shipping_tax_amount'] );
}
}
}
return $total;
}
/**
* How much money is left to refund?
* @return string
*/
public function get_remaining_refund_amount() {
return wc_format_decimal( $this->get_total() - $this->get_total_refunded() );
}
/**
* How many items are left to refund?
* @return int
*/
public function get_remaining_refund_items() {
return absint( $this->get_item_count() - $this->get_item_count_refunded() );
}
}