WC_Customer::get_is_vat_exempt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The WooCommerce customer class handles storage of the current customer's data, such as location.
4
 *
5
 * @package WooCommerce/Classes
6
 * @version 3.0.0
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
require_once dirname( __FILE__ ) . '/legacy/class-wc-legacy-customer.php';
12
13
/**
14
 * Customer class.
15
 */
16
class WC_Customer extends WC_Legacy_Customer {
17
18
	/**
19
	 * Stores customer data.
20
	 *
21
	 * @var array
22
	 */
23
	protected $data = array(
24
		'date_created'       => null,
25
		'date_modified'      => null,
26
		'email'              => '',
27
		'first_name'         => '',
28
		'last_name'          => '',
29
		'display_name'       => '',
30
		'role'               => 'customer',
31
		'username'           => '',
32
		'billing'            => array(
33
			'first_name' => '',
34
			'last_name'  => '',
35
			'company'    => '',
36
			'address_1'  => '',
37
			'address_2'  => '',
38
			'city'       => '',			
39
			'postcode'   => '',
40
			'country'    => '',
41
			'state'      => '',
42
			'email'      => '',
43
			'phone'      => '',
44
		),
45
		'shipping'           => array(
46
			'first_name' => '',
47
			'last_name'  => '',
48
			'company'    => '',
49
			'address_1'  => '',
50
			'address_2'  => '',
51
			'city'       => '',			
52
			'postcode'   => '',
53
			'country'    => '',
54
			'state'      => '',
55
		),
56
		'is_paying_customer' => false,
57
	);
58
59
	/**
60
	 * Stores a password if this needs to be changed. Write-only and hidden from _data.
61
	 *
62
	 * @var string
63
	 */
64
	protected $password = '';
65
66
	/**
67
	 * Stores if user is VAT exempt for this session.
68
	 *
69
	 * @var string
70
	 */
71
	protected $is_vat_exempt = false;
72
73
	/**
74
	 * Stores if user has calculated shipping in this session.
75
	 *
76
	 * @var string
77
	 */
78
	protected $calculated_shipping = false;
79
80
	/**
81
	 * Load customer data based on how WC_Customer is called.
82
	 *
83
	 * If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
84
	 * data will be pulled from the session for the current user/customer.
85
	 *
86
	 * @param WC_Customer|int $data       Customer ID or data.
87
	 * @param bool            $is_session True if this is the customer session.
88
	 * @throws Exception If customer cannot be read/found and $data is set.
89
	 */
90 46
	public function __construct( $data = 0, $is_session = false ) {
91 46
		parent::__construct( $data );
92
93 46 View Code Duplication
		if ( $data instanceof WC_Customer ) {
94
			$this->set_id( absint( $data->get_id() ) );
95 46
		} elseif ( is_numeric( $data ) ) {
96 46
			$this->set_id( $data );
97
		}
98
99 46
		$this->data_store = WC_Data_Store::load( 'customer' );
100
101
		// If we have an ID, load the user from the DB.
102 46 View Code Duplication
		if ( $this->get_id() ) {
103
			try {
104 24
				$this->data_store->read( $this );
105
			} catch ( Exception $e ) {
106
				$this->set_id( 0 );
107 24
				$this->set_object_read( true );
108
			}
109
		} else {
110 33
			$this->set_object_read( true );
111
		}
112
113
		// If this is a session, set or change the data store to sessions. Changes do not persist in the database.
114 46
		if ( $is_session ) {
115 4
			$this->data_store = WC_Data_Store::load( 'customer-session' );
116 4
			$this->data_store->read( $this );
117
		}
118
	}
119
120
	/**
121
	 * Prefix for action and filter hooks on data.
122
	 *
123
	 * @since  3.0.0
124
	 * @return string
125
	 */
126 119
	protected function get_hook_prefix() {
127 119
		return 'woocommerce_customer_get_';
128
	}
129
130
	/**
131
	 * Delete a customer and reassign posts..
132
	 *
133
	 * @param int $reassign Reassign posts and links to new User ID.
134
	 * @since 3.0.0
135
	 * @return bool
136
	 */
137 View Code Duplication
	public function delete_and_reassign( $reassign = null ) {
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...
138
		if ( $this->data_store ) {
139
			$this->data_store->delete(
140
				$this,
141
				array(
142
					'force_delete' => true,
143
					'reassign'     => $reassign,
144
				)
145
			);
146
			$this->set_id( 0 );
147
			return true;
148
		}
149
		return false;
150
	}
151
152
	/**
153
	 * Is customer outside base country (for tax purposes)?
154
	 *
155
	 * @return bool
156
	 */
157 2
	public function is_customer_outside_base() {
158 2
		list( $country, $state ) = $this->get_taxable_address();
159 2
		if ( $country ) {
160 2
			$default = wc_get_base_location();
161 2
			if ( $default['country'] !== $country ) {
162 2
				return true;
163
			}
164 2
			if ( $default['state'] && $default['state'] !== $state ) {
165
				return true;
166
			}
167
		}
168 2
		return false;
169
	}
170
171
	/**
172
	 * Return this customer's avatar.
173
	 *
174
	 * @since 3.0.0
175
	 * @return string
176
	 */
177 1
	public function get_avatar_url() {
178 1
		return get_avatar_url( $this->get_email() );
179
	}
180
181
	/**
182
	 * Get taxable address.
183
	 *
184
	 * @return array
185
	 */
186 95
	public function get_taxable_address() {
187 95
		$tax_based_on = get_option( 'woocommerce_tax_based_on' );
188
189
		// Check shipping method at this point to see if we need special handling.
190 95
		if ( true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && count( array_intersect( wc_get_chosen_shipping_method_ids(), apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) ) ) ) > 0 ) {
191 2
			$tax_based_on = 'base';
192
		}
