1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Order Line Item (tax) |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce/Classes |
6
|
|
|
* @version 3.0.0 |
7
|
|
|
* @since 3.0.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
defined( 'ABSPATH' ) || exit; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Order item tax. |
14
|
|
|
*/ |
15
|
|
|
class WC_Order_Item_Tax extends WC_Order_Item { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Order Data array. This is the core order data exposed in APIs since 3.0.0. |
19
|
|
|
* |
20
|
|
|
* @since 3.0.0 |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $extra_data = array( |
24
|
|
|
'rate_code' => '', |
25
|
|
|
'rate_id' => 0, |
26
|
|
|
'label' => '', |
27
|
|
|
'compound' => false, |
28
|
|
|
'tax_total' => 0, |
29
|
|
|
'shipping_tax_total' => 0, |
30
|
|
|
'rate_percent' => null, |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
|-------------------------------------------------------------------------- |
35
|
|
|
| Setters |
36
|
|
|
|-------------------------------------------------------------------------- |
37
|
|
|
*/ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set order item name. |
41
|
|
|
* |
42
|
|
|
* @param string $value Name. |
43
|
|
|
*/ |
44
|
4 |
|
public function set_name( $value ) { |
45
|
4 |
|
$this->set_rate_code( $value ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set item name. |
50
|
|
|
* |
51
|
|
|
* @param string $value Rate code. |
52
|
|
|
*/ |
53
|
10 |
|
public function set_rate_code( $value ) { |
54
|
10 |
|
$this->set_prop( 'rate_code', wc_clean( $value ) ); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set item name. |
59
|
|
|
* |
60
|
|
|
* @param string $value Label. |
61
|
|
|
*/ |
62
|
10 |
|
public function set_label( $value ) { |
63
|
10 |
|
$this->set_prop( 'label', wc_clean( $value ) ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set tax rate id. |
68
|
|
|
* |
69
|
|
|
* @param int $value Rate ID. |
70
|
|
|
*/ |
71
|
11 |
|
public function set_rate_id( $value ) { |
72
|
11 |
|
$this->set_prop( 'rate_id', absint( $value ) ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set tax total. |
77
|
|
|
* |
78
|
|
|
* @param string $value Tax total. |
79
|
|
|
*/ |
80
|
12 |
|
public function set_tax_total( $value ) { |
81
|
12 |
|
$this->set_prop( 'tax_total', $value ? wc_format_decimal( $value ) : 0 ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set shipping tax total. |
86
|
|
|
* |
87
|
|
|
* @param string $value Shipping tax total. |
88
|
|
|
*/ |
89
|
12 |
|
public function set_shipping_tax_total( $value ) { |
90
|
12 |
|
$this->set_prop( 'shipping_tax_total', $value ? wc_format_decimal( $value ) : 0 ); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set compound. |
95
|
|
|
* |
96
|
|
|
* @param bool $value If tax is compound. |
97
|
|
|
*/ |
98
|
11 |
|
public function set_compound( $value ) { |
99
|
11 |
|
$this->set_prop( 'compound', (bool) $value ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set rate value. |
104
|
|
|
* |
105
|
|
|
* @param float $value tax rate value. |
106
|
|
|
*/ |
107
|
10 |
|
public function set_rate_percent( $value ) { |
108
|
10 |
|
$this->set_prop( 'rate_percent', (float) $value ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set properties based on passed in tax rate by ID. |
113
|
|
|
* |
114
|
|
|
* @param int $tax_rate_id Tax rate ID. |
115
|
|
|
*/ |
116
|
10 |
|
public function set_rate( $tax_rate_id ) { |
117
|
10 |
|
$tax_rate = WC_Tax::_get_tax_rate( $tax_rate_id, OBJECT ); |
118
|
|
|
|
119
|
10 |
|
$this->set_rate_id( $tax_rate_id ); |
120
|
10 |
|
$this->set_rate_code( WC_Tax::get_rate_code( $tax_rate ) ); |
121
|
10 |
|
$this->set_label( WC_Tax::get_rate_label( $tax_rate ) ); |
122
|
10 |
|
$this->set_compound( WC_Tax::is_compound( $tax_rate ) ); |
123
|
10 |
|
$this->set_rate_percent( WC_Tax::get_rate_percent_value( $tax_rate ) ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/* |
127
|
|
|
|-------------------------------------------------------------------------- |
128
|
|
|
| Getters |
129
|
|
|
|-------------------------------------------------------------------------- |
130
|
|
|
*/ |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get order item type. |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
13 |
|
public function get_type() { |
138
|
13 |
|
return 'tax'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get rate code/name. |
143
|
|
|
* |
144
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
10 |
|
public function get_name( $context = 'view' ) { |
148
|
10 |
|
return $this->get_rate_code( $context ); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get rate code/name. |
153
|
|
|
* |
154
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
10 |
|
public function get_rate_code( $context = 'view' ) { |
158
|
10 |
|
return $this->get_prop( 'rate_code', $context ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get label. |
163
|
|
|
* |
164
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
10 |
View Code Duplication |
public function get_label( $context = 'view' ) { |
|
|
|
|
168
|
10 |
|
$label = $this->get_prop( 'label', $context ); |
169
|
10 |
|
if ( 'view' === $context ) { |
170
|
|
|
return $label ? $label : __( 'Tax', 'woocommerce' ); |
171
|
|
|
} else { |
172
|
10 |
|
return $label; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get tax rate ID. |
178
|
|
|
* |
179
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
180
|
|
|
* @return int |
181
|
|
|
*/ |
182
|
11 |
|
public function get_rate_id( $context = 'view' ) { |
183
|
11 |
|
return $this->get_prop( 'rate_id', $context ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get tax_total |
188
|
|
|
* |
189
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
12 |
|
public function get_tax_total( $context = 'view' ) { |
193
|
12 |
|
return $this->get_prop( 'tax_total', $context ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get shipping_tax_total |
198
|
|
|
* |
199
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
200
|
|
|
* @return string |
201
|
|
|
*/ |
202
|
12 |
|
public function get_shipping_tax_total( $context = 'view' ) { |
203
|
12 |
|
return $this->get_prop( 'shipping_tax_total', $context ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get compound. |
208
|
|
|
* |
209
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
11 |
|
public function get_compound( $context = 'view' ) { |
213
|
11 |
|
return $this->get_prop( 'compound', $context ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Is this a compound tax rate? |
218
|
|
|
* |
219
|
|
|
* @return boolean |
220
|
|
|
*/ |
221
|
|
|
public function is_compound() { |
222
|
|
|
return $this->get_compound(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get rate value |
227
|
|
|
* |
228
|
|
|
* @param string $context What the value is for. Valid values are 'view' and 'edit'. |
229
|
|
|
* @return float |
230
|
|
|
*/ |
231
|
10 |
|
public function get_rate_percent( $context = 'view' ) { |
232
|
10 |
|
return $this->get_prop( 'rate_percent', $context ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/* |
236
|
|
|
|-------------------------------------------------------------------------- |
237
|
|
|
| Array Access Methods |
238
|
|
|
|-------------------------------------------------------------------------- |
239
|
|
|
| |
240
|
|
|
| For backwards compatibility with legacy arrays. |
241
|
|
|
| |
242
|
|
|
*/ |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* O for ArrayAccess/Backwards compatibility. |
246
|
|
|
* |
247
|
|
|
* @deprecated Add deprecation notices in future release. |
248
|
|
|
* @param string $offset Offset. |
249
|
|
|
* @return mixed |
250
|
|
|
*/ |
251
|
1 |
View Code Duplication |
public function offsetGet( $offset ) { |
|
|
|
|
252
|
1 |
|
if ( 'tax_amount' === $offset ) { |
253
|
1 |
|
$offset = 'tax_total'; |
254
|
1 |
|
} elseif ( 'shipping_tax_amount' === $offset ) { |
255
|
1 |
|
$offset = 'shipping_tax_total'; |
256
|
|
|
} |
257
|
1 |
|
return parent::offsetGet( $offset ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* OffsetSet for ArrayAccess/Backwards compatibility. |
262
|
|
|
* |
263
|
|
|
* @deprecated Add deprecation notices in future release. |
264
|
|
|
* @param string $offset Offset. |
265
|
|
|
* @param mixed $value Value. |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
public function offsetSet( $offset, $value ) { |
|
|
|
|
268
|
|
|
if ( 'tax_amount' === $offset ) { |
269
|
|
|
$offset = 'tax_total'; |
270
|
|
|
} elseif ( 'shipping_tax_amount' === $offset ) { |
271
|
|
|
$offset = 'shipping_tax_total'; |
272
|
|
|
} |
273
|
|
|
parent::offsetSet( $offset, $value ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* OffsetExists for ArrayAccess. |
278
|
|
|
* |
279
|
|
|
* @param string $offset Offset. |
280
|
|
|
* @return bool |
281
|
|
|
*/ |
282
|
|
|
public function offsetExists( $offset ) { |
283
|
|
|
if ( in_array( $offset, array( 'tax_amount', 'shipping_tax_amount' ), true ) ) { |
284
|
|
|
return true; |
285
|
|
|
} |
286
|
|
|
return parent::offsetExists( $offset ); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
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.