2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
2016-03-08 21:44:28 +00:00
|
|
|
include_once( 'legacy/class-wc-legacy-customer.php' );
|
2015-11-06 09:22:19 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2016-03-08 21:44:28 +00:00
|
|
|
exit;
|
2015-11-06 09:22:19 +00:00
|
|
|
}
|
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2011-12-08 19:45:24 +00:00
|
|
|
* The WooCommerce customer class handles storage of the current customer's data, such as location.
|
2011-08-09 15:16:18 +00:00
|
|
|
*
|
2014-11-18 14:32:48 +00:00
|
|
|
* @class WC_Customer
|
2016-03-08 21:44:28 +00:00
|
|
|
* @version 2.7.0
|
2014-11-18 14:32:48 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2016-03-17 17:30:20 +00:00
|
|
|
class WC_Customer extends WC_Legacy_Customer {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-11-18 14:32:48 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Stores customer data.
|
2014-11-18 14:32:48 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2016-03-08 21:44:28 +00:00
|
|
|
protected $_data = array(
|
2016-08-30 16:50:35 +00:00
|
|
|
'id' => 0,
|
|
|
|
'date_created' => '',
|
|
|
|
'date_modified' => '',
|
|
|
|
'email' => '',
|
|
|
|
'first_name' => '',
|
|
|
|
'last_name' => '',
|
|
|
|
'role' => 'customer',
|
|
|
|
'username' => '',
|
|
|
|
'billing' => array(
|
|
|
|
'first_name' => '',
|
|
|
|
'last_name' => '',
|
|
|
|
'company' => '',
|
|
|
|
'address_1' => '',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => '',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => '',
|
|
|
|
'country' => '',
|
|
|
|
'email' => '',
|
|
|
|
'phone' => '',
|
|
|
|
),
|
|
|
|
'shipping' => array(
|
|
|
|
'first_name' => '',
|
|
|
|
'last_name' => '',
|
|
|
|
'company' => '',
|
|
|
|
'address_1' => '',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => '',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => '',
|
|
|
|
'country' => '',
|
|
|
|
),
|
|
|
|
'is_paying_customer' => false,
|
2016-03-08 21:44:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keys which are also stored in a session (so we can make sure they get updated...)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_session_keys = array(
|
2016-08-27 03:04:10 +00:00
|
|
|
'billing_postcode',
|
|
|
|
'billing_city',
|
|
|
|
'billing_address_1',
|
|
|
|
'billing_address',
|
|
|
|
'billing_address_2',
|
|
|
|
'billing_state',
|
|
|
|
'billing_country',
|
|
|
|
'shipping_postcode',
|
|
|
|
'shipping_city',
|
|
|
|
'shipping_address_1',
|
|
|
|
'shipping_address',
|
|
|
|
'shipping_address_2',
|
|
|
|
'shipping_state',
|
|
|
|
'shipping_country',
|
|
|
|
'is_vat_exempt',
|
|
|
|
'calculated_shipping',
|
|
|
|
'billing_first_name',
|
|
|
|
'billing_last_name',
|
|
|
|
'billing_company',
|
|
|
|
'billing_phone',
|
|
|
|
'billing_email',
|
|
|
|
'shipping_first_name',
|
|
|
|
'shipping_last_name',
|
|
|
|
'shipping_company',
|
2016-03-08 21:44:28 +00:00
|
|
|
);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-03-17 19:03:23 +00:00
|
|
|
/**
|
2016-08-04 20:13:07 +00:00
|
|
|
* Data stored in meta keys, but not considered "meta"
|
2016-03-17 19:03:23 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_internal_meta_keys = array(
|
2016-08-27 03:04:10 +00:00
|
|
|
'billing_postcode',
|
|
|
|
'billing_city',
|
|
|
|
'billing_address_1',
|
|
|
|
'billing_address_2',
|
|
|
|
'billing_state',
|
|
|
|
'billing_country',
|
|
|
|
'shipping_postcode',
|
|
|
|
'shipping_city',
|
|
|
|
'shipping_address_1',
|
|
|
|
'shipping_address_2',
|
|
|
|
'shipping_state',
|
|
|
|
'shipping_country',
|
|
|
|
'paying_customer',
|
|
|
|
'last_update',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'show_admin_bar_front',
|
|
|
|
'use_ssl',
|
|
|
|
'admin_color',
|
|
|
|
'rich_editing',
|
|
|
|
'comment_shortcuts',
|
|
|
|
'dismissed_wp_pointers',
|
|
|
|
'show_welcome_panel',
|
|
|
|
'_woocommerce_persistent_cart',
|
|
|
|
'session_tokens',
|
2016-09-01 10:46:05 +00:00
|
|
|
'nickname',
|
|
|
|
'description',
|
2016-08-27 03:04:10 +00:00
|
|
|
'billing_first_name',
|
|
|
|
'billing_last_name',
|
|
|
|
'billing_company',
|
|
|
|
'billing_phone',
|
|
|
|
'billing_email',
|
|
|
|
'shipping_first_name',
|
|
|
|
'shipping_last_name',
|
|
|
|
'shipping_company',
|
2016-09-01 10:46:05 +00:00
|
|
|
'default_password_nag',
|
|
|
|
'primary_blog',
|
|
|
|
'source_domain',
|
2016-03-17 19:03:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2016-08-09 20:51:38 +00:00
|
|
|
* Internal meta type used to store user data.
|
2016-03-17 19:03:23 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_meta_type = 'user';
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-08-15 12:17:43 +00:00
|
|
|
* If this is the customer session, this is true. When true, guest accounts will not be saved to the DB.
|
2016-03-08 21:44:28 +00:00
|
|
|
* @var boolean
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-08-15 12:17:43 +00:00
|
|
|
protected $_is_session = false;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-08-30 16:50:35 +00:00
|
|
|
/**
|
|
|
|
* Stores a password if this needs to be changed. Write-only and hidden from _data.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_password = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores if user is VAT exempt for this session.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_is_vat_exempt = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores if user has calculated shipping in this session.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_calculated_shipping = false;
|
|
|
|
|
2016-03-08 21:44:28 +00:00
|
|
|
/**
|
|
|
|
* Load customer data based on how WC_Customer is called.
|
2016-08-15 12:17:43 +00:00
|
|
|
*
|
|
|
|
* If $customer is 'new', you can build a new WC_Customer object. If it's empty, some
|
2016-03-09 20:49:02 +00:00
|
|
|
* data will be pulled from the session for the current user/customer.
|
2016-08-15 12:17:43 +00:00
|
|
|
*
|
|
|
|
* @param int $customer_id Customer ID
|
|
|
|
* @param bool $is_session True if this is the customer session
|
2016-03-08 21:44:28 +00:00
|
|
|
*/
|
2016-08-15 12:17:43 +00:00
|
|
|
public function __construct( $customer_id = 0, $is_session = false ) {
|
|
|
|
if ( $customer_id > 0 ) {
|
|
|
|
$this->read( $customer_id );
|
2012-08-14 19:42:38 +00:00
|
|
|
}
|
2016-08-15 12:17:43 +00:00
|
|
|
if ( $is_session ) {
|
|
|
|
$this->_is_session = true;
|
|
|
|
$this->load_session();
|
2016-08-31 12:25:13 +00:00
|
|
|
add_action( 'shutdown', array( $this, 'save_to_session' ), 10 );
|
2014-05-08 09:35:51 +00:00
|
|
|
}
|
2013-01-12 13:02:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:14:50 +00:00
|
|
|
/**
|
|
|
|
* Loads a WC session into the customer class.
|
|
|
|
*/
|
|
|
|
public function load_session() {
|
2016-08-15 12:17:43 +00:00
|
|
|
$data = (array) WC()->session->get( 'customer' );
|
|
|
|
if ( ! empty( $data ) ) {
|
|
|
|
foreach ( $this->_session_keys as $session_key ) {
|
|
|
|
$function_key = $session_key;
|
|
|
|
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
|
|
|
|
$session_key = str_replace( 'billing_', '', $session_key );
|
|
|
|
}
|
|
|
|
if ( ! empty( $data[ $session_key ] ) && is_callable( array( $this, "set_{$function_key}" ) ) ) {
|
|
|
|
$this->{"set_{$function_key}"}( $data[ $session_key ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->load_defaults();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load default values if props are unset.
|
|
|
|
*/
|
|
|
|
protected function load_defaults() {
|
|
|
|
$default = wc_get_customer_default_location();
|
|
|
|
|
|
|
|
// Set some defaults if some of our values are still not set.
|
2016-08-15 17:26:00 +00:00
|
|
|
if ( ! $this->get_billing_country() ) {
|
2016-08-15 12:17:43 +00:00
|
|
|
$this->set_billing_country( $default['country'] );
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:26:00 +00:00
|
|
|
if ( ! $this->get_shipping_country() ) {
|
2016-08-15 12:17:43 +00:00
|
|
|
$this->set_shipping_country( $this->get_billing_country() );
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:26:00 +00:00
|
|
|
if ( ! $this->get_billing_state() ) {
|
2016-08-15 12:17:43 +00:00
|
|
|
$this->set_billing_state( $default['state'] );
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:26:00 +00:00
|
|
|
if ( ! $this->get_shipping_state() ) {
|
2016-08-15 12:17:43 +00:00
|
|
|
$this->set_shipping_state( $this->get_billing_state() );
|
2016-03-11 18:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 09:04:52 +00:00
|
|
|
/**
|
|
|
|
* Gets the customers last order.
|
|
|
|
* @return WC_Order|false
|
|
|
|
*/
|
|
|
|
public function get_last_order() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$last_order = $wpdb->get_var( "SELECT posts.ID
|
|
|
|
FROM $wpdb->posts AS posts
|
|
|
|
LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
|
|
|
|
WHERE meta.meta_key = '_customer_user'
|
|
|
|
AND meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
|
|
|
|
AND posts.post_type = 'shop_order'
|
|
|
|
AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
|
|
|
|
ORDER BY posts.ID DESC
|
|
|
|
" );
|
|
|
|
|
|
|
|
if ( $last_order ) {
|
|
|
|
return wc_get_order( absint( $last_order ) );
|
2016-03-14 19:14:50 +00:00
|
|
|
} else {
|
2016-08-16 09:04:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of orders this customer has.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function get_order_count() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$count = $wpdb->get_var( "SELECT COUNT(*)
|
|
|
|
FROM $wpdb->posts as posts
|
|
|
|
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
|
|
|
|
WHERE meta.meta_key = '_customer_user'
|
|
|
|
AND posts.post_type = 'shop_order'
|
|
|
|
AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
|
|
|
|
AND meta_value = '" . esc_sql( $this->get_id() ) . "'
|
|
|
|
" );
|
|
|
|
|
|
|
|
return absint( $count );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return how much money this customer has spent.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_total_spent() {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
|
|
|
|
FROM $wpdb->posts as posts
|
|
|
|
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
|
|
|
|
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
|
|
|
|
WHERE meta.meta_key = '_customer_user'
|
|
|
|
AND meta.meta_value = '" . esc_sql( $this->get_id() ) . "'
|
|
|
|
AND posts.post_type = 'shop_order'
|
|
|
|
AND posts.post_status IN ( 'wc-completed', 'wc-processing' )
|
|
|
|
AND meta2.meta_key = '_order_total'
|
|
|
|
" );
|
|
|
|
|
|
|
|
if ( ! $spent ) {
|
|
|
|
$spent = 0;
|
|
|
|
}
|
|
|
|
|
2016-08-31 14:43:34 +00:00
|
|
|
return wc_format_decimal( $spent, 2 );
|
2016-08-16 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is customer outside base country (for tax purposes)?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_customer_outside_base() {
|
|
|
|
list( $country, $state ) = $this->get_taxable_address();
|
|
|
|
if ( $country ) {
|
|
|
|
$default = wc_get_base_location();
|
|
|
|
if ( $default['country'] !== $country ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ( $default['state'] && $default['state'] !== $state ) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-11 18:28:34 +00:00
|
|
|
}
|
2016-08-16 09:04:52 +00:00
|
|
|
return false;
|
2016-03-11 18:28:34 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 16:50:35 +00:00
|
|
|
/**
|
|
|
|
* Is customer VAT exempt?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_vat_exempt() {
|
|
|
|
return $this->get_is_vat_exempt();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Has calculated shipping?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has_calculated_shipping() {
|
|
|
|
return $this->get_calculated_shipping();
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Getters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Methods for getting data from the customer object.
|
2013-11-27 09:03:47 +00:00
|
|
|
*/
|
2014-09-22 12:23:28 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 12:17:43 +00:00
|
|
|
* Return a customer's user ID. Logged out users have ID 0.
|
2016-03-08 21:44:28 +00:00
|
|
|
* @since 2.7.0
|
2016-08-15 12:17:43 +00:00
|
|
|
* @return int
|
2014-09-22 12:23:28 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
public function get_id() {
|
|
|
|
return $this->_data['id'];
|
2014-09-22 12:23:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-18 13:32:44 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Return the customer's username.
|
|
|
|
* @since 2.7.0
|
2014-11-18 13:32:44 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
public function get_username() {
|
|
|
|
return $this->_data['username'];
|
2014-09-22 12:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Return the customer's email.
|
|
|
|
* @since 2.7.0
|
2014-09-22 12:23:28 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
public function get_email() {
|
2016-08-09 10:16:54 +00:00
|
|
|
return $this->_data['email'];
|
2014-04-04 07:38:56 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-03-10 16:22:47 +00:00
|
|
|
/**
|
|
|
|
* Return customer's first name.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function get_first_name() {
|
|
|
|
return $this->_data['first_name'];
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return customer's last name.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function get_last_name() {
|
|
|
|
return $this->_data['last_name'];
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return customer's user role.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function get_role() {
|
|
|
|
return $this->_data['role'];
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-11 18:28:34 +00:00
|
|
|
* Return this customer's avatar.
|
2016-03-10 16:22:47 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function get_avatar_url() {
|
|
|
|
$avatar_html = get_avatar( $this->get_email() );
|
|
|
|
|
|
|
|
// Get the URL of the avatar from the provided HTML
|
|
|
|
preg_match( '/src=["|\'](.+)[\&|"|\']/U', $avatar_html, $matches );
|
|
|
|
|
|
|
|
if ( isset( $matches[1] ) && ! empty( $matches[1] ) ) {
|
|
|
|
return esc_url( $matches[1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the date this customer was created.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function get_date_created() {
|
|
|
|
return absint( $this->_data['date_created'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the date this customer was last updated.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function get_date_modified() {
|
|
|
|
return absint( $this->_data['date_modified'] );
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:51:38 +00:00
|
|
|
/**
|
|
|
|
* Gets customer billing first name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_billing_first_name() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['first_name'];
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets customer billing last name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_billing_last_name() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['last_name'];
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets customer billing company.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_billing_company() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['company'];
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 13:13:20 +00:00
|
|
|
/**
|
|
|
|
* Gets billing phone.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_billing_phone() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['phone'];
|
2016-08-15 13:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets billing email.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_billing_email() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['email'];
|
2016-08-15 13:13:20 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Gets customer postcode.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_postcode() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return wc_format_postcode( $this->_data['billing']['postcode'], $this->get_billing_country() );
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer city.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_city() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['city'];
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer address.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_address() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['address_1'];
|
2012-09-23 16:16:39 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
/**
|
|
|
|
* Get customer address.
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_address_1() {
|
|
|
|
return $this->get_billing_address();
|
2016-03-15 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2012-09-23 16:16:39 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's second address.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_address_2() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['address_2'];
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer state.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_state() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['state'];
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer country.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function get_billing_country() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['billing']['country'];
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-08-09 20:51:38 +00:00
|
|
|
/**
|
|
|
|
* Gets customer shipping first name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_shipping_first_name() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['first_name'];
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets customer shipping last name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_shipping_last_name() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['last_name'];
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets customer shipping company.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_shipping_company() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['company'];
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's shipping state.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_shipping_state() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['state'];
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's shipping country.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_shipping_country() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['country'];
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's shipping postcode.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_shipping_postcode() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return wc_format_postcode( $this->_data['shipping']['postcode'], $this->get_shipping_country() );
|
2012-09-23 16:16:39 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-23 16:16:39 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's shipping city.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_shipping_city() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['city'];
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's shipping address.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_shipping_address() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['address_1'];
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-03-15 18:37:42 +00:00
|
|
|
/**
|
|
|
|
* Get customer address.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_shipping_address_1() {
|
|
|
|
return $this->get_shipping_address();
|
|
|
|
}
|
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Get customer's second shipping address.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_shipping_address_2() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_data['shipping']['address_2'];
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get if customer is VAT exempt?
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function get_is_vat_exempt() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_is_vat_exempt;
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Has customer calculated shipping?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function get_calculated_shipping() {
|
2016-08-30 16:50:35 +00:00
|
|
|
return $this->_calculated_shipping;
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-10-01 09:45:07 +00:00
|
|
|
/**
|
2016-01-06 14:56:25 +00:00
|
|
|
* Get taxable address.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return array
|
2012-10-01 09:45:07 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function get_taxable_address() {
|
2012-12-03 15:13:59 +00:00
|
|
|
$tax_based_on = get_option( 'woocommerce_tax_based_on' );
|
|
|
|
|
2013-09-03 15:26:02 +00:00
|
|
|
// Check shipping method at this point to see if we need special handling
|
2016-06-29 14:23:39 +00:00
|
|
|
if ( true === apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && sizeof( array_intersect( wc_get_chosen_shipping_method_ids(), apply_filters( 'woocommerce_local_pickup_methods', array( 'legacy_local_pickup', 'local_pickup' ) ) ) ) > 0 ) {
|
2013-09-03 15:26:02 +00:00
|
|
|
$tax_based_on = 'base';
|
|
|
|
}
|
|
|
|
|
2015-06-22 14:30:34 +00:00
|
|
|
if ( 'base' === $tax_based_on ) {
|
2015-06-23 10:50:15 +00:00
|
|
|
$country = WC()->countries->get_base_country();
|
|
|
|
$state = WC()->countries->get_base_state();
|
|
|
|
$postcode = WC()->countries->get_base_postcode();
|
|
|
|
$city = WC()->countries->get_base_city();
|
2015-06-17 14:19:04 +00:00
|
|
|
} elseif ( 'billing' === $tax_based_on ) {
|
2016-03-17 19:39:29 +00:00
|
|
|
$country = $this->get_billing_country();
|
|
|
|
$state = $this->get_billing_state();
|
|
|
|
$postcode = $this->get_billing_postcode();
|
|
|
|
$city = $this->get_billing_city();
|
2012-12-03 15:13:59 +00:00
|
|
|
} else {
|
2014-11-18 14:32:48 +00:00
|
|
|
$country = $this->get_shipping_country();
|
|
|
|
$state = $this->get_shipping_state();
|
|
|
|
$postcode = $this->get_shipping_postcode();
|
|
|
|
$city = $this->get_shipping_city();
|
2012-10-01 09:45:07 +00:00
|
|
|
}
|
2012-12-03 15:13:59 +00:00
|
|
|
|
2012-10-01 09:45:07 +00:00
|
|
|
return apply_filters( 'woocommerce_customer_taxable_address', array( $country, $state, $postcode, $city ) );
|
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2015-03-27 16:43:04 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Gets a customer's downloadable products.
|
|
|
|
* @return array Array of downloadable products
|
|
|
|
*/
|
|
|
|
public function get_downloadable_products() {
|
|
|
|
$downloads = array();
|
2016-08-15 12:17:43 +00:00
|
|
|
if ( $this->get_id() ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$downloads = wc_get_customer_available_downloads( $this->get_id() );
|
2015-03-27 16:43:04 +00:00
|
|
|
}
|
2016-03-09 20:49:02 +00:00
|
|
|
return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
|
|
|
|
}
|
2015-03-27 16:43:04 +00:00
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/**
|
|
|
|
* Is the user a paying customer?
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function get_is_paying_customer() {
|
|
|
|
return (bool) $this->_data['is_paying_customer'];
|
|
|
|
}
|
2015-03-27 16:52:21 +00:00
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Setters
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Functions for setting customer data. These should not update anything in the
|
|
|
|
| database itself and should only change what is stored in the class
|
|
|
|
| object.
|
|
|
|
*/
|
2015-03-27 16:52:21 +00:00
|
|
|
|
2016-08-16 09:04:52 +00:00
|
|
|
/**
|
|
|
|
* Set customer ID.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param int $value
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-16 09:04:52 +00:00
|
|
|
*/
|
|
|
|
protected function set_id( $value ) {
|
|
|
|
$this->_data['id'] = absint( $value );
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/**
|
|
|
|
* Set customer's username.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $username
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-09 20:49:02 +00:00
|
|
|
*/
|
|
|
|
public function set_username( $username ) {
|
|
|
|
$this->_data['username'] = $username;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer's email.
|
|
|
|
* @since 2.7.0
|
2016-08-30 13:34:58 +00:00
|
|
|
* @param string $value
|
|
|
|
* @throws WC_Data_Exception
|
2016-03-09 20:49:02 +00:00
|
|
|
*/
|
2016-08-30 13:34:58 +00:00
|
|
|
public function set_email( $value ) {
|
|
|
|
if ( $value && ! is_email( $value ) ) {
|
|
|
|
$this->error( 'customer_invalid_email', __( 'Invalid email address', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
$this->_data['email'] = sanitize_email( $value );
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 16:22:47 +00:00
|
|
|
/**
|
|
|
|
* Set customer's first name.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $first_name
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-10 16:22:47 +00:00
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function set_first_name( $first_name ) {
|
|
|
|
$this->_data['first_name'] = $first_name;
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer's last name.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $last_name
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-10 16:22:47 +00:00
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function set_last_name( $last_name ) {
|
|
|
|
$this->_data['last_name'] = $last_name;
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer's user role(s).
|
|
|
|
* @since 2.7.0
|
2016-03-11 18:28:34 +00:00
|
|
|
* @param mixed $role
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-10 16:22:47 +00:00
|
|
|
*/
|
2016-03-11 18:28:34 +00:00
|
|
|
public function set_role( $role ) {
|
2016-08-30 13:34:58 +00:00
|
|
|
global $wp_roles;
|
|
|
|
|
|
|
|
if ( $role && ! empty( $wp_roles->roles ) && ! in_array( $role, array_keys( $wp_roles->roles ) ) ) {
|
|
|
|
$this->error( 'customer_invalid_role', __( 'Invalid role', 'woocommerce' ) );
|
|
|
|
}
|
2016-03-11 18:28:34 +00:00
|
|
|
$this->_data['role'] = $role;
|
|
|
|
}
|
2016-03-10 16:22:47 +00:00
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/**
|
|
|
|
* Set customer's password.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $password
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-09 20:49:02 +00:00
|
|
|
*/
|
|
|
|
public function set_password( $password ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_password = wc_clean( $password );
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 16:22:47 +00:00
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Set the date this customer was last updated.
|
2016-03-10 16:22:47 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
* @param integer $timestamp
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-10 16:22:47 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function set_date_modified( $timestamp ) {
|
2016-03-10 16:22:47 +00:00
|
|
|
$this->_data['date_modified'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-15 18:37:42 +00:00
|
|
|
* Set the date this customer was last updated.
|
2016-03-10 16:22:47 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
* @param integer $timestamp
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-10 16:22:47 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function set_date_created( $timestamp ) {
|
2016-03-11 18:28:34 +00:00
|
|
|
$this->_data['date_created'] = is_numeric( $timestamp ) ? $timestamp : strtotime( $timestamp );
|
2016-03-10 16:22:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/**
|
|
|
|
* Set customer address to match shop base address.
|
|
|
|
* @since 2.7.0
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-09 20:49:02 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_address_to_base() {
|
2016-03-09 20:49:02 +00:00
|
|
|
$base = wc_get_customer_default_location();
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['country'] = $base['country'];
|
|
|
|
$this->_data['billing']['state'] = $base['state'];
|
|
|
|
$this->_data['billing']['postcode'] = '';
|
|
|
|
$this->_data['billing']['city'] = '';
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer shipping address to base address.
|
|
|
|
* @since 2.7.0
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-09 20:49:02 +00:00
|
|
|
*/
|
2016-03-15 18:37:42 +00:00
|
|
|
public function set_shipping_address_to_base() {
|
2016-03-09 20:49:02 +00:00
|
|
|
$base = wc_get_customer_default_location();
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['country'] = $base['country'];
|
|
|
|
$this->_data['shipping']['state'] = $base['state'];
|
|
|
|
$this->_data['shipping']['postcode'] = '';
|
|
|
|
$this->_data['shipping']['city'] = '';
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets all shipping info at once.
|
|
|
|
* @param string $country
|
|
|
|
* @param string $state
|
|
|
|
* @param string $postcode
|
|
|
|
* @param string $city
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-09 20:49:02 +00:00
|
|
|
*/
|
|
|
|
public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['country'] = $country;
|
|
|
|
$this->_data['shipping']['state'] = $state;
|
|
|
|
$this->_data['shipping']['postcode'] = $postcode;
|
|
|
|
$this->_data['shipping']['city'] = $city;
|
2015-03-27 16:43:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Sets all address info at once.
|
2014-09-07 23:37:55 +00:00
|
|
|
* @param string $country
|
|
|
|
* @param string $state
|
2016-03-09 20:49:02 +00:00
|
|
|
* @param string $postcode
|
|
|
|
* @param string $city
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_location( $country, $state, $postcode = '', $city = '' ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['country'] = $country;
|
|
|
|
$this->_data['billing']['state'] = $state;
|
|
|
|
$this->_data['billing']['postcode'] = $postcode;
|
|
|
|
$this->_data['billing']['city'] = $city;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 13:13:20 +00:00
|
|
|
* Set billing first name.
|
|
|
|
* @return string
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-08-15 13:13:20 +00:00
|
|
|
public function set_billing_first_name( $value ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['first_name'] = $value;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-08-15 13:13:20 +00:00
|
|
|
* Set billing last name.
|
|
|
|
* @return string
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-08-15 13:13:20 +00:00
|
|
|
public function set_billing_last_name( $value ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['last_name'] = $value;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-08-09 20:51:38 +00:00
|
|
|
/**
|
2016-08-15 13:13:20 +00:00
|
|
|
* Set billing company.
|
|
|
|
* @return string
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
2016-08-15 13:13:20 +00:00
|
|
|
public function set_billing_company( $value ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['company'] = $value;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-15 13:13:20 +00:00
|
|
|
* Set billing phone.
|
|
|
|
* @return string
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
2016-08-15 13:13:20 +00:00
|
|
|
public function set_billing_phone( $value ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['phone'] = $value;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-15 13:13:20 +00:00
|
|
|
* Set billing email.
|
2016-08-30 13:34:58 +00:00
|
|
|
* @param string $value
|
2016-08-15 13:13:20 +00:00
|
|
|
* @return string
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
2016-08-30 13:34:58 +00:00
|
|
|
public function set_billing_email( $value ) {
|
|
|
|
if ( $value && ! is_email( $value ) ) {
|
|
|
|
$this->error( 'customer_invalid_billing_email', __( 'Invalid billing email address', 'woocommerce' ) );
|
|
|
|
}
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['email'] = sanitize_email( $value );
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set customer country.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param mixed $country
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_country( $country ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['country'] = $country;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set customer state.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param mixed $state
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_state( $state ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['state'] = $state;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Sets customer postcode.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param mixed $postcode
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_postcode( $postcode ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['postcode'] = $postcode;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-09-23 16:16:39 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Sets customer city.
|
2014-09-02 19:50:19 +00:00
|
|
|
* @param mixed $city
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_city( $city ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['city'] = $city;
|
2012-09-23 16:16:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set customer address.
|
2012-11-27 01:56:48 +00:00
|
|
|
* @param mixed $address
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_address( $address ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['address_1'] = $address;
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:14:50 +00:00
|
|
|
/**
|
|
|
|
* Set customer address.
|
|
|
|
* @param mixed $address
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-14 19:14:50 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_address_1( $address ) {
|
|
|
|
$this->set_billing_address( $address );
|
2016-03-14 19:14:50 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set customer's second address.
|
2015-08-05 13:05:00 +00:00
|
|
|
* @param mixed $address
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2016-03-17 19:39:29 +00:00
|
|
|
public function set_billing_address_2( $address ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['billing']['address_2'] = $address;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-08-09 20:51:38 +00:00
|
|
|
/**
|
|
|
|
* Sets customer shipping first name.
|
|
|
|
* @param string $first_name
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
|
|
|
public function set_shipping_first_name( $first_name ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['first_name'] = $first_name;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets customer shipping last name.
|
|
|
|
* @param string $last_name
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
|
|
|
public function set_shipping_last_name( $last_name ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['last_name'] = $last_name;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets customer shipping company.
|
|
|
|
* @param string $company.
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-08-09 20:51:38 +00:00
|
|
|
*/
|
|
|
|
public function set_shipping_company( $company ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['company'] = $company;
|
2016-08-09 20:51:38 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set shipping country.
|
2014-05-15 12:27:13 +00:00
|
|
|
* @param string $country
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_country( $country ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['country'] = $country;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set shipping state.
|
2014-05-15 12:27:13 +00:00
|
|
|
* @param string $state
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_state( $state ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['state'] = $state;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set shipping postcode.
|
2014-05-15 12:27:13 +00:00
|
|
|
* @param string $postcode
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_postcode( $postcode ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['postcode'] = $postcode;
|
2012-09-23 16:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Sets shipping city.
|
2014-09-02 19:50:19 +00:00
|
|
|
* @param string $city
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_city( $city ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['city'] = $city;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set shipping address.
|
2014-05-15 12:27:13 +00:00
|
|
|
* @param string $address
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_address( $address ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['address_1'] = $address;
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:14:50 +00:00
|
|
|
/**
|
|
|
|
* Set customer shipping address.
|
|
|
|
* @param mixed $address
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2016-03-14 19:14:50 +00:00
|
|
|
*/
|
|
|
|
public function set_shipping_address_1( $address ) {
|
|
|
|
$this->set_shipping_address( $address );
|
|
|
|
}
|
|
|
|
|
2012-11-27 01:56:48 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set second shipping address.
|
2015-08-05 13:05:00 +00:00
|
|
|
* @param string $address
|
2016-08-30 13:34:58 +00:00
|
|
|
* @throws WC_Data_Exception
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2015-08-05 13:05:00 +00:00
|
|
|
public function set_shipping_address_2( $address ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_data['shipping']['address_2'] = $address;
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 16:59:41 +00:00
|
|
|
/**
|
|
|
|
* Set if the user a paying customer.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param boolean $is_paying_customer
|
|
|
|
* @throws WC_Data_Exception
|
|
|
|
*/
|
|
|
|
function set_is_paying_customer( $is_paying_customer ) {
|
|
|
|
$this->_data['is_paying_customer'] = (bool) $is_paying_customer;
|
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set if customer has tax exemption.
|
2014-05-15 12:27:13 +00:00
|
|
|
* @param bool $is_vat_exempt
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_is_vat_exempt( $is_vat_exempt ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_is_vat_exempt = (bool) $is_vat_exempt;
|
2011-09-14 14:55:03 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2012-09-07 18:28:27 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Calculated shipping?
|
2014-09-07 23:37:55 +00:00
|
|
|
* @param boolean $calculated
|
2012-09-07 18:28:27 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
public function set_calculated_shipping( $calculated = true ) {
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_calculated_shipping = (bool) $calculated;
|
2012-09-07 18:28:27 +00:00
|
|
|
}
|
2016-08-31 12:25:13 +00:00
|
|
|
|
2016-03-08 21:44:28 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| CRUD methods
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Methods which create, read, update and delete from the database.
|
|
|
|
|
|
|
|
|
| A save method is included for convenience (chooses update or create based
|
|
|
|
| on if the order exists yet).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a customer.
|
|
|
|
* @since 2.7.0.
|
|
|
|
*/
|
|
|
|
public function create() {
|
2016-08-30 16:50:35 +00:00
|
|
|
$customer_id = wc_create_new_customer( $this->get_email(), $this->get_username(), $this->_password );
|
2016-08-15 15:53:48 +00:00
|
|
|
|
|
|
|
if ( ! is_wp_error( $customer_id ) ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['id'] = $customer_id;
|
2016-08-09 20:51:38 +00:00
|
|
|
update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
|
2016-03-17 19:39:29 +00:00
|
|
|
update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
|
2016-08-09 20:51:38 +00:00
|
|
|
update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
|
2016-03-09 20:49:02 +00:00
|
|
|
update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
|
|
|
|
update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
|
2016-03-10 16:22:47 +00:00
|
|
|
update_user_meta( $this->get_id(), 'last_update', $this->get_date_modified() );
|
2016-03-11 18:28:34 +00:00
|
|
|
update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
|
|
|
|
wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
|
2016-03-17 19:03:23 +00:00
|
|
|
$wp_user = new WP_User( $this->get_id() );
|
|
|
|
$this->set_date_created( strtotime( $wp_user->user_registered ) );
|
2016-08-16 10:57:25 +00:00
|
|
|
$this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) );
|
2016-08-15 12:17:43 +00:00
|
|
|
$this->read_meta_data();
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 13:48:29 +00:00
|
|
|
/**
|
|
|
|
* Callback which flattens post meta (gets the first value).
|
|
|
|
* @param array $value
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function flatten_post_meta( $value ) {
|
2016-08-31 14:43:34 +00:00
|
|
|
return is_array( $value ) ? current( $value ) : $value;
|
2016-08-30 13:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 21:44:28 +00:00
|
|
|
/**
|
|
|
|
* Read a customer from the database.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param integer $id
|
|
|
|
*/
|
|
|
|
public function read( $id ) {
|
2016-03-11 18:28:34 +00:00
|
|
|
global $wpdb;
|
2016-03-17 19:03:23 +00:00
|
|
|
|
2016-08-16 10:08:54 +00:00
|
|
|
// User object is required.
|
2016-08-16 09:04:52 +00:00
|
|
|
if ( ! $id || ! ( $user_object = get_user_by( 'id', $id ) ) || empty( $user_object->ID ) ) {
|
|
|
|
$this->set_id( 0 );
|
|
|
|
return;
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 10:08:54 +00:00
|
|
|
// Only users on this site should be read.
|
|
|
|
if ( is_multisite() && ! is_user_member_of_blog( $id ) ) {
|
|
|
|
$this->set_id( 0 );
|
|
|
|
return;
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 09:04:52 +00:00
|
|
|
$this->set_id( $user_object->ID );
|
2016-09-01 20:50:14 +00:00
|
|
|
$this->set_props( array_map( array( $this, 'flatten_post_meta' ), get_user_meta( $id ) ) );
|
2016-08-30 13:48:29 +00:00
|
|
|
$this->set_props( array(
|
|
|
|
'is_paying_customer' => get_user_meta( $id, 'paying_customer', true ),
|
|
|
|
'email' => $user_object->user_email,
|
|
|
|
'username' => $user_object->user_login,
|
|
|
|
'date_created' => strtotime( $user_object->user_registered ),
|
|
|
|
'date_modified' => get_user_meta( $id, 'last_update', true ),
|
|
|
|
'role' => ! empty ( $user_object->roles[0] ) ? $user_object->roles[0] : 'customer',
|
|
|
|
) );
|
2016-08-16 09:04:52 +00:00
|
|
|
$this->read_meta_data();
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a customer.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function update() {
|
2016-08-31 14:43:34 +00:00
|
|
|
wp_update_user( array( 'ID' => $this->get_id(), 'user_email' => $this->get_email() ) );
|
2016-03-09 20:49:02 +00:00
|
|
|
// Only update password if a new one was set with set_password
|
2016-08-30 16:50:35 +00:00
|
|
|
if ( ! empty( $this->_password ) ) {
|
2016-08-31 14:43:34 +00:00
|
|
|
wp_update_user( array( 'ID' => $this->get_id(), 'user_pass' => $this->_password ) );
|
2016-08-30 16:50:35 +00:00
|
|
|
$this->_password = '';
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 20:51:38 +00:00
|
|
|
update_user_meta( $this->get_id(), 'billing_first_name', $this->get_billing_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_last_name', $this->get_billing_last_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_company', $this->get_billing_company() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_phone', $this->get_billing_phone() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_email', $this->get_billing_email() );
|
2016-03-17 19:39:29 +00:00
|
|
|
update_user_meta( $this->get_id(), 'billing_postcode', $this->get_billing_postcode() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_city', $this->get_billing_city() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_1', $this->get_billing_address() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_2', $this->get_billing_address_2() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_state', $this->get_billing_state() );
|
2016-08-15 13:13:20 +00:00
|
|
|
update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
|
2016-03-17 19:39:29 +00:00
|
|
|
update_user_meta( $this->get_id(), 'billing_country', $this->get_billing_country() );
|
2016-08-09 20:51:38 +00:00
|
|
|
update_user_meta( $this->get_id(), 'shipping_first_name', $this->get_shipping_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_last_name', $this->get_shipping_last_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_company', $this->get_shipping_company() );
|
2016-03-09 20:49:02 +00:00
|
|
|
update_user_meta( $this->get_id(), 'shipping_postcode', $this->get_shipping_postcode() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_city', $this->get_shipping_city() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_address_1', $this->get_shipping_address() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_address_2', $this->get_shipping_address_2() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_state', $this->get_shipping_state() );
|
|
|
|
update_user_meta( $this->get_id(), 'shipping_country', $this->get_shipping_country() );
|
|
|
|
update_user_meta( $this->get_id(), 'paying_customer', $this->get_is_paying_customer() );
|
2016-03-17 19:03:23 +00:00
|
|
|
update_user_meta( $this->get_id(), 'first_name', $this->get_first_name() );
|
|
|
|
update_user_meta( $this->get_id(), 'last_name', $this->get_last_name() );
|
2016-03-11 18:28:34 +00:00
|
|
|
wp_update_user( array( 'ID' => $this->get_id(), 'role' => $this->get_role() ) );
|
2016-08-16 10:57:25 +00:00
|
|
|
$this->set_date_modified( get_user_meta( $this->get_id(), 'last_update', true ) );
|
2016-03-17 19:03:23 +00:00
|
|
|
$this->save_meta_data();
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a customer.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function delete() {
|
2016-03-09 20:49:02 +00:00
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-09 20:51:38 +00:00
|
|
|
return wp_delete_user( $this->get_id() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a customer and reassign posts..
|
|
|
|
*
|
|
|
|
* @param int $reassign Reassign posts and links to new User ID.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function delete_and_reassign( $reassign = null ) {
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return wp_delete_user( $this->get_id(), $reassign );
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-04 18:39:34 +00:00
|
|
|
* Save data. Create when creating a new user/class, update when editing
|
|
|
|
* an existing user, and save session when working on a logged out guest
|
|
|
|
* session.
|
2016-03-08 21:44:28 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function save() {
|
2016-08-15 12:17:43 +00:00
|
|
|
if ( $this->_is_session ) {
|
2016-08-31 12:25:13 +00:00
|
|
|
$this->save_to_session();
|
2016-08-15 12:17:43 +00:00
|
|
|
} elseif ( ! $this->get_id() ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->create();
|
|
|
|
} else {
|
2016-08-15 12:17:43 +00:00
|
|
|
$this->update();
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 21:44:28 +00:00
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/**
|
|
|
|
* Saves data to the session only (does not overwrite DB values).
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function save_to_session() {
|
|
|
|
$data = array();
|
|
|
|
foreach ( $this->_session_keys as $session_key ) {
|
2016-03-17 20:26:33 +00:00
|
|
|
$function_key = $session_key;
|
|
|
|
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
|
|
|
|
$session_key = str_replace( 'billing_', '', $session_key );
|
|
|
|
}
|
|
|
|
$data[ $session_key ] = $this->{"get_$function_key"}();
|
2016-03-09 20:49:02 +00:00
|
|
|
}
|
2016-08-31 12:25:13 +00:00
|
|
|
if ( $data !== WC()->session->get( 'customer' ) ) {
|
|
|
|
WC()->session->set( 'customer', $data );
|
|
|
|
}
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 14:43:34 +00:00
|
|
|
/**
|
|
|
|
* Callback to remove unwanted meta data.
|
|
|
|
*
|
|
|
|
* @param object $meta
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function exclude_internal_meta_keys( $meta ) {
|
|
|
|
global $wpdb;
|
|
|
|
return ! in_array( $meta->meta_key, $this->get_internal_meta_keys() )
|
|
|
|
&& 0 !== strpos( $meta->meta_key, 'closedpostboxes_' )
|
|
|
|
&& 0 !== strpos( $meta->meta_key, 'metaboxhidden_' )
|
|
|
|
&& 0 !== strpos( $meta->meta_key, 'manageedit-' )
|
|
|
|
&& ! strstr( $meta->meta_key, $wpdb->prefix );
|
|
|
|
}
|
2016-09-01 10:46:05 +00:00
|
|
|
|
2014-05-28 17:05:19 +00:00
|
|
|
}
|