WC_Customer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
eloc 5
nc 2
nop 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
8
/**
9
 * Customer
10
 *
11
 * The WooCommerce customer class handles storage of the current customer's data, such as location.
12
 *
13
 * @class    WC_Customer
14
 * @version  2.3.0
15
 * @package  WooCommerce/Classes
16
 * @category Class
17
 * @author   WooThemes
18
 *
19
 * @property string $country
20
 * @property string $state
21
 * @property string $postcode
22
 * @property string $city
23
 * @property string $address_1
24
 * @property string $address_2
25
 * @property string $shipping_country
26
 * @property string $shipping_state
27
 * @property string $shipping_postcode
28
 * @property string $shipping_city
29
 * @property string $shipping_address_1
30
 * @property string $shipping_address_2
31
 * @property string $is_vat_exempt
32
 * @property string $calculated_shipping
33
 */
34
class WC_Customer {
35
36
	/**
37
	 * Stores customer data.
38
	 *
39
	 * @var array
40
	 */
41
	protected $_data = array();
42
43
	/**
44
	 * Stores bool when data is changed.
45
	 *
46
	 * @var bool
47
	 */
48
	private $_changed = false;
49
50
	/**
51
	 * Constructor for the customer class loads the customer data.
52
	 *
53
	 */
54
	public function __construct() {
55
		$this->_data = (array) WC()->session->get( 'customer' );
56
57
		// No data - set defaults
58
		if ( empty( $this->_data ) ) {
59
			$this->set_default_data();
60
		}
61
62
		// When leaving or ending page load, store data
63
		add_action( 'shutdown', array( $this, 'save_data' ), 10 );
64
	}
65
66
	/**
67
	 * Save data function.
68
	 */
69
	public function save_data() {
70
		if ( $this->_changed ) {
71
			WC()->session->set( 'customer', $this->_data );
72
		}
73
	}
74
75
	/**
76
	 * __set function.
77
	 *
78
	 * @param mixed $property
79
	 * @return bool
80
	 */
81 View Code Duplication
	public function __isset( $property ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
		if ( 'address' === $property ) {
83
			$property = 'address_1';
84
		}
85
		if ( 'shipping_address' === $property ) {
86
			$property = 'shipping_address_1';
87
		}
88
		return isset( $this->_data[ $property ] );
89
	}
90
91
	/**
92
	 * __get function.
93
	 *
94
	 * @param string $property
95
	 * @return string
96
	 */
97 View Code Duplication
	public function __get( $property ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
		if ( 'address' === $property ) {
99
			$property = 'address_1';
100
		}
101
		if ( 'shipping_address' === $property ) {
102
			$property = 'shipping_address_1';
103
		}
104
		return isset( $this->_data[ $property ] ) ? $this->_data[ $property ] : '';
105
	}
106
107
	/**
108
	 * __set function.
109
	 *
110
	 * @param mixed $property
111
	 * @param mixed $value
112
	 */
113
	public function __set( $property, $value ) {
114
		if ( 'address' === $property ) {
115
			$property = 'address_1';
116
		}
117
		if ( 'shipping_address' === $property ) {
118
			$property = 'shipping_address_1';
119
		}
120
		$this->_data[ $property ] = $value;
121
		$this->_changed = true;
122
	}
123
124
	/**
125
	 * Get default country for a customer.
126
	 *
127
	 * @return string
128
	 */
129
	public function get_default_country() {
130
		$default = wc_get_customer_default_location();
131
		return $default['country'];
132
	}
133
134
	/**
135
	 * Get default state for a customer.
136
	 *
137
	 * @return string
138
	 */
139
	public function get_default_state() {
140
		$default = wc_get_customer_default_location();
141
		return $default['state'];
142
	}
143
144
	/**
145
	 * Has calculated shipping?
146
	 *
147
	 * @return bool
148
	 */
149
	public function has_calculated_shipping() {
150
		return ! empty( $this->calculated_shipping );
151
	}
152
153
	/**
154
	 * Set customer address to match shop base address.
155
	 */
156
	public function set_to_base() {
157
		$this->country  = $this->get_default_country();
158
		$this->state    = $this->get_default_state();
159
		$this->postcode = '';
160
		$this->city     = '';
161
	}
162
163
	/**
164
	 * Set customer shipping address to base address.
165
	 */
166
	public function set_shipping_to_base() {
167
		$this->shipping_country  = $this->get_default_country();
168
		$this->shipping_state    = $this->get_default_state();
169
		$this->shipping_postcode = '';
170
		$this->shipping_city     = '';
171
	}
172
173
	/**
174
	 * Is customer outside base country (for tax purposes)?
175
	 *
176
	 * @return bool
177
	 */
178
	public function is_customer_outside_base() {
179
		list( $country, $state ) = $this->get_taxable_address();
180
181
		if ( $country ) {
182
183
			$default = wc_get_base_location();
184
185
			if ( $default['country'] !== $country ) {
186
				return true;
187
			}
188
189
			if ( $default['state'] && $default['state'] !== $state ) {
190
				return true;
191
			}
192
193
		}
194
195
		return false;
196
	}
197
198
	/**
199
	 * Is the user a paying customer?
200
	 *
201
	 * @return bool
202
	 */
203
	public function is_paying_customer( $user_id ) {
204
		return '1' === get_user_meta( $user_id, 'paying_customer', true );
205
	}
206
207
	/**
208
	 * Is customer VAT exempt?
209
	 *
210
	 * @return bool
211
	 */
212
	public function is_vat_exempt() {
213
		return ( ! empty( $this->is_vat_exempt ) ) ? true : false;
214
	}
215
216
	/**
217
	 * Gets the state from the current session.
218
	 *
219
	 * @return string
220
	 */
221
	public function get_state() {
222
		return $this->state;
223
	}
224
225
	/**
226
	 * Gets the country from the current session.
227
	 *
228
	 * @return string
229
	 */
230
	public function get_country() {
231
		return $this->country;
232
	}
233
234
	/**
235
	 * Gets the postcode from the current session.
236
	 *
237
	 * @return string
238
	 */
239
	public function get_postcode() {
240
		return empty( $this->postcode ) ? '' : wc_format_postcode( $this->postcode, $this->get_country() );
241
	}
242
243
	/**
244
	 * Get the city from the current session.
245
	 *
246
	 * @return string
247
	 */
248
	public function get_city() {
249
		return $this->city;
250
	}
251
252
	/**
253
	 * Gets the address from the current session.
254
	 *
255
	 * @return string
256
	 */
257
	public function get_address() {
258
		return $this->address_1;
259
	}
260
261
	/**
262
	 * Gets the address_2 from the current session.
263
	 *
264
	 * @return string
265
	 */
266
	public function get_address_2() {
267
		return $this->address_2;
268
	}
269
270
	/**
271
	 * Gets the state from the current session.
272
	 *
273
	 * @return string
274
	 */
275
	public function get_shipping_state() {
276
		return $this->shipping_state;
277
	}
278
279
	/**
280
	 * Gets the country from the current session.
281
	 *
282
	 * @return string
283
	 */
284
	public function get_shipping_country() {
285
		return $this->shipping_country;
286
	}
287
288
	/**
289
	 * Gets the postcode from the current session.
290
	 *
291
	 * @return string
292
	 */
293
	public function get_shipping_postcode() {
294
		return empty( $this->shipping_postcode ) ? '' : wc_format_postcode( $this->shipping_postcode, $this->get_shipping_country() );
295
	}
296
297
	/**
298
	 * Gets the city from the current session.
299
	 *
300
	 * @return string
301
	 */
302
	public function get_shipping_city() {
303
		return $this->shipping_city;
304
	}
305
306
	/**
307
	 * Gets the address from the current session.
308
	 *
309
	 * @return string
310
	 */
311
	public function get_shipping_address() {
312
		return $this->shipping_address_1;
313
	}
314
315
	/**
316
	 * Gets the address_2 from the current session.
317
	 *
318
	 * @return string
319
	 */
320
	public function get_shipping_address_2() {
321
		return $this->shipping_address_2;
322
	}
323
324
	/**
325
	 * Get taxable address.
326
	 *
327
	 * @return array
328
	 */
329
	public function get_taxable_address() {
330
		$tax_based_on = get_option( 'woocommerce_tax_based_on' );
331
332
		// Check shipping method at this point to see if we need special handling
333
		if ( true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && sizeof( array_intersect( WC()->session->get( 'chosen_shipping_methods', array() ), apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) ) ) ) > 0 ) {
334
			$tax_based_on = 'base';
335
		}