193
194 95
		if ( 'base' === $tax_based_on ) {
195 17
			$country  = WC()->countries->get_base_country();
196 17
			$state    = WC()->countries->get_base_state();
197 17
			$postcode = WC()->countries->get_base_postcode();
198 17
			$city     = WC()->countries->get_base_city();
199 82
		} elseif ( 'billing' === $tax_based_on ) {
200 3
			$country  = $this->get_billing_country();
201 3
			$state    = $this->get_billing_state();
202 3
			$postcode = $this->get_billing_postcode();
203 3
			$city     = $this->get_billing_city();
204
		} else {
205 81
			$country  = $this->get_shipping_country();
206 81
			$state    = $this->get_shipping_state();
207 81
			$postcode = $this->get_shipping_postcode();
208 81
			$city     = $this->get_shipping_city();
209
		}
210
211 95
		return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
212
	}
213
214
	/**
215
	 * Gets a customer's downloadable products.
216
	 *
217
	 * @return array Array of downloadable products
218
	 */
219 1
	public function get_downloadable_products() {
220 1
		$downloads = array();
221 1
		if ( $this->get_id() ) {
222 1
			$downloads = wc_get_customer_available_downloads( $this->get_id() );
223
		}
224 1
		return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
225
	}
226
227
	/**
228
	 * Is customer VAT exempt?
229
	 *
230
	 * @return bool
231
	 */
232 1
	public function is_vat_exempt() {
233 1
		return $this->get_is_vat_exempt();
234
	}
235
236
	/**
237
	 * Has calculated shipping?
238
	 *
239
	 * @return bool
240
	 */
241 1
	public function has_calculated_shipping() {
242 1
		return $this->get_calculated_shipping();
243
	}
244
245
	/**
246
	 * Get if customer is VAT exempt?
247
	 *
248
	 * @since 3.0.0
249
	 * @return bool
250
	 */
251 80
	public function get_is_vat_exempt() {
252 80
		return $this->is_vat_exempt;
253
	}
254
255
	/**
256
	 * Get password (only used when updating the user object).
257
	 *
258
	 * @return string
259
	 */
260 38
	public function get_password() {
261 38
		return $this->password;
262
	}
263
264
	/**
265
	 * Has customer calculated shipping?
266
	 *
267
	 * @return bool
268
	 */
269 3
	public function get_calculated_shipping() {
270 3
		return $this->calculated_shipping;
271
	}
272
273
	/**
274
	 * Set if customer has tax exemption.
275
	 *
276
	 * @param bool $is_vat_exempt If is vat exempt.
277
	 */
