|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Convert an order object to the order schema format. |
|
4
|
|
|
* |
|
5
|
|
|
* @package WooCommerce/RestApi |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace WooCommerce\RestApi\Controllers\Version4\Schema; |
|
9
|
|
|
|
|
10
|
|
|
defined( 'ABSPATH' ) || exit; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* OrderResponse class. |
|
14
|
|
|
*/ |
|
15
|
|
|
class OrderResponse extends AbstractResponse { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Decimal places to round to. |
|
19
|
|
|
* |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $dp; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct() { |
|
28
|
|
|
$this->dp = wc_get_price_decimals(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set decimal places. |
|
33
|
|
|
* |
|
34
|
|
|
* @param int $dp Decimals. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function set_dp( $dp ) { |
|
37
|
|
|
$this->dp = (int) $dp; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Convert object to match data in the schema. |
|
42
|
|
|
* |
|
43
|
|
|
* @param \WC_Order $object Product data. |
|
44
|
|
|
* @param string $context Request context. Options: 'view' and 'edit'. |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function prepare_response( $object, $context ) { |
|
48
|
|
|
$data = $object->get_data(); |
|
49
|
|
|
$format_decimal = array( 'discount_total', 'discount_tax', 'shipping_total', 'shipping_tax', 'shipping_total', 'shipping_tax', 'cart_tax', 'total', 'total_tax' ); |
|
50
|
|
|
$format_date = array( 'date_created', 'date_modified', 'date_completed', 'date_paid' ); |
|
51
|
|
|
$format_line_items = array( 'line_items', 'tax_lines', 'shipping_lines', 'fee_lines', 'coupon_lines' ); |
|
52
|
|
|
|
|
53
|
|
|
// Format decimal values. |
|
54
|
|
|
foreach ( $format_decimal as $key ) { |
|
55
|
|
|
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->dp ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Format date values. |
|
59
|
|
|
foreach ( $format_date as $key ) { |
|
60
|
|
|
$datetime = $data[ $key ]; |
|
61
|
|
|
$data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); |
|
62
|
|
|
$data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Format the order status. |
|
66
|
|
|
$data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status']; |
|
67
|
|
|
|
|
68
|
|
|
// Format line items. |
|
69
|
|
|
foreach ( $format_line_items as $key ) { |
|
70
|
|
|
$data[ $key ] = array_values( array_map( array( $this, 'prepare_order_item_data' ), $data[ $key ] ) ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Refunds. |
|
74
|
|
|
$data['refunds'] = array(); |
|
75
|
|
|
foreach ( $object->get_refunds() as $refund ) { |
|
76
|
|
|
$data['refunds'][] = array( |
|
77
|
|
|
'id' => $refund->get_id(), |
|
78
|
|
|
'reason' => $refund->get_reason() ? $refund->get_reason() : '', |
|
79
|
|
|
'total' => '-' . wc_format_decimal( $refund->get_amount(), $this->dp ), |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Currency symbols. |
|
84
|
|
|
$currency_symbol = get_woocommerce_currency_symbol( $data['currency'] ); |
|
85
|
|
|
$data['currency_symbol'] = html_entity_decode( $currency_symbol ); |
|
86
|
|
|
|
|
87
|
|
|
return array( |
|
88
|
|
|
'id' => $object->get_id(), |
|
89
|
|
|
'parent_id' => $data['parent_id'], |
|
90
|
|
|
'number' => $data['number'], |
|
91
|
|
|
'order_key' => $data['order_key'], |
|
92
|
|
|
'created_via' => $data['created_via'], |
|
93
|
|
|
'version' => $data['version'], |
|
94
|
|
|
'status' => $data['status'], |
|
95
|
|
|
'currency' => $data['currency'], |
|
96
|
|
|
'currency_symbol' => $data['currency_symbol'], |
|
97
|
|
|
'date_created' => $data['date_created'], |
|
98
|
|
|
'date_created_gmt' => $data['date_created_gmt'], |
|
99
|
|
|
'date_modified' => $data['date_modified'], |
|
100
|
|
|
'date_modified_gmt' => $data['date_modified_gmt'], |
|
101
|
|
|
'discount_total' => $data['discount_total'], |
|
102
|
|
|
'discount_tax' => $data['discount_tax'], |
|
103
|
|
|
'shipping_total' => $data['shipping_total'], |
|
104
|
|
|
'shipping_tax' => $data['shipping_tax'], |
|
105
|
|
|
'cart_tax' => $data['cart_tax'], |
|
106
|
|
|
'total' => $data['total'], |
|
107
|
|
|
'total_tax' => $data['total_tax'], |
|
108
|
|
|
'prices_include_tax' => $data['prices_include_tax'], |
|
109
|
|
|
'customer_id' => $data['customer_id'], |
|
110
|
|
|
'customer_ip_address' => $data['customer_ip_address'], |
|
111
|
|
|
'customer_user_agent' => $data['customer_user_agent'], |
|
112
|
|
|
'customer_note' => $data['customer_note'], |
|
113
|
|
|
'billing' => $data['billing'], |
|
114
|
|
|
'shipping' => $data['shipping'], |
|
115
|
|
|
'payment_method' => $data['payment_method'], |
|
116
|
|
|
'payment_method_title' => $data['payment_method_title'], |
|
117
|
|
|
'transaction_id' => $data['transaction_id'], |
|
118
|
|
|
'date_paid' => $data['date_paid'], |
|
119
|
|
|
'date_paid_gmt' => $data['date_paid_gmt'], |
|
120
|
|
|
'date_completed' => $data['date_completed'], |
|
121
|
|
|
'date_completed_gmt' => $data['date_completed_gmt'], |
|
122
|
|
|
'cart_hash' => $data['cart_hash'], |
|
123
|
|
|
'meta_data' => $data['meta_data'], |
|
124
|
|
|
'line_items' => $data['line_items'], |
|
125
|
|
|
'tax_lines' => $data['tax_lines'], |
|
126
|
|
|
'shipping_lines' => $data['shipping_lines'], |
|
127
|
|
|
'fee_lines' => $data['fee_lines'], |
|
128
|
|
|
'coupon_lines' => $data['coupon_lines'], |
|
129
|
|
|
'refunds' => $data['refunds'], |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Expands an order item to get its data. |
|
135
|
|
|
* |
|
136
|
|
|
* @param \WC_Order_item $item Order item data. |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function prepare_order_item_data( $item ) { |
|
140
|
|
|
$data = $item->get_data(); |
|
141
|
|
|
$format_decimal = array( 'subtotal', 'subtotal_tax', 'total', 'total_tax', 'tax_total', 'shipping_tax_total' ); |
|
142
|
|
|
|
|
143
|
|
|
// Format decimal values. |
|
144
|
|
|
foreach ( $format_decimal as $key ) { |
|
145
|
|
|
if ( isset( $data[ $key ] ) ) { |
|
146
|
|
|
$data[ $key ] = wc_format_decimal( $data[ $key ], $this->dp ); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// Add SKU and PRICE to products. |
|
151
|
|
|
if ( is_callable( array( $item, 'get_product' ) ) ) { |
|
152
|
|
|
$data['sku'] = $item->get_product() ? $item->get_product()->get_sku() : null; |
|
|
|
|
|
|
153
|
|
|
$data['price'] = $item->get_quantity() ? $item->get_total() / $item->get_quantity() : 0; |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// Format taxes. |
|
157
|
|
|
if ( ! empty( $data['taxes']['total'] ) ) { |
|
158
|
|
|
$taxes = array(); |
|
159
|
|
|
|
|
160
|
|
|
foreach ( $data['taxes']['total'] as $tax_rate_id => $tax ) { |
|
161
|
|
|
$taxes[] = array( |
|
162
|
|
|
'id' => $tax_rate_id, |
|
163
|
|
|
'total' => $tax, |
|
164
|
|
|
'subtotal' => isset( $data['taxes']['subtotal'][ $tax_rate_id ] ) ? $data['taxes']['subtotal'][ $tax_rate_id ] : '', |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
$data['taxes'] = $taxes; |
|
168
|
|
|
} elseif ( isset( $data['taxes'] ) ) { |
|
169
|
|
|
$data['taxes'] = array(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
// Remove names for coupons, taxes and shipping. |
|
173
|
|
|
if ( isset( $data['code'] ) || isset( $data['rate_code'] ) || isset( $data['method_title'] ) ) { |
|
174
|
|
|
unset( $data['name'] ); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// Remove props we don't want to expose. |
|
178
|
|
|
unset( $data['order_id'] ); |
|
179
|
|
|
unset( $data['type'] ); |
|
180
|
|
|
|
|
181
|
|
|
return $data; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.