Completed
Pull Request — master (#10259)
by Mike
13:41
created

WC_Order_Item_Product::offsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Order Line Item (product).
4
 *
5
 * @class 		WC_Order_Item_Product
6
 * @version		2.6.0
7
 * @since       2.6.0
8
 * @package		WooCommerce/Classes
9
 * @author 		WooThemes
10
 */
11
class WC_Order_Item_Product extends WC_Order_Item {
12
13
    /**
14
	 * Data properties of this order item object.
15
	 * @since 2.6.0
16
	 * @var array
17
	 */
18
    protected $_data = array(
19
        'order_id'      => 0,
20
		'order_item_id' => 0,
21
        'name'          => '',
22
        'product_id'    => 0,
23
        'variation_id'  => 0,
24
        'qty'           => 0,
25
        'tax_class'     => '',
26
        'subtotal'      => 0,
27
        'subtotal_tax'  => 0,
28
        'total'         => 0,
29
        'total_tax'     => 0,
30
        'taxes'         => array(
31
            'subtotal' => array(),
32
            'total'    => array()
33
        ),
34
        'meta_data'     => array(),
35
    );
36
37
    /**
38
     * offsetGet for ArrayAccess/Backwards compatibility.
39
     * @todo Add deprecation notices in future release.
40
     * @param string $offset
41
     * @return mixed
42
     */
43
    public function offsetGet( $offset ) {
44
        if ( 'line_subtotal' === $offset ) {
45
            $offset = 'subtotal';
46
        }
47
        elseif ( 'line_subtotal_tax' === $offset ) {
48
            $offset = 'subtotal_tax';
49
        }
50
        elseif ( 'line_total' === $offset ) {
51
            $offset = 'total';
52
        }
53
        elseif ( 'line_tax' === $offset ) {
54
            $offset = 'total_tax';
55
        }
56
        elseif ( 'line_tax_data' === $offset ) {
57
            $offset = 'taxes';
58
        }
59
        return parent::offsetGet( $offset );
60
    }
61
62
    /**
63
     * offsetExists for ArrayAccess
64
     * @param string $offset
65
     * @return bool
66
     */
67
    public function offsetExists( $offset ) {
68
        if ( in_array( $offset, array( 'line_subtotal', 'line_subtotal_tax', 'line_total', 'line_tax', 'line_tax_data', 'item_meta_array', 'item_meta' ) ) ) {
69
            return true;
70
        }
71
        return parent::offsetExists( $offset );
72
    }
73
74
    /**
75
     * Read/populate data properties specific to this order item.
76
     */
77
    public function read( $id ) {
78
        parent::read( $id );
79
        if ( $this->get_order_item_id() ) {
80
            $this->set_product_id( get_metadata( 'order_item', $this->get_order_item_id(), '_product_id', true ) );
81
            $this->set_variation_id( get_metadata( 'order_item', $this->get_order_item_id(), '_variation_id', true ) );
82
            $this->set_qty( get_metadata( 'order_item', $this->get_order_item_id(), '_qty', true ) );
83
            $this->set_tax_class( get_metadata( 'order_item', $this->get_order_item_id(), '_tax_class', true ) );
84
            $this->set_subtotal( get_metadata( 'order_item', $this->get_order_item_id(), '_line_subtotal', true ) );
85
            $this->set_subtotal_tax( get_metadata( 'order_item', $this->get_order_item_id(), '_line_subtotal_tax', true ) );
86
            $this->set_total( get_metadata( 'order_item', $this->get_order_item_id(), '_line_total', true ) );
87
            $this->set_total_tax( get_metadata( 'order_item', $this->get_order_item_id(), '_line_tax', true ) );
88
            $this->set_taxes( get_metadata( 'order_item', $this->get_order_item_id(), '_line_tax_data', true ) );
89
        }
90
    }
91
92
    /**
93
     * Save properties specific to this order item.
94
     */
95
    public function save() {
96
        parent::save();
97
        if ( $this->get_order_item_id() ) {
98
            wc_update_order_item_meta( $this->get_order_item_id(), '_product_id', $this->get_product_id() );
99
            wc_update_order_item_meta( $this->get_order_item_id(), '_variation_id', $this->get_variation_id() );
100
            wc_update_order_item_meta( $this->get_order_item_id(), '_qty', $this->get_qty() );
101
            wc_update_order_item_meta( $this->get_order_item_id(), '_tax_class', $this->get_tax_class() );
102
            wc_update_order_item_meta( $this->get_order_item_id(), '_line_subtotal', $this->get_subtotal() );
103
            wc_update_order_item_meta( $this->get_order_item_id(), '_line_subtotal_tax', $this->get_subtotal_tax() );
104
            wc_update_order_item_meta( $this->get_order_item_id(), '_line_total', $this->get_total() );
105
            wc_update_order_item_meta( $this->get_order_item_id(), '_line_tax', $this->get_total_tax() );
106
            wc_update_order_item_meta( $this->get_order_item_id(), '_line_tax_data', $this->get_taxes() );
107
        }
108
    }
109
110
    /**
111
     * Internal meta keys we don't want exposed as part of meta_data.
112
     * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
113
     */
114
    protected function get_internal_meta_keys() {
115
        return array( '_product_id', '_variation_id', '_qty', '_tax_class', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' );
116
    }
117
118
    /**
119
     * Get the associated product.
120
     * @return WC_Product|bool
121
     */
122
    public function get_product() {
123
        if ( $this->get_variation_id() ) {
124
			return wc_get_product( $this->get_variation_id() );
125
		} else {
126
			return wc_get_product( $this->get_product_id() );
127
		}
128
    }
129
130
    /**
131
     * Get tax status.
132
     * @return string
133
     */
134
    public function get_tax_status() {
135
        $product = $this->get_product();
136
        return $product ? $product->get_tax_class() : 'taxable';
137
    }
138
139
    /*
140
	|--------------------------------------------------------------------------
141
	| Setters
142
	|--------------------------------------------------------------------------
143
	*/
144
145
    /**
146
     * Set qty.
147
     * @param int $value
148
     */
149
    public function set_qty( $value ) {
150
        $this->_data['qty'] = wc_stock_amount( $value );
151
    }
152
153
    /**
154
     * Set tax class.
155
     * @param string $value
156
     */
157
    public function set_tax_class( $value ) {
158
        $this->_data['tax_class'] = $value;
159
    }
160
161
    /**
162
     * Set Product ID
163
     * @param int $value
164
     */
165
    public function set_product_id( $value ) {
166
        $this->_data['product_id'] = absint( $value );
167
    }
168
169
    /**
170
     * Set variation ID.
171
     * @param int $value
172
     */
173
    public function set_variation_id( $value ) {
174
        $this->_data['variation_id'] = absint( $value );
175
    }
176
177
    /**
178
     * Line subtotal (before discounts).
179
     * @param string $value
180
     */
181
    public function set_subtotal( $value ) {
182
        $this->_data['subtotal'] = wc_format_decimal( $value );
183
    }
184
185
    /**
186
     * Line total (after discounts).
187
     * @param string $value
188
     */
189
    public function set_total( $value ) {
190
        $this->_data['total'] = wc_format_decimal( $value );
191
    }
192
193
    /**
194
     * Line subtotal tax (before discounts).
195
     * @param string $value
196
     */
197
    public function set_subtotal_tax( $value ) {
198
        $this->_data['subtotal_tax'] = wc_format_decimal( $value );
199
    }
200
201
    /**
202
     * Line total tax (after discounts).
203
     * @param string $value
204
     */
205
    public function set_total_tax( $value ) {
206
        $this->_data['total_tax'] = wc_format_decimal( $value );
207
    }
208
209
    /**
210
     * Set line taxes.
211
     * @param array $raw_tax_data
212
     */
213
    public function set_taxes( $raw_tax_data ) {
214
        $raw_tax_data = maybe_unserialize( $raw_tax_data );
215
        $tax_data     = array(
216
            'total'    => array(),
217
            'subtotal' => array()
218
        );
219
        if ( ! empty( $raw_tax_data['total'] ) && ! empty( $raw_tax_data['subtotal'] ) ) {
220
            $tax_data['total']    = array_map( 'wc_format_decimal', $raw_tax_data['total'] );
221
            $tax_data['subtotal'] = array_map( 'wc_format_decimal', $raw_tax_data['subtotal'] );
222
        }
223
        $this->_data['taxes'] = $tax_data;
224
    }
225
226
    /**
227
     * Set variation data (stored as meta data - write only).
228
     * @param array $data Key/Value pairs
229
     */
230
    public function set_variations( $data ) {
231
        foreach ( $data as $key => $value ) {
232
            $this->_data['meta_data'][ str_replace( 'attribute_', '', $key ) ] = $value;
233
        }
234
    }
235
236
    /*
237
	|--------------------------------------------------------------------------
238
	| Getters
239
	|--------------------------------------------------------------------------
240
	*/
241
242
    /**
243
     * Get order item type.
244
     * @return string
245
     */
246
    public function get_type() {
247
        return 'line_item';
248
    }
249
250
    /**
251
     * Get product ID.
252
     * @return int
253
     */
254
    public function get_product_id() {
255
        return absint( $this->_data['product_id'] );
256
    }
257
258
    /**
259
     * Get variation ID.
260
     * @return int
261
     */
262
    public function get_variation_id() {
263
        return absint( $this->_data['variation_id'] );
264
    }
265
266
    /**
267
     * Get qty.
268
     * @return int
269
     */
270
    public function get_qty() {
271
        return wc_stock_amount( $this->_data['qty'] );
272
    }
273
274
    /**
275
     * Get tax class.
276
     * @return string
277
     */
278
    public function get_tax_class() {
279
        return $this->_data['tax_class'];
280
    }
281
282
    /**
283
     * Get subtotal.
284
     * @return string
285
     */
286
    public function get_subtotal() {
287
        return wc_format_decimal( $this->_data['subtotal'] );
288
    }
289
290
    /**
291
     * Get subtotal tax.
292
     * @return string
293
     */
294
    public function get_subtotal_tax() {
295
        return wc_format_decimal( $this->_data['subtotal_tax'] );
296
    }
297
298
    /**
299
     * Get total.
300
     * @return string
301
     */
302
    public function get_total() {
303
        return wc_format_decimal( $this->_data['total'] );
304
    }
305
306
    /**
307
     * Get total tax.
308
     * @return string
309
     */
310
    public function get_total_tax() {
311
        return wc_format_decimal( $this->_data['total_tax'] );
312
    }
313
314
    /**
315
     * Get fee taxes.
316
     * @return array
317
     */
318
    public function get_taxes() {
319
        return $this->_data['taxes'];
320
    }
321
322
    /**
323
     * Get meta data.
324
     * @return array of key/value pairs
325
     */
326
    public function get_meta_data() {
327
        return $this->_data['meta_data'];
328
    }
329
}
330