278 56
	public function set_is_vat_exempt( $is_vat_exempt ) {
279 56
		$this->is_vat_exempt = wc_string_to_bool( $is_vat_exempt );
0 ignored issues
show
Documentation introduced by
$is_vat_exempt is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
The property $is_vat_exempt was declared of type string, but wc_string_to_bool($is_vat_exempt) is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
280
	}
281
282
	/**
283
	 * Calculated shipping?
284
	 *
285
	 * @param bool $calculated If shipping is calculated.
286
	 */
287 5
	public function set_calculated_shipping( $calculated = true ) {
288 5
		$this->calculated_shipping = wc_string_to_bool( $calculated );
0 ignored issues
show
Documentation introduced by
$calculated is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
The property $calculated_shipping was declared of type string, but wc_string_to_bool($calculated) is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
289
	}
290
291
	/**
292
	 * Set customer's password.
293
	 *
294
	 * @since 3.0.0
295
	 * @param string $password Password.
296
	 */
297 28
	public function set_password( $password ) {
298 28
		$this->password = $password;
299
	}
300
301
	/**
302
	 * Gets the customers last order.
303
	 *
304
	 * @return WC_Order|false
305
	 */
306 2
	public function get_last_order() {
307 2
		return $this->data_store->get_last_order( $this );
308
	}
309
310
	/**
311
	 * Return the number of orders this customer has.
312
	 *
313
	 * @return integer
314
	 */
315 4
	public function get_order_count() {
316 4
		return $this->data_store->get_order_count( $this );
317
	}
318
319
	/**
320
	 * Return how much money this customer has spent.
321
	 *
322
	 * @return float
323
	 */
324 2
	public function get_total_spent() {
325 2
		return $this->data_store->get_total_spent( $this );
326
	}
327
328
	/*
329
	|--------------------------------------------------------------------------
330
	| Getters
331
	|--------------------------------------------------------------------------
332
	*/
333
334
	/**
335
	 * Return the customer's username.
336
	 *
337
	 * @since  3.0.0
338
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
339
	 * @return string
340
	 */
341 29
	public function get_username( $context = 'view' ) {
342 29
		return $this->get_prop( 'username', $context );
343
	}
344
345
	/**
346
	 * Return the customer's email.
347
	 *
348
	 * @since  3.0.0
349
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
350
	 * @return string
351
	 */
352 39
	public function get_email( $context = 'view' ) {
353 39
		return $this->get_prop( 'email', $context );
354
	}
355
356
	/**
357
	 * Return customer's first name.
358
	 *
359
	 * @since  3.0.0
360
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
361
	 * @return string
362
	 */
363 27
	public function get_first_name( $context = 'view' ) {
364 27
		return $this->get_prop( 'first_name', $context );
365
	}
366
367
	/**
368
	 * Return customer's last name.
369
	 *
370
	 * @since  3.0.0
371
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
372
	 * @return string
373
	 */
374 2
	public function get_last_name( $context = 'view' ) {
375 2
		return $this->get_prop( 'last_name', $context );
376
	}
377
378
	/**
379
	 * Return customer's display name.
380
	 *
381
	 * @since  3.1.0
382
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
383
	 * @return string
384
	 */
385 39
	public function get_display_name( $context = 'view' ) {
386 39
		return $this->get_prop( 'display_name', $context );
387
	}
388
389
	/**
390
	 * Return customer's user role.
391
	 *
392
	 * @since  3.0.0
393
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
394
	 * @return string
395
	 */
396 29
	public function get_role( $context = 'view' ) {
397 29
		return $this->get_prop( 'role', $context );
398
	}
399
400
	/**
401
	 * Return the date this customer was created.
402
	 *
403
	 * @since  3.0.0
404
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
405
	 * @return WC_DateTime|null object if the date is set or null if there is no date.
406
	 */
407 3
	public function get_date_created( $context = 'view' ) {
408 3
		return $this->get_prop( 'date_created', $context );
409
	}
410
411
	/**
412
	 * Return the date this customer was last updated.
413
	 *
414
	 * @since  3.0.0
415
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
416
	 * @return WC_DateTime|null object if the date is set or null if there is no date.
417
	 */