336
337
		if ( 'base' === $tax_based_on ) {
338
339
			$country  = WC()->countries->get_base_country();
340
			$state    = WC()->countries->get_base_state();
341
			$postcode = WC()->countries->get_base_postcode();
342
			$city     = WC()->countries->get_base_city();
343
344
		} elseif ( 'billing' === $tax_based_on ) {
345
			$country  = $this->get_country();
346
			$state    = $this->get_state();
347
			$postcode = $this->get_postcode();
348
			$city     = $this->get_city();
349
350
		} else {
351
			$country  = $this->get_shipping_country();
352
			$state    = $this->get_shipping_state();
353
			$postcode = $this->get_shipping_postcode();
354
			$city     = $this->get_shipping_city();
355
		}
356
357
		return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
358
	}
359
360
	/**
361
	 * Set default data for a customer.
362
	 */
363
	public function set_default_data( $get_user_profile_data = true ) {
364
		$this->_data = array(
365
			'postcode'            => '',
366
			'city'                => '',
367
			'address_1'           => '',
368
			'address_2'           => '',
369
			'state'               => '',
370
			'country'             => '',
371
			'shipping_postcode'   => '',
372
			'shipping_city'       => '',
373
			'shipping_address_1'  => '',
374
			'shipping_address_2'  => '',
375
			'shipping_state'      => '',
376
			'shipping_country'    => '',
377
			'is_vat_exempt'       => false,
378
			'calculated_shipping' => false
379
		);
380
381
		if ( is_user_logged_in() && $get_user_profile_data ) {
382
			foreach ( $this->_data as $key => $value ) {
383
				$meta_value          = get_user_meta( get_current_user_id(), ( false === strstr( $key, 'shipping_' ) ? 'billing_' : '' ) . $key, true );
384
				$this->_data[ $key ] = $meta_value ? $meta_value : $this->_data[ $key ];
385
			}
386
		}
387
388
		if ( empty( $this->_data['country'] ) ) {
389
			$this->_data['country'] = $this->get_default_country();
390
		}
391
392
		if ( empty( $this->_data['shipping_country'] ) ) {
393
			$this->_data['shipping_country'] = $this->_data['country'];
394
		}
395
396
		if ( empty( $this->_data['state'] ) ) {
397
			$this->_data['state'] = $this->get_default_state();
398
		}
399
400
		if ( empty( $this->_data['shipping_state'] ) ) {
401
			$this->_data['shipping_state'] = $this->_data['state'];
402
		}
403
	}
