1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Order Line Item (fee). |
8
|
|
|
* |
9
|
|
|
* @version 2.7.0 |
10
|
|
|
* @since 2.7.0 |
11
|
|
|
* @package WooCommerce/Classes |
12
|
|
|
* @author WooThemes |
13
|
|
|
*/ |
14
|
|
|
class WC_Order_Item_Fee extends WC_Order_Item { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Data properties of this order item object. |
18
|
|
|
* @since 2.7.0 |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $_data = array( |
22
|
|
|
'order_id' => 0, |
23
|
|
|
'id' => 0, |
24
|
|
|
'name' => '', |
25
|
|
|
'tax_class' => '', |
26
|
|
|
'tax_status' => 'taxable', |
27
|
|
|
'total' => '', |
28
|
|
|
'total_tax' => '', |
29
|
|
|
'taxes' => array( |
30
|
|
|
'total' => array() |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* offsetGet for ArrayAccess/Backwards compatibility. |
36
|
|
|
* @deprecated Add deprecation notices in future release. |
37
|
|
|
* @param string $offset |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function offsetGet( $offset ) { |
|
|
|
|
41
|
|
|
if ( 'line_total' === $offset ) { |
42
|
|
|
$offset = 'total'; |
43
|
|
|
} elseif ( 'line_tax' === $offset ) { |
44
|
|
|
$offset = 'total_tax'; |
45
|
|
|
} elseif ( 'line_tax_data' === $offset ) { |
46
|
|
|
$offset = 'taxes'; |
47
|
|
|
} |
48
|
|
|
return parent::offsetGet( $offset ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* offsetSet for ArrayAccess/Backwards compatibility. |
53
|
|
|
* @deprecated Add deprecation notices in future release. |
54
|
|
|
* @param string $offset |
55
|
|
|
* @param mixed $value |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function offsetSet( $offset, $value ) { |
|
|
|
|
58
|
|
|
if ( 'line_total' === $offset ) { |
59
|
|
|
$offset = 'total'; |
60
|
|
|
} elseif ( 'line_tax' === $offset ) { |
61
|
|
|
$offset = 'total_tax'; |
62
|
|
|
} elseif ( 'line_tax_data' === $offset ) { |
63
|
|
|
$offset = 'taxes'; |
64
|
|
|
} |
65
|
|
|
parent::offsetSet( $offset, $value ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* offsetExists for ArrayAccess |
70
|
|
|
* @param string $offset |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function offsetExists( $offset ) { |
74
|
|
|
if ( in_array( $offset, array( 'line_total', 'line_tax', 'line_tax_data' ) ) ) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
return parent::offsetExists( $offset ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Read/populate data properties specific to this order item. |
82
|
|
|
*/ |
83
|
|
|
public function read( $id ) { |
84
|
|
|
parent::read( $id ); |
85
|
|
|
if ( $this->get_id() ) { |
86
|
|
|
$this->set_tax_class( get_metadata( 'order_item', $this->get_id(), '_tax_class', true ) ); |
87
|
|
|
$this->set_tax_status( get_metadata( 'order_item', $this->get_id(), '_tax_status', true ) ); |
88
|
|
|
$this->set_total( get_metadata( 'order_item', $this->get_id(), '_line_total', true ) ); |
89
|
|
|
$this->set_total_tax( get_metadata( 'order_item', $this->get_id(), '_line_tax', true ) ); |
90
|
|
|
$this->set_taxes( get_metadata( 'order_item', $this->get_id(), '_line_tax_data', true ) ); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Save properties specific to this order item. |
96
|
|
|
* @return int Item ID |
97
|
|
|
*/ |
98
|
|
|
public function save() { |
99
|
|
|
parent::save(); |
100
|
|
|
if ( $this->get_id() ) { |
101
|
|
|
wc_update_order_item_meta( $this->get_id(), '_tax_class', $this->get_tax_class() ); |
102
|
|
|
wc_update_order_item_meta( $this->get_id(), '_tax_status', $this->get_tax_status() ); |
103
|
|
|
wc_update_order_item_meta( $this->get_id(), '_line_total', $this->get_total() ); |
104
|
|
|
wc_update_order_item_meta( $this->get_id(), '_line_tax', $this->get_total_tax() ); |
105
|
|
|
wc_update_order_item_meta( $this->get_id(), '_line_tax_data', $this->get_taxes() ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->get_id(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Internal meta keys we don't want exposed as part of meta_data. |
113
|
|
|
* @return array() |
|
|
|
|
114
|
|
|
*/ |
115
|
|
|
protected function get_internal_meta_keys() { |
116
|
|
|
return array( '_tax_class', '_tax_status', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
|-------------------------------------------------------------------------- |
121
|
|
|
| Setters |
122
|
|
|
|-------------------------------------------------------------------------- |
123
|
|
|
*/ |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set tax class. |
127
|
|
|
* @param string $value |
128
|
|
|
*/ |
129
|
|
|
public function set_tax_class( $value ) { |
130
|
|
|
if ( $value && ! in_array( $value, WC_Tax::get_tax_classes() ) ) { |
|
|
|
|
131
|
|
|
//$this->throw_exception( __METHOD__, 'Invalid tax class' ); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
$this->_data['tax_class'] = $value; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set tax_status. |
138
|
|
|
* @param string $value |
139
|
|
|
*/ |
140
|
|
|
public function set_tax_status( $value ) { |
141
|
|
|
if ( in_array( $value, array( 'taxable', 'none' ) ) ) { |
142
|
|
|
$this->_data['tax_status'] = $value; |
143
|
|
|
} else { |
144
|
|
|
$this->_data['tax_status'] = 'taxable'; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set total. |
150
|
|
|
* @param string $value |
151
|
|
|
*/ |
152
|
|
|
public function set_total( $value ) { |
153
|
|
|
$this->_data['total'] = wc_format_decimal( $value ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set total tax. |
158
|
|
|
* @param string $value |
159
|
|
|
*/ |
160
|
|
|
public function set_total_tax( $value ) { |
161
|
|
|
$this->_data['total_tax'] = wc_format_decimal( $value ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Set taxes. |
166
|
|
|
* |
167
|
|
|
* This is an array of tax ID keys with total amount values. |
168
|
|
|
* @param array $raw_tax_data |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function set_taxes( $raw_tax_data ) { |
|
|
|
|
171
|
|
|
$raw_tax_data = maybe_unserialize( $raw_tax_data ); |
172
|
|
|
$tax_data = array( |
173
|
|
|
'total' => array(), |
174
|
|
|
); |
175
|
|
|
if ( ! empty( $raw_tax_data['total'] ) ) { |
176
|
|
|
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data['total'] ); |
177
|
|
|
} |
178
|
|
|
$this->_data['taxes'] = $tax_data; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/* |
182
|
|
|
|-------------------------------------------------------------------------- |
183
|
|
|
| Getters |
184
|
|
|
|-------------------------------------------------------------------------- |
185
|
|
|
*/ |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get order item name. |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function get_name() { |
192
|
|
|
return $this->_data['name'] ? $this->_data['name'] : __( 'Fee', 'woocommerce' ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get order item type. |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function get_type() { |
200
|
|
|
return 'fee'; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get tax class. |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function get_tax_class() { |
208
|
|
|
return $this->_data['tax_class']; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Get tax status. |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public function get_tax_status() { |
216
|
|
|
return $this->_data['tax_status']; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get total fee. |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
public function get_total() { |
224
|
|
|
return wc_format_decimal( $this->_data['total'] ); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get total tax. |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function get_total_tax() { |
232
|
|
|
return wc_format_decimal( $this->_data['total_tax'] ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get fee taxes. |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
public function get_taxes() { |
240
|
|
|
return $this->_data['taxes']; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
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.