418 5
	public function get_date_modified( $context = 'view' ) {
419 5
		return $this->get_prop( 'date_modified', $context );
420
	}
421
422
	/**
423
	 * Gets a prop for a getter method.
424
	 *
425
	 * @since  3.0.0
426
	 * @param  string $prop Name of prop to get.
427
	 * @param  string $address billing or shipping.
428
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'. What the value is for. Valid values are view and edit.
429
	 * @return mixed
430
	 */
431 112 View Code Duplication
	protected function get_address_prop( $prop, $address = 'billing', $context = 'view' ) {
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...
432 112
		$value = null;
433
434 112
		if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
435 112
			$value = isset( $this->changes[ $address ][ $prop ] ) ? $this->changes[ $address ][ $prop ] : $this->data[ $address ][ $prop ];
436
437 112
			if ( 'view' === $context ) {
438 102
				$value = apply_filters( $this->get_hook_prefix() . $address . '_' . $prop, $value, $this );
439
			}
440
		}
441 112
		return $value;
442
	}
443
444
	/**
445
	 * Get billing.
446
	 *
447
	 * @since  3.2.0
448
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
449
	 * @return array
450
	 */
451 1
	public function get_billing( $context = 'view' ) {
452 1
		return $this->get_prop( 'billing', $context );
453
	}
454
455
	/**
456
	 * Get billing_first_name.
457
	 *
458
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
459
	 * @return string
460
	 */
461 2
	public function get_billing_first_name( $context = 'view' ) {
462 2
		return $this->get_address_prop( 'first_name', 'billing', $context );
463
	}
464
465
	/**
466
	 * Get billing_last_name.
467
	 *
468
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
469
	 * @return string
470
	 */
471 2
	public function get_billing_last_name( $context = 'view' ) {
472 2
		return $this->get_address_prop( 'last_name', 'billing', $context );
473
	}
474
475
	/**
476
	 * Get billing_company.
477
	 *
478
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
479
	 * @return string
480
	 */
481 2
	public function get_billing_company( $context = 'view' ) {
482 2
		return $this->get_address_prop( 'company', 'billing', $context );
483
	}
484
485
	/**
486
	 * Get billing_address_1.
487
	 *
488
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
489
	 * @return string
490
	 */
491 3
	public function get_billing_address( $context = 'view' ) {
492 3
		return $this->get_billing_address_1( $context );
493
	}
494
495
	/**
496
	 * Get billing_address_1.
497
	 *
498
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
499
	 * @return string
500
	 */
501 27
	public function get_billing_address_1( $context = 'view' ) {
502 27
		return $this->get_address_prop( 'address_1', 'billing', $context );
503
	}
504
505
	/**
506
	 * Get billing_address_2.
507
	 *
508
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
509
	 * @return string $value
510
	 */
511 27
	public function get_billing_address_2( $context = 'view' ) {
512 27
		return $this->get_address_prop( 'address_2', 'billing', $context );
513
	}
514
515
	/**
516
	 * Get billing_city.
517
	 *
518
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
519
	 * @return string $value
520
	 */
521 29
	public function get_billing_city( $context = 'view' ) {
522 29
		return $this->get_address_prop( 'city', 'billing', $context );
523
	}
524
525
	/**
526
	 * Get billing_state.
527
	 *
528
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
529
	 * @return string
530
	 */
531 30
	public function get_billing_state( $context = 'view' ) {
532 30
		return $this->get_address_prop( 'state', 'billing', $context );
533
	}
534
535
	/**
536
	 * Get billing_postcode.
537
	 *
538
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
539
	 * @return string
540
	 */
541 29
	public function get_billing_postcode( $context = 'view' ) {
542 29
		return $this->get_address_prop( 'postcode', 'billing', $context );
543
	}
544
545
	/**
546
	 * Get billing_country.
547
	 *
548
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
549
	 * @return string
550
	 */
551 30
	public function get_billing_country( $context = 'view' ) {
552 30
		return $this->get_address_prop( 'country', 'billing', $context );
553
	}
554
555
	/**
556
	 * Get billing_email.
557
	 *
558
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
559
	 * @return string
560
	 */
561 8
	public function get_billing_email( $context = 'view' ) {
562 8
		return $this->get_address_prop( 'email', 'billing', $context );
563
	}