404
405
	/**
406
	 * Sets session data for the location.
407
	 *
408
	 * @param string $country
409
	 * @param string $state
410
	 * @param string $postcode (default: '')
411
	 * @param string $city (default: '')
412
	 */
413
	public function set_location( $country, $state, $postcode = '', $city = '' ) {
414
		$this->country  = $country;
415
		$this->state    = $state;
416
		$this->postcode = $postcode;
417
		$this->city     = $city;
418
	}
419
420
	/**
421
	 * Sets session data for the country.
422
	 *
423
	 * @param mixed $country
424
	 */
425
	public function set_country( $country ) {
426
		$this->country = $country;
427
	}
428
429
	/**
430
	 * Sets session data for the state.
431
	 *
432
	 * @param mixed $state
433
	 */
434
	public function set_state( $state ) {
435
		$this->state = $state;
436
	}
437
438
	/**
439
	 * Sets session data for the postcode.
440
	 *
441
	 * @param mixed $postcode
442
	 */
443
	public function set_postcode( $postcode ) {
444
		$this->postcode = $postcode;
445
	}
446
447
	/**
448
	 * Sets session data for the city.
449
	 *
450
	 * @param mixed $city
451
	 */
452
	public function set_city( $city ) {
453
		$this->city = $city;
454
	}
