1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 class WC_Customer {
14
15
16 protected $_data;
17
18
19 private $_changed = false;
20
21 22 23 24 25 26
27 public function __construct() {
28
29 if ( empty( WC()->session->customer ) ) {
30
31 $default = apply_filters( 'woocommerce_customer_default_location', get_option( 'woocommerce_default_country' ) );
32
33 if ( strstr( $default, ':' ) ) {
34 list( $country, $state ) = explode( ':', $default );
35 } else {
36 $country = $default;
37 $state = '';
38 }
39
40 $this->_data = array(
41 'country' => esc_html( $country ),
42 'state' => '',
43 'postcode' => '',
44 'city' => '',
45 'address' => '',
46 'address_2' => '',
47 'shipping_country' => esc_html( $country ),
48 'shipping_state' => '',
49 'shipping_postcode' => '',
50 'shipping_city' => '',
51 'shipping_address' => '',
52 'shipping_address_2' => '',
53 'is_vat_exempt' => false,
54 'calculated_shipping' => false
55 );
56
57 } else {
58
59 $this->_data = WC()->session->customer;
60
61 }
62
63
64 add_action( 'shutdown', array( $this, 'save_data' ), 10 );
65 }
66
67 68 69 70 71 72
73 public function save_data() {
74 if ( $this->_changed ) {
75 $GLOBALS['woocommerce']->session->customer = $this->_data;
76 }
77 }
78
79 80 81 82 83 84
85 public function __isset( $property ) {
86 return isset( $this->_data[ $property ] );
87 }
88
89 90 91 92 93 94 95
96 public function __get( $property ) {
97 return isset( $this->_data[ $property ] ) ? $this->_data[ $property ] : null;
98 }
99
100 101 102 103 104 105 106 107
108 public function __set( $property, $value ) {
109 $this->_data[ $property ] = $value;
110 $this->_changed = true;
111 }
112
113 114 115 116 117 118
119 public function has_calculated_shipping() {
120 return ( ! empty( $this->calculated_shipping ) ) ? true : false;
121 }
122
123
124 125 126 127 128 129
130 public function set_to_base() {
131 $default = apply_filters( 'woocommerce_customer_default_location', get_option('woocommerce_default_country') );
132 if ( strstr( $default, ':' ) ) {
133 list( $country, $state ) = explode( ':', $default );
134 } else {
135 $country = $default;
136 $state = '';
137 }
138 $this->country = $country;
139 $this->state = $state;
140 $this->postcode = '';
141 $this->city = '';
142 }
143
144
145 146 147 148 149 150
151 public function set_shipping_to_base() {
152 $default = get_option('woocommerce_default_country');
153 if ( strstr( $default, ':' ) ) {
154 list( $country, $state ) = explode( ':', $default );
155 } else {
156 $country = $default;
157 $state = '';
158 }
159 $this->shipping_country = $country;
160 $this->shipping_state = $state;
161 $this->shipping_postcode = '';
162 $this->shipping_city = '';
163 }
164
165
166 167 168 169 170 171
172 public function is_customer_outside_base() {
173 list( $country, $state, $postcode, $city ) = $this->get_taxable_address();
174
175 if ( $country ) {
176
177 $default = get_option('woocommerce_default_country');
178
179 if ( strstr( $default, ':' ) ) {
180 list( $default_country, $default_state ) = explode( ':', $default );
181 } else {
182 $default_country = $default;
183 $default_state = '';
184 }
185
186 if ( $default_country !== $country ) return true;
187 if ( $default_state && $default_state !== $state ) return true;
188
189 }
190 return false;
191 }
192
193 194 195 196 197 198
199 function is_paying_customer( $user_id ) {
200 return '1' === get_user_meta( $user_id, 'paying_customer', true );
201 }
202
203
204 205 206 207 208 209
210 public function is_vat_exempt() {
211 return ( ! empty( $this->is_vat_exempt ) ) ? true : false;
212 }
213
214
215 216 217 218 219 220
221 public function get_state() {
222 if ( isset( $this->state ) ) return $this->state;
223 }
224
225
226 227 228 229 230 231
232 public function get_country() {
233 if ( isset( $this->country ) ) return $this->country;
234 }
235
236
237 238 239 240 241 242
243 public function get_postcode() {
244 if ( isset( $this->postcode ) && $this->postcode !== false )
245 return wc_format_postcode( $this->postcode, $this->get_country() );
246 }
247
248
249 250 251 252 253 254
255 public function get_city() {
256 if ( isset( $this->city ) ) return $this->city;
257 }
258
259 260 261 262 263 264
265 public function get_address() {
266 if ( isset( $this->address ) ) return $this->address;
267 }
268
269 270 271 272 273 274
275 public function get_address_2() {
276 if ( isset( $this->address_2 ) ) return $this->address_2;
277 }
278
279 280 281 282 283 284
285 public function get_shipping_state() {
286 if ( isset( $this->shipping_state ) ) return $this->shipping_state;
287 }
288
289
290 291 292 293 294 295
296 public function get_shipping_country() {
297 if ( isset( $this->shipping_country ) ) return $this->shipping_country;
298 }
299
300
301 302 303 304 305 306
307 public function get_shipping_postcode() {
308 if ( isset( $this->shipping_postcode ) )
309 return wc_format_postcode( $this->shipping_postcode, $this->get_shipping_country() );
310 }
311
312
313 314 315 316 317 318
319 public function get_shipping_city() {
320 if ( isset( $this->shipping_city ) ) return $this->shipping_city;
321 }
322
323 324 325 326 327 328
329 public function get_shipping_address() {
330 if ( isset( $this->shipping_address ) ) return $this->shipping_address;
331 }
332
333 334 335 336 337 338
339 public function get_shipping_address_2() {
340 if ( isset( $this->shipping_address_2 ) ) return $this->shipping_address_2;
341 }
342
343 344 345 346 347 348
349 public function get_taxable_address() {
350 $tax_based_on = get_option( 'woocommerce_tax_based_on' );
351
352
353 if ( apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) == true && sizeof( array_intersect( WC()->session->get( 'chosen_shipping_methods', array( get_option( 'woocommerce_default_shipping_method' ) ) ), apply_filters( 'woocommerce_local_pickup_methods', array( 'local_pickup' ) ) ) ) > 0 ) {
354 $tax_based_on = 'base';
355 }
356
357 if ( $tax_based_on == 'base' ) {
358
359 $default = get_option( 'woocommerce_default_country' );
360 if ( strstr( $default, ':' ) ) {
361 list( $country, $state ) = explode( ':', $default );
362 } else {
363 $country = $default;
364 $state = '';
365 }
366
367 $postcode = '';
368 $city = '';
369
370 } elseif ( $tax_based_on == 'billing' ) {
371
372 $country = $this->get_country();
373 $state = $this->get_state();
374 $postcode = $this->get_postcode();
375 $city = $this->get_city();
376
377 } else {
378
379 $country = $this->get_shipping_country();
380 $state = $this->get_shipping_state();
381 $postcode = $this->get_shipping_postcode();
382 $city = $this->get_shipping_city();
383
384 }
385
386 return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
387 }
388
389
390 391 392 393 394 395 396 397 398 399
400 public function set_location( $country, $state, $postcode = '', $city = '' ) {
401 $this->country = $country;
402 $this->state = $state;
403 $this->postcode = $postcode;
404 $this->city = $city;
405 }
406
407
408 409 410 411 412 413 414
415 public function set_country( $country ) {
416 $this->country = $country;
417 }
418
419
420 421 422 423 424 425 426
427 public function set_state( $state ) {
428 $this->state = $state;
429 }
430
431
432 433 434 435 436 437 438
439 public function set_postcode( $postcode ) {
440 $this->postcode = $postcode;
441 }
442
443
444 445 446 447 448 449 450
451 public function set_city( $city ) {
452 $this->city = $city;
453 }
454
455 456 457 458 459 460 461
462 public function set_address( $address ) {
463 $this->address = $address;
464 }
465
466 467 468 469 470 471 472
473 public function set_address_2( $address_2 ) {
474 $this->address_2 = $address_2;
475 }
476
477 478 479 480 481 482 483 484 485 486
487 public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
488 $this->shipping_country = $country;
489 $this->shipping_state = $state;
490 $this->shipping_postcode = $postcode;
491 $this->shipping_city = $city;
492 }
493
494
495 496 497 498 499 500 501
502 public function set_shipping_country( $country ) {
503 $this->shipping_country = $country;
504 }
505
506
507 508 509 510 511 512 513
514 public function set_shipping_state( $state ) {
515 $this->shipping_state = $state;
516 }
517
518
519 520 521 522 523 524 525
526 public function set_shipping_postcode( $postcode ) {
527 $this->shipping_postcode = $postcode;
528 }
529
530
531 532 533 534 535 536 537
538 public function set_shipping_city( $city ) {
539 $this->shipping_city = $city;
540 }
541
542 543 544 545 546 547 548
549 public function set_shipping_address( $address ) {
550 $this->shipping_address = $address;
551 }
552
553 554 555 556 557 558 559
560 public function set_shipping_address_2( $address_2 ) {
561 $this->shipping_address_2 = $address_2;
562 }
563
564
565 566 567 568 569 570 571
572 public function set_is_vat_exempt( $is_vat_exempt ) {
573 $this->is_vat_exempt = $is_vat_exempt;
574 }
575
576
577 578 579 580 581 582 583
584 public function calculated_shipping( $calculated = true ) {
585 $this->calculated_shipping = $calculated;
586 }
587
588
589 590 591 592 593 594
595 public function get_downloadable_products() {
596 global $wpdb;
597
598 $downloads = array();
599 $_product = null;
600 $order = null;
601 $file_number = 0;
602
603 if ( is_user_logged_in() ) {
604
605
606 $results = $wpdb->get_results( $wpdb->prepare( "
607 SELECT permissions.*
608 FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions
609 LEFT JOIN {$wpdb->posts} as posts ON permissions.order_id = posts.ID
610 WHERE user_id = %s
611 AND permissions.order_id > 0
612 AND posts.post_status = 'publish'
613 AND
614 (
615 permissions.downloads_remaining > 0
616 OR
617 permissions.downloads_remaining = ''
618 )
619 AND
620 (
621 permissions.access_expires IS NULL
622 OR
623 permissions.access_expires >= %s
624 )
625 GROUP BY permissions.download_id
626 ORDER BY permissions.order_id, permissions.product_id, permissions.permission_id;
627 ", get_current_user_id(), date( 'Y-m-d', current_time( 'timestamp' ) ) ) );
628
629 if ( $results ) {
630 foreach ( $results as $result ) {
631 if ( ! $order || $order->id != $result->order_id ) {
632
633 $order = new WC_Order( $result->order_id );
634 $_product = null;
635 }
636
637
638 if ( ! $order->is_download_permitted() ) {
639 continue;
640 }
641
642 if ( ! $_product || $_product->id != $result->product_id ) {
643
644 $file_number = 0;
645 $_product = get_product( $result->product_id );
646 }
647
648
649 if ( ! $_product || ! $_product->exists() || ! $_product->has_file( $result->download_id ) ) {
650 continue;
651 }
652
653 $download_file = $_product->get_file( $result->download_id );
654
655 $download_name = apply_filters(
656 'woocommerce_downloadable_product_name',
657 $_product->get_title() . ' – ' . $download_file['name'],
658 $_product,
659 $result->download_id,
660 $file_number
661 );
662
663 $downloads[] = array(
664 'download_url' => add_query_arg( array( 'download_file' => $result->product_id, 'order' => $result->order_key, 'email' => $result->user_email, 'key' => $result->download_id ), home_url( '/', 'http' ) ),
665 'download_id' => $result->download_id,
666 'product_id' => $result->product_id,
667 'download_name' => $download_name,
668 'order_id' => $order->id,
669 'order_key' => $order->order_key,
670 'downloads_remaining' => $result->downloads_remaining
671 );
672
673 $file_number++;
674 }
675 }
676 }
677
678 return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
679 }
680 }