564
565
	/**
566
	 * Get billing_phone.
567
	 *
568
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
569
	 * @return string
570
	 */
571 2
	public function get_billing_phone( $context = 'view' ) {
572 2
		return $this->get_address_prop( 'phone', 'billing', $context );
573
	}
574
575
	/**
576
	 * Get shipping.
577
	 *
578
	 * @since  3.2.0
579
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
580
	 * @return array
581
	 */
582
	public function get_shipping( $context = 'view' ) {
583
		return $this->get_prop( 'shipping', $context );
584
	}
585
586
	/**
587
	 * Get shipping_first_name.
588
	 *
589
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
590
	 * @return string
591
	 */
592 2
	public function get_shipping_first_name( $context = 'view' ) {
593 2
		return $this->get_address_prop( 'first_name', 'shipping', $context );
594
	}
595
596
	/**
597
	 * Get shipping_last_name.
598
	 *
599
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
600
	 * @return string
601
	 */
602 2
	public function get_shipping_last_name( $context = 'view' ) {
603 2
		return $this->get_address_prop( 'last_name', 'shipping', $context );
604
	}
605
606
	/**
607
	 * Get shipping_company.
608
	 *
609
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
610
	 * @return string
611
	 */
612 2
	public function get_shipping_company( $context = 'view' ) {
613 2
		return $this->get_address_prop( 'company', 'shipping', $context );
614
	}
615
616
	/**
617
	 * Get shipping_address_1.
618
	 *
619
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
620
	 * @return string
621
	 */
622 20
	public function get_shipping_address( $context = 'view' ) {
623 20
		return $this->get_shipping_address_1( $context );
624
	}
625
626
	/**
627
	 * Get shipping_address_1.
628
	 *
629
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
630
	 * @return string
631
	 */
632 44
	public function get_shipping_address_1( $context = 'view' ) {
633 44
		return $this->get_address_prop( 'address_1', 'shipping', $context );
634
	}
635
636
	/**
637
	 * Get shipping_address_2.
638
	 *
639
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
640
	 * @return string
641
	 */
642 44
	public function get_shipping_address_2( $context = 'view' ) {
643 44
		return $this->get_address_prop( 'address_2', 'shipping', $context );
644
	}
645
646
	/**
647
	 * Get shipping_city.
648
	 *
649
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
650
	 * @return string
651
	 */
652 110
	public function get_shipping_city( $context = 'view' ) {
653 110
		return $this->get_address_prop( 'city', 'shipping', $context );
654
	}
655
656
	/**
657
	 * Get shipping_state.
658
	 *
659
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
660
	 * @return string
661
	 */
662 112
	public function get_shipping_state( $context = 'view' ) {
663 112
		return $this->get_address_prop( 'state', 'shipping', $context );
664
	}
665
666
	/**
667
	 * Get shipping_postcode.
668
	 *
669
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
670
	 * @return string
671
	 */
672 110
	public function get_shipping_postcode( $context = 'view' ) {
673 110
		return $this->get_address_prop( 'postcode', 'shipping', $context );
674
	}
675
676
	/**
677
	 * Get shipping_country.
678
	 *
679
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
680
	 * @return string
681
	 */
682 111
	public function get_shipping_country( $context = 'view' ) {
683 111
		return $this->get_address_prop( 'country', 'shipping', $context );
684
	}
685
686
	/**
687
	 * Is the user a paying customer?
688
	 *
689
	 * @since  3.0.0
690
	 * @param  string $context What the value is for. Valid values are 'view' and 'edit'.
691
	 * @return bool
692
	 */
693 12
	public function get_is_paying_customer( $context = 'view' ) {
694 12
		return $this->get_prop( 'is_paying_customer', $context );
695
	}
696
697
	/*
698
	|--------------------------------------------------------------------------
699
	| Setters
700
	|--------------------------------------------------------------------------
701
	*/
702
703
	/**
704
	 * Set customer's username.
705
	 *
706
	 * @since 3.0.0
707
	 * @param string $username Username.
708
	 */
709 42
	public function set_username( $username ) {
710 42
		$this->set_prop( 'username', $username );
711
	}