455
456
	/**
457
	 * Sets session data for the address.
458
	 *
459
	 * @param mixed $address
460
	 */
461
	public function set_address( $address ) {
462
		$this->address_1 = $address;
463
	}
464
465
	/**
466
	 * Sets session data for the $address.
467
	 *
468
	 * @param mixed $address
469
	 */
470
	public function set_address_2( $address ) {
471
		$this->address_2 = $address;
472
	}
473
474
	/**
475
	 * Sets session data for the location.
476
	 *
477
	 * @param string $country
478
	 * @param string $state (default: '')
479
	 * @param string $postcode (default: '')
480
	 * @param string $city (default: '')
481
	 */
482
	public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
483
		$this->shipping_country  = $country;
484
		$this->shipping_state    = $state;
485
		$this->shipping_postcode = $postcode;
486
		$this->shipping_city     = $city;
487
	}
488
489
	/**
490
	 * Sets session data for the country.
491
	 *
492
	 * @param string $country
493
	 */
494
	public function set_shipping_country( $country ) {
495
		$this->shipping_country = $country;
496
	}
497
498
	/**
499
	 * Sets session data for the state.
500
	 *
501
	 * @param string $state
502
	 */
503
	public function set_shipping_state( $state ) {
504
		$this->shipping_state = $state;
505
	}
506
507
	/**
508
	 * Sets session data for the postcode.
509
	 *
510
	 * @param string $postcode
511
	 */
512
	public function set_shipping_postcode( $postcode ) {
513
		$this->shipping_postcode = $postcode;
514
	}
515
516
	/**
517
	 * Sets session data for the city.
518
	 *
519
	 * @param string $city
520
	 */
521
	public function set_shipping_city( $city ) {
522
		$this->shipping_city = $city;
523
	}
524
525
	/**
526
	 * Sets session data for the address.
527
	 *
528
	 * @param string $address
529
	 */
530
	public function set_shipping_address( $address ) {
531
		$this->shipping_address_1 = $address;
532
	}
533
534
	/**
535
	 * Sets session data for the address_2.
536
	 *
537
	 * @param string $address
538
	 */
539
	public function set_shipping_address_2( $address ) {
540
		$this->shipping_address_2 = $address;
541
	}
542
543
	/**
544
	 * Sets session data for the tax exemption.
545
	 *
546
	 * @param bool $is_vat_exempt
547
	 */
548
	public function set_is_vat_exempt( $is_vat_exempt ) {
549
		$this->is_vat_exempt = $is_vat_exempt;
550
	}
551
552
	/**
553
	 * Calculated shipping.
554
	 *
555
	 * @param boolean $calculated
556
	 */
557
	public function calculated_shipping( $calculated = true ) {
558
		$this->calculated_shipping = $calculated;
559
	}
560
561
	/**
562
	 * Gets a user's downloadable products if they are logged in.
563
	 *
564
	 * @return array Array of downloadable products
565
	 */
566
	public function get_downloadable_products() {
567
		$downloads = array();
568
569
		if ( is_user_logged_in() ) {
570
			$downloads = wc_get_customer_available_downloads( get_current_user_id() );
571
		}
572
573
		return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
574
	}
575
}
576