1 <?php
  2 /**
  3  * Customer
  4  *
  5  * The WooCommerce customer class handles storage of the current customer's data, such as location.
  6  *
  7  * @class       WC_Customer
  8  * @version     1.6.4
  9  * @package     WooCommerce/Classes
 10  * @category    Class
 11  * @author      WooThemes
 12  */
 13 class WC_Customer {
 14 
 15     /** Stores customer data as an array */
 16     protected $_data;
 17 
 18     /** Stores bool when data is changed */
 19     private $_changed = false;
 20 
 21     /**
 22      * Constructor for the customer class loads the customer data.
 23      *
 24      * @access public
 25      * @return void
 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         // When leaving or ending page load, store data
 64         add_action( 'shutdown', array( $this, 'save_data' ), 10 );
 65     }
 66 
 67     /**
 68      * save_data function.
 69      *
 70      * @access public
 71      * @return void
 72      */
 73     public function save_data() {
 74         if ( $this->_changed ) {
 75             $GLOBALS['woocommerce']->session->customer = $this->_data;
 76         }
 77     }
 78 
 79     /**
 80      * __set function.
 81      * @access   public
 82      * @param mixed $property
 83      * @return bool
 84      */
 85     public function __isset( $property ) {
 86         return isset( $this->_data[ $property ] );
 87     }
 88 
 89     /**
 90      * __get function.
 91      *
 92      * @access public
 93      * @param mixed $property
 94      * @return mixed|null
 95      */
 96     public function __get( $property ) {
 97         return isset( $this->_data[ $property ] ) ? $this->_data[ $property ] : null;
 98     }
 99 
100     /**
101      * __set function.
102      *
103      * @access public
104      * @param mixed $property
105      * @param mixed $value
106      * @return void
107      */
108     public function __set( $property, $value ) {
109         $this->_data[ $property ] = $value;
110         $this->_changed = true;
111     }
112 
113     /**
114      * has_calculated_shipping function.
115      *
116      * @access public
117      * @return bool
118      */
119     public function has_calculated_shipping() {
120         return ( ! empty( $this->calculated_shipping ) ) ? true : false;
121     }
122 
123 
124     /**
125      * Set customer address to match shop base address.
126      *
127      * @access public
128      * @return void
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      * Set customer shipping address to base address.
147      *
148      * @access public
149      * @return void
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      * Is customer outside base country (for tax purposes)?
168      *
169      * @access public
170      * @return bool
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      * Is the user a paying customer?
195      *
196      * @access public
197      * @return bool
198      */
199     function is_paying_customer( $user_id ) {
200         return '1' === get_user_meta( $user_id, 'paying_customer', true );
201     }
202 
203 
204     /**
205      * Is customer VAT exempt?
206      *
207      * @access public
208      * @return bool
209      */
210     public function is_vat_exempt() {
211         return ( ! empty( $this->is_vat_exempt ) ) ? true : false;
212     }
213 
214 
215     /**
216      * Gets the state from the current session.
217      *
218      * @access public
219      * @return string
220      */
221     public function get_state() {
222         if ( isset( $this->state ) ) return $this->state;
223     }
224 
225 
226     /**
227      * Gets the country from the current session
228      *
229      * @access public
230      * @return string
231      */
232     public function get_country() {
233         if ( isset( $this->country ) ) return $this->country;
234     }
235 
236 
237     /**
238      * Gets the postcode from the current session.
239      *
240      * @access public
241      * @return string
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      * Get the city from the current session.
251      *
252      * @access public
253      * @return string
254      */
255     public function get_city() {
256         if ( isset( $this->city ) ) return $this->city;
257     }
258 
259     /**
260      * Gets the address from the current session.
261      *
262      * @access public
263      * @return string
264      */
265     public function get_address() {
266         if ( isset( $this->address ) ) return $this->address;
267     }
268 
269     /**
270      * Gets the address_2 from the current session.
271      *
272      * @access public
273      * @return string
274      */
275     public function get_address_2() {
276         if ( isset( $this->address_2 ) ) return $this->address_2;
277     }
278 
279     /**
280      * Gets the state from the current session.
281      *
282      * @access public
283      * @return string
284      */
285     public function get_shipping_state() {
286         if ( isset( $this->shipping_state ) ) return $this->shipping_state;
287     }
288 
289 
290     /**
291      * Gets the country from the current session.
292      *
293      * @access public
294      * @return string
295      */
296     public function get_shipping_country() {
297         if ( isset( $this->shipping_country ) ) return $this->shipping_country;
298     }
299 
300 
301     /**
302      * Gets the postcode from the current session.
303      *
304      * @access public
305      * @return string
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      * Gets the city from the current session.
315      *
316      * @access public
317      * @return string
318      */
319     public function get_shipping_city() {
320         if ( isset( $this->shipping_city ) ) return $this->shipping_city;
321     }
322 
323     /**
324      * Gets the address from the current session.
325      *
326      * @access public
327      * @return string
328      */
329     public function get_shipping_address() {
330         if ( isset( $this->shipping_address ) ) return $this->shipping_address;
331     }
332 
333     /**
334      * Gets the address_2 from the current session.
335      *
336      * @access public
337      * @return string
338      */
339     public function get_shipping_address_2() {
340         if ( isset( $this->shipping_address_2 ) ) return $this->shipping_address_2;
341     }
342 
343     /**
344      * get_taxable_address function.
345      *
346      * @access public
347      * @return array
348      */
349     public function get_taxable_address() {
350         $tax_based_on = get_option( 'woocommerce_tax_based_on' );
351 
352         // Check shipping method at this point to see if we need special handling
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      * Sets session data for the location.
392      *
393      * @access public
394      * @param mixed $country
395      * @param mixed $state
396      * @param string $postcode (default: '')
397      * @param string $city (default: '')
398      * @return void
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      * Sets session data for the country.
410      *
411      * @access public
412      * @param mixed $country
413      * @return void
414      */
415     public function set_country( $country ) {
416         $this->country = $country;
417     }
418 
419 
420     /**
421      * Sets session data for the state.
422      *
423      * @access public
424      * @param mixed $state
425      * @return void
426      */
427     public function set_state( $state ) {
428         $this->state = $state;
429     }
430 
431 
432     /**
433      * Sets session data for the postcode.
434      *
435      * @access public
436      * @param mixed $postcode
437      * @return void
438      */
439     public function set_postcode( $postcode ) {
440         $this->postcode = $postcode;
441     }
442 
443 
444     /**
445      * Sets session data for the city.
446      *
447      * @access public
448      * @param mixed $postcode
449      * @return void
450      */
451     public function set_city( $city ) {
452         $this->city = $city;
453     }
454 
455     /**
456      * Sets session data for the address.
457      *
458      * @access public
459      * @param mixed $address
460      * @return void
461      */
462     public function set_address( $address ) {
463         $this->address = $address;
464     }
465 
466     /**
467      * Sets session data for the address_2.
468      *
469      * @access public
470      * @param mixed $address_2
471      * @return void
472      */
473     public function set_address_2( $address_2 ) {
474         $this->address_2 = $address_2;
475     }
476 
477     /**
478      * Sets session data for the location.
479      *
480      * @access public
481      * @param mixed $country
482      * @param string $state (default: '')
483      * @param string $postcode (default: '')
484      * @param string $city (default: '')
485      * @return void
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      * Sets session data for the country.
497      *
498      * @access public
499      * @param mixed $country
500      * @return void
501      */
502     public function set_shipping_country( $country ) {
503         $this->shipping_country = $country;
504     }
505 
506 
507     /**
508      * Sets session data for the state.
509      *
510      * @access public
511      * @param mixed $state
512      * @return void
513      */
514     public function set_shipping_state( $state ) {
515         $this->shipping_state = $state;
516     }
517 
518 
519     /**
520      * Sets session data for the postcode.
521      *
522      * @access public
523      * @param mixed $postcode
524      * @return void
525      */
526     public function set_shipping_postcode( $postcode ) {
527         $this->shipping_postcode = $postcode;
528     }
529 
530 
531     /**
532      * Sets session data for the city.
533      *
534      * @access public
535      * @param mixed $postcode
536      * @return void
537      */
538     public function set_shipping_city( $city ) {
539         $this->shipping_city = $city;
540     }
541 
542     /**
543      * Sets session data for the address.
544      *
545      * @access public
546      * @param mixed $address
547      * @return void
548      */
549     public function set_shipping_address( $address ) {
550         $this->shipping_address = $address;
551     }
552 
553     /**
554      * Sets session data for the address_2.
555      *
556      * @access public
557      * @param mixed $address_2
558      * @return void
559      */
560     public function set_shipping_address_2( $address_2 ) {
561         $this->shipping_address_2 = $address_2;
562     }
563 
564 
565     /**
566      * Sets session data for the tax exemption.
567      *
568      * @access public
569      * @param mixed $is_vat_exempt
570      * @return void
571      */
572     public function set_is_vat_exempt( $is_vat_exempt ) {
573         $this->is_vat_exempt = $is_vat_exempt;
574     }
575 
576 
577     /**
578      * calculated_shipping function.
579      *
580      * @access public
581      * @param mixed $calculated
582      * @return void
583      */
584     public function calculated_shipping( $calculated = true ) {
585         $this->calculated_shipping = $calculated;
586     }
587 
588 
589     /**
590      * Gets a user's downloadable products if they are logged in.
591      *
592      * @access public
593      * @return array Array of downloadable products
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             // Get results from valid orders only
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                         // new order
633                         $order    = new WC_Order( $result->order_id );
634                         $_product = null;
635                     }
636 
637                     // Downloads permitted?
638                     if ( ! $order->is_download_permitted() ) {
639                         continue;
640                     }
641 
642                     if ( ! $_product || $_product->id != $result->product_id ) {
643                         // new product
644                         $file_number = 0;
645                         $_product    = get_product( $result->product_id );
646                     }
647 
648                     // Check product exists and has the file
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                     // Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files
655                     $download_name = apply_filters(
656                         'woocommerce_downloadable_product_name',
657                         $_product->get_title() . ' &ndash; ' . $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 }
WooCommerce API documentation generated by ApiGen 2.8.0