712
713
	/**
714
	 * Set customer's email.
715
	 *
716
	 * @since 3.0.0
717
	 * @param string $value Email.
718
	 */
719 42
	public function set_email( $value ) {
720 42
		if ( $value && ! is_email( $value ) ) {
721
			$this->error( 'customer_invalid_email', __( 'Invalid email address', 'woocommerce' ) );
722
		}
723 42
		$this->set_prop( 'email', sanitize_email( $value ) );
724
	}
725
726
	/**
727
	 * Set customer's first name.
728
	 *
729
	 * @since 3.0.0
730
	 * @param string $first_name First name.
731
	 */
732 41
	public function set_first_name( $first_name ) {
733 41
		$this->set_prop( 'first_name', $first_name );
734
	}
735
736
	/**
737
	 * Set customer's last name.
738
	 *
739
	 * @since 3.0.0
740
	 * @param string $last_name Last name.
741
	 */
742 25
	public function set_last_name( $last_name ) {
743 25
		$this->set_prop( 'last_name', $last_name );
744
	}
745
746
	/**
747
	 * Set customer's display name.
748
	 *
749
	 * @since 3.1.0
750
	 * @param string $display_name Display name.
751
	 */
752 25
	public function set_display_name( $display_name ) {
753
		/* translators: 1: first name 2: last name */
754 25
		$this->set_prop( 'display_name', is_email( $display_name ) ? sprintf( _x( '%1$s %2$s', 'display name', 'woocommerce' ), $this->get_first_name(), $this->get_last_name() ) : $display_name );
755
	}
756
757
	/**
758
	 * Set customer's user role(s).
759
	 *
760
	 * @since 3.0.0
761
	 * @param mixed $role User role.
762
	 */
763 25
	public function set_role( $role ) {
764
		global $wp_roles;
765
766 25
		if ( $role && ! empty( $wp_roles->roles ) && ! in_array( $role, array_keys( $wp_roles->roles ), true ) ) {
767
			$this->error( 'customer_invalid_role', __( 'Invalid role', 'woocommerce' ) );
768
		}
769 25
		$this->set_prop( 'role', $role );
770
	}
771
772
	/**
773
	 * Set the date this customer was last updated.
774
	 *
775
	 * @since  3.0.0
776
	 * @param  string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
777
	 */
778 42
	public function set_date_created( $date = null ) {
779 42
		$this->set_date_prop( 'date_created', $date );
780
	}
781
782
	/**
783
	 * Set the date this customer was last updated.
784
	 *
785
	 * @since  3.0.0
786
	 * @param  string|integer|null $date UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if their is no date.
787
	 */
788 42
	public function set_date_modified( $date = null ) {
789 42
		$this->set_date_prop( 'date_modified', $date );
790
	}
791
792
	/**
793
	 * Set customer address to match shop base address.
794
	 *
795
	 * @since 3.0.0
796
	 */
797 2
	public function set_billing_address_to_base() {
798 2
		$base = wc_get_customer_default_location();
799 2
		$this->set_billing_location( $base['country'], $base['state'], '', '' );
800
	}
801
802
	/**
803
	 * Set customer shipping address to base address.
804
	 *
805
	 * @since 3.0.0
806
	 */
807 1
	public function set_shipping_address_to_base() {
808 1
		$base = wc_get_customer_default_location();
809 1
		$this->set_shipping_location( $base['country'], $base['state'], '', '' );
810
	}
811
812
	/**
813
	 * Sets all address info at once.
814
	 *
815
	 * @param string $country  Country.
816
	 * @param string $state    State.
817
	 * @param string $postcode Postcode.
818
	 * @param string $city     City.
819
	 */
820 3 View Code Duplication
	public function set_billing_location( $country, $state = '', $postcode = '', $city = '' ) {
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...
821 3
		$address_data = $this->get_prop( 'billing', 'edit' );
822
823 3
		$address_data['address_1'] = '';
824 3
		$address_data['address_2'] = '';
825 3
		$address_data['city']      = $city;
826 3
		$address_data['state']     = $state;
827 3
		$address_data['postcode']  = $postcode;
828 3
		$address_data['country']   = $country;
829
830 3
		$this->set_prop( 'billing', $address_data );
831
	}
832
833
	/**
834
	 * Sets all shipping info at once.
835
	 *
836
	 * @param string $country  Country.
837
	 * @param string $state    State.
838
	 * @param string $postcode Postcode.
839
	 * @param string $city     City.
840
	 */
841 2 View Code Duplication
	public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
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...
842 2
		$address_data = $this->get_prop( 'shipping', 'edit' );
843
844 2
		$address_data['address_1'] = '';
845 2
		$address_data['address_2'] = '';
846 2
		$address_data['city']      = $city;
847 2
		$address_data['state']     = $state;
848 2
		$address_data['postcode']  = $postcode;
849 2
		$address_data['country']   = $country;
850
851 2
		$this->set_prop( 'shipping', $address_data );
852
	}
853
854
	/**
855
	 * Sets a prop for a setter method.
856
	 *
857
	 * @since 3.0.0
858
	 * @param string $prop    Name of prop to set.
859
	 * @param string $address Name of address to set. billing or shipping.
860
	 * @param mixed  $value   Value of the prop.
861
	 */
862 30 View Code Duplication
	protected function set_address_prop( $prop, $address = 'billing', $value ) {
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...
863 30
		if ( array_key_exists( $prop, $this->data[ $address ] ) ) {
864 30
			if ( true === $this->object_read ) {
865 30
				if ( $value !== $this->data[ $address ][ $prop ] || ( isset( $this->changes[ $address ] ) && array_key_exists( $prop, $this->changes[ $address ] ) ) ) {
866 30
					$this->changes[ $address ][ $prop ] = $value;
867
				}
868
			} else {
869 9
				$this->data[ $address ][ $prop ] = $value;
870
			}
871
		}
872
	}
873
874
	/**
875
	 * Set billing_first_name.
876
	 *
877
	 * @param string $value Billing first name.
878
	 */
879 1
	public function set_billing_first_name( $value ) {
880 1
		$this->set_address_prop( 'first_name', 'billing', $value );
881
	}
882
883
	/**
884
	 * Set billing_last_name.
885
	 *
886
	 * @param string $value Billing last name.
887
	 */
888 1
	public function set_billing_last_name( $value ) {
889 1
		$this->set_address_prop( 'last_name', 'billing', $value );
890
	}
891
892
	/**
893
	 * Set billing_company.
894
	 *
895
	 * @param string $value Billing company.
896
	 */
897 1
	public function set_billing_company( $value ) {
898 1
		$this->set_address_prop( 'company', 'billing', $value );
899
	}
900
901
	/**
902
	 * Set billing_address_1.
903
	 *
904
	 * @param string $value Billing address line 1.
905
	 */
906 29
	public function set_billing_address( $value ) {
907 29
		$this->set_billing_address_1( $value );
908
	}
909
910
	/**
911
	 * Set billing_address_1.
912
	 *
913
	 * @param string $value Billing address line 1.
914
	 */
915 29
	public function set_billing_address_1( $value ) {
916 29
		$this->set_address_prop( 'address_1', 'billing', $value );
917
	}
918
919
	/**
920
	 * Set billing_address_2.
921
	 *
922
	 * @param string $value Billing address line 2.
923
	 */
924 29
	public function set_billing_address_2( $value ) {
925 29
		$this->set_address_prop( 'address_2', 'billing', $value );
926
	}
927
928
	/**
929
	 * Set billing_city.
930
	 *
931
	 * @param string $value Billing city.
932
	 */
933 29
	public function set_billing_city( $value ) {
934 29
		$this->set_address_prop( 'city', 'billing', $value );
935
	}
936
937
	/**
938
	 * Set billing_state.
939
	 *
940
	 * @param string $value Billing state.
941
	 */
942 30
	public function set_billing_state( $value ) {
943 30
		$this->set_address_prop( 'state', 'billing', $value );
944
	}
945
946
	/**
947
	 * Set billing_postcode.
948
	 *
949
	 * @param string $value Billing postcode.
950
	 */
951 29
	public function set_billing_postcode( $value ) {
952 29
		$this->set_address_prop( 'postcode', 'billing', $value );
953
	}
954
955
	/**
956
	 * Set billing_country.
957
	 *
958
	 * @param string $value Billing country.
959
	 */
960 30
	public function set_billing_country( $value ) {
961 30
		$this->set_address_prop( 'country', 'billing', $value );
962
	}
963
964
	/**
965
	 * Set billing_email.
966
	 *
967
	 * @param string $value Billing email.
968
	 */
969 5
	public function set_billing_email( $value ) {
970 5
		if ( $value && ! is_email( $value ) ) {
971
			$this->error( 'customer_invalid_billing_email', __( 'Invalid billing email address', 'woocommerce' ) );
972
		}
973 5
		$this->set_address_prop( 'email', 'billing', sanitize_email( $value ) );
974
	}
975
976
	/**
977
	 * Set billing_phone.
978
	 *
979
	 * @param string $value Billing phone.
980
	 */
981 1
	public function set_billing_phone( $value ) {
982 1
		$this->set_address_prop( 'phone', 'billing', $value );
983
	}
984
985
	/**
986
	 * Set shipping_first_name.
987
	 *
988
	 * @param string $value Shipping first name.
989
	 */
990 1
	public function set_shipping_first_name( $value ) {
991 1
		$this->set_address_prop( 'first_name', 'shipping', $value );
992
	}
993
994
	/**
995
	 * Set shipping_last_name.
996
	 *
997
	 * @param string $value Shipping last name.
998
	 */
999 1
	public function set_shipping_last_name( $value ) {
1000 1
		$this->set_address_prop( 'last_name', 'shipping', $value );
1001
	}
1002
1003
	/**
1004
	 * Set shipping_company.
1005
	 *
1006
	 * @param string $value Shipping company.
1007
	 */
1008 1
	public function set_shipping_company( $value ) {
1009 1
		$this->set_address_prop( 'company', 'shipping', $value );
1010
	}
1011
1012
	/**
1013
	 * Set shipping_address_1.
1014
	 *
1015
	 * @param string $value Shipping address line 1.
1016
	 */
1017 29
	public function set_shipping_address( $value ) {
1018 29
		$this->set_shipping_address_1( $value );
1019
	}
1020
1021
	/**
1022
	 * Set shipping_address_1.
1023
	 *
1024
	 * @param string $value Shipping address line 1.
1025
	 */
1026 29
	public function set_shipping_address_1( $value ) {
1027 29
		$this->set_address_prop( 'address_1', 'shipping', $value );
1028
	}
1029
1030
	/**
1031
	 * Set shipping_address_2.
1032
	 *
1033
	 * @param string $value Shipping address line 2.
1034
	 */
1035 29
	public function set_shipping_address_2( $value ) {
1036 29
		$this->set_address_prop( 'address_2', 'shipping', $value );
1037
	}
1038
1039
	/**
1040
	 * Set shipping_city.
1041
	 *
1042
	 * @param string $value Shipping city.
1043
	 */
1044 29
	public function set_shipping_city( $value ) {
1045 29
		$this->set_address_prop( 'city', 'shipping', $value );
1046
	}
1047
1048
	/**
1049
	 * Set shipping_state.
1050
	 *
1051
	 * @param string $value Shipping state.
1052
	 */
1053 30
	public function set_shipping_state( $value ) {
1054 30
		$this->set_address_prop( 'state', 'shipping', $value );
1055
	}
1056
1057
	/**
1058
	 * Set shipping_postcode.
1059
	 *
1060
	 * @param string $value Shipping postcode.
1061
	 */
1062 29
	public function set_shipping_postcode( $value ) {
1063 29
		$this->set_address_prop( 'postcode', 'shipping', $value );
1064
	}
1065
1066
	/**
1067
	 * Set shipping_country.
1068
	 *
1069
	 * @param string $value Shipping country.
1070
	 */
1071 29
	public function set_shipping_country( $value ) {
1072 29
		$this->set_address_prop( 'country', 'shipping', $value );
1073
	}
1074
1075
	/**
1076
	 * Set if the user a paying customer.
1077
	 *
1078
	 * @since 3.0.0
1079
	 * @param bool $is_paying_customer If is a paying customer.
1080
	 */
1081 25
	public function set_is_paying_customer( $is_paying_customer ) {
1082 25
		$this->set_prop( 'is_paying_customer', (bool) $is_paying_customer );
1083
	}
1084
}
1085