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-08 21:44:28 +00:00
|
|
|
class WC_Customer extends WC_Legacy_Customer implements WC_Data {
|
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(
|
|
|
|
'id' => 0,
|
2016-03-09 20:49:02 +00:00
|
|
|
'email' => '',
|
|
|
|
'username' => '', // read only on updates
|
|
|
|
'password' => '', // write only
|
2016-03-08 21:44:28 +00:00
|
|
|
'postcode' => '',
|
|
|
|
'city' => '',
|
|
|
|
'address_1' => '',
|
|
|
|
'address_2' => '',
|
|
|
|
'state' => '',
|
|
|
|
'country' => '',
|
|
|
|
'shipping_postcode' => '',
|
|
|
|
'shipping_city' => '',
|
|
|
|
'shipping_address_1' => '',
|
|
|
|
'shipping_address_2' => '',
|
|
|
|
'shipping_state' => '',
|
|
|
|
'shipping_country' => '',
|
2016-03-09 20:49:02 +00:00
|
|
|
'is_paying_customer' => false,
|
|
|
|
'is_vat_exempt' => false, // session only.
|
|
|
|
'calculated_shipping' => false, // session only
|
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(
|
|
|
|
'postcode', 'city', 'address_1', 'address_2', 'state', 'country',
|
|
|
|
'shipping_postcode', 'shipping_city', 'shipping_address_1', 'shipping_address_2',
|
|
|
|
'shipping_state', 'shipping_country', 'is_vat_exempt', 'calculated_shipping',
|
|
|
|
);
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-11-18 14:32:48 +00:00
|
|
|
/**
|
2016-03-08 21:44:28 +00:00
|
|
|
* Was data changed in the database for this class?
|
|
|
|
* @var boolean
|
2014-11-18 14:32:48 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
protected $_changed = false;
|
2013-01-12 13:02:47 +00:00
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-03-08 21:44:28 +00:00
|
|
|
* If some of the customer information is loaded by session (instead of just from the DB).
|
|
|
|
* @var boolean
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
protected $_from_session = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Customer can also return an object for a logged out user (session).
|
|
|
|
* $_is_user will be false in this case. It will be true for all other cases
|
|
|
|
* (logged in users or getting a WC_Customer for another object)
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $_is_user = false;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-03-08 21:44:28 +00:00
|
|
|
/**
|
|
|
|
* Load customer data based on how WC_Customer is called.
|
2016-03-09 20:49:02 +00:00
|
|
|
* @param mixed $customer WC_Customer object or customer ID is accepted.
|
|
|
|
* if $customer is null, you can build a new WC_Customer object. If it's empty, some
|
|
|
|
* data will be pulled from the session for the current user/customer.
|
2016-03-08 21:44:28 +00:00
|
|
|
*/
|
|
|
|
public function __construct( $customer = '' ) {
|
|
|
|
if ( $customer instanceof WC_Customer ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_is_user = true;
|
2016-03-08 21:44:28 +00:00
|
|
|
$this->read( absint( $customer->get_id() ) );
|
|
|
|
} else if ( is_numeric( $customer ) ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_is_user = true;
|
2016-03-08 21:44:28 +00:00
|
|
|
$this->read( $customer );
|
2016-03-09 20:49:02 +00:00
|
|
|
} else if ( is_null( $customer ) ) {
|
|
|
|
$this->_is_user = true; // not an existing user yet, we are creating a new one
|
2016-03-08 21:44:28 +00:00
|
|
|
} else if ( empty( $customer ) ) {
|
|
|
|
$this->_from_session = true;
|
|
|
|
if ( is_user_logged_in() ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_is_user = true;
|
2016-03-08 21:44:28 +00:00
|
|
|
$this->read( get_current_user_id() );
|
|
|
|
} else {
|
|
|
|
$this->read( WC()->session->get_customer_id() );
|
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-03-08 21:44:28 +00:00
|
|
|
if ( $this->_from_session ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
add_action( 'shutdown', array( $this, 'save_session_if_changed' ), 10 );
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
2012-09-07 18:28:27 +00:00
|
|
|
}
|
2012-09-23 16:16:39 +00:00
|
|
|
|
2013-01-12 13:02:47 +00:00
|
|
|
/**
|
2016-03-08 21:44:28 +00:00
|
|
|
* Saves customer information to the current session if any data changed.
|
|
|
|
* @since 2.7.0
|
2013-01-12 13:02:47 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
public function save_session_if_changed() {
|
2014-05-08 09:35:51 +00:00
|
|
|
if ( $this->_changed ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->save_to_session();
|
2014-05-08 09:35:51 +00:00
|
|
|
}
|
2013-01-12 13:02:47 +00:00
|
|
|
}
|
|
|
|
|
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-03-09 20:49:02 +00:00
|
|
|
* Return a customer's user ID. If the current customer is logged out, this will be a session key.
|
2016-03-08 21:44:28 +00:00
|
|
|
* @since 2.7.0
|
2016-03-09 20:49:02 +00:00
|
|
|
* @return mixed
|
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() {
|
|
|
|
return sanitize_email( $this->_data['email'] );
|
2014-04-04 07:38:56 +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-09 20:49:02 +00:00
|
|
|
public function get_postcode() {
|
|
|
|
return wc_format_postcode( $this->_data['postcode'], $this->get_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-09 20:49:02 +00:00
|
|
|
public function get_city() {
|
|
|
|
return $this->_data['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-09 20:49:02 +00:00
|
|
|
public function get_address() {
|
|
|
|
return $this->_data['address_1'];
|
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 second address.
|
2013-11-27 09:03:47 +00:00
|
|
|
* @return string
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
public function get_address_2() {
|
|
|
|
return $this->_data['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-09 20:49:02 +00:00
|
|
|
public function get_state() {
|
|
|
|
return $this->_data['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-09 20:49:02 +00:00
|
|
|
public function get_country() {
|
|
|
|
return $this->_data['country'];
|
2012-11-27 01:56:48 +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-03-09 20:49:02 +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-03-09 20:49:02 +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-03-09 20:49:02 +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-03-09 20:49:02 +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-03-09 20:49:02 +00:00
|
|
|
return $this->_data['shipping_address_1'];
|
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'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-03-09 20:49:02 +00:00
|
|
|
return $this->_data['shipping_address_2'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get if customer is VAT exempt?
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function get_is_vat_exempt() {
|
|
|
|
return ( ! empty( $this->_data['is_vat_exempt'] ) ) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Has customer calculated shipping?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function get_calculated_shipping() {
|
|
|
|
return ! empty( $this->_data['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-03-09 20:49:02 +00:00
|
|
|
if ( true === (bool) apply_filters( 'woocommerce_apply_base_tax_for_local_pickup', true ) && WC()->cart->needs_shipping() && sizeof( array_intersect( WC()->session->get( 'chosen_shipping_methods', array() ), 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 ) {
|
2014-11-18 14:32:48 +00:00
|
|
|
$country = $this->get_country();
|
|
|
|
$state = $this->get_state();
|
|
|
|
$postcode = $this->get_postcode();
|
|
|
|
$city = $this->get_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();
|
|
|
|
if ( $this->_is_user ) {
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* Get all class data in array format.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_data() {
|
|
|
|
return $this->_data;
|
|
|
|
}
|
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-03-09 20:49:02 +00:00
|
|
|
/**
|
|
|
|
* Set customer's username.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $username
|
|
|
|
*/
|
|
|
|
public function set_username( $username ) {
|
|
|
|
$this->_data['username'] = $username;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer's email.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $email
|
|
|
|
*/
|
|
|
|
public function set_email( $email ) {
|
|
|
|
$this->_data['email'] = sanitize_email( $email );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer's password.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param string $password
|
|
|
|
*/
|
|
|
|
public function set_password( $password ) {
|
|
|
|
$this->_data['password'] = wc_clean( $password );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer address to match shop base address.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function set_address_to_base() {
|
|
|
|
$base = wc_get_customer_default_location();
|
|
|
|
$this->_data['country'] = $base['country'];
|
|
|
|
$this->_data['state'] = $base['state'];
|
|
|
|
$this->_data['postcode'] = '';
|
|
|
|
$this->_data['city'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set customer shipping address to base address.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function set_address_shipping_to_base() {
|
|
|
|
$base = wc_get_customer_default_location();
|
|
|
|
$this->_data['shipping_country'] = $base['country'];
|
|
|
|
$this->_data['shipping_state'] = $base['state'];
|
|
|
|
$this->_data['shipping_postcode'] = '';
|
|
|
|
$this->_data['shipping_city'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets all shipping info at once.
|
|
|
|
* @param string $country
|
|
|
|
* @param string $state
|
|
|
|
* @param string $postcode
|
|
|
|
* @param string $city
|
|
|
|
*/
|
|
|
|
public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
|
|
|
|
$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
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_location( $country, $state, $postcode = '', $city = '' ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['country'] = $country;
|
|
|
|
$this->_data['state'] = $state;
|
|
|
|
$this->_data['postcode'] = $postcode;
|
|
|
|
$this->_data['city'] = $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
|
|
|
* Set customer country.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param mixed $country
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_country( $country ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['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 customer state.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param mixed $state
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_state( $state ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['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
|
|
|
* Sets customer postcode.
|
2012-08-14 19:42:38 +00:00
|
|
|
* @param mixed $postcode
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_postcode( $postcode ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['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
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_city( $city ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['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
|
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_address( $address ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['address_1'] = $address;
|
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
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2015-08-05 13:05:00 +00:00
|
|
|
public function set_address_2( $address ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['address_2'] = $address;
|
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 country.
|
2014-05-15 12:27:13 +00:00
|
|
|
* @param string $country
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_country( $country ) {
|
2016-03-09 20:49:02 +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
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_state( $state ) {
|
2016-03-09 20:49:02 +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
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_postcode( $postcode ) {
|
2016-03-09 20:49:02 +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
|
2012-09-23 16:16:39 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_city( $city ) {
|
2016-03-09 20:49:02 +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
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2012-12-14 21:43:45 +00:00
|
|
|
public function set_shipping_address( $address ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['shipping_address_1'] = $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
|
2012-11-27 01:56:48 +00:00
|
|
|
*/
|
2015-08-05 13:05:00 +00:00
|
|
|
public function set_shipping_address_2( $address ) {
|
2016-03-09 20:49:02 +00:00
|
|
|
$this->_data['shipping_address_2'] = $address;
|
2012-11-27 01:56:48 +00:00
|
|
|
}
|
|
|
|
|
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-03-09 20:49:02 +00:00
|
|
|
$this->_data['is_vat_exempt'] = $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 ) {
|
|
|
|
$this->_data['calculated_shipping'] = $calculated;
|
2012-09-07 18:28:27 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Set if the user a paying customer.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param boolean $is_paying_customer
|
2011-08-09 15:16:18 +00:00
|
|
|
*/
|
2016-03-09 20:49:02 +00:00
|
|
|
function set_is_paying_customer( $is_paying_customer ) {
|
|
|
|
$this->_data['is_paying_customer'] = (bool) $is_paying_customer;
|
|
|
|
}
|
2014-02-17 11:58:14 +00:00
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Other methods
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Other functions for interacting with customers.
|
|
|
|
*/
|
2012-08-28 15:21:54 +00:00
|
|
|
|
2016-03-09 20:49:02 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-08-14 19:42:38 +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-03-09 20:49:02 +00:00
|
|
|
$customer_id = wc_create_new_customer( $this->get_email(), $this->get_username(), $this->_data['password'] );
|
|
|
|
if ( $customer_id ) {
|
|
|
|
$this->_data['id'] = $customer_id;
|
|
|
|
update_user_meta( $this->get_id(), 'billing_postcode', $this->get_postcode() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_city', $this->get_city() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_1', $this->get_address() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_2', $this->get_address_2() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_state', $this->get_state() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_country', $this->get_country() );
|
|
|
|
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-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a customer from the database.
|
|
|
|
* @since 2.7.0
|
|
|
|
* @param integer $id
|
|
|
|
*/
|
|
|
|
public function read( $id ) {
|
|
|
|
$pull_from_db = true;
|
|
|
|
if ( $this->_from_session ) {
|
|
|
|
$data = (array) WC()->session->get( 'customer' );
|
|
|
|
if ( ! empty( $data ) ) {
|
|
|
|
$pull_from_db = false;
|
|
|
|
foreach ( $this->_session_keys as $session_key ) {
|
|
|
|
$this->_data[ $session_key ] = $data[ $session_key ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $pull_from_db ) {
|
|
|
|
foreach ( array_keys( $this->_data ) as $key ) {
|
|
|
|
$meta_value = get_user_meta( $id, ( false === strstr( $key, 'shipping_' ) ? 'billing_' : '' ) . $key, true );
|
|
|
|
$this->_data[ $key ] = $meta_value ? $meta_value : $this->_data[ $key ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
if ( $this->_is_user ) {
|
|
|
|
$this->_data['is_paying_customer'] = get_user_meta( $id, 'paying_customer', true );
|
|
|
|
$wp_user = new WP_User( $id );
|
|
|
|
$this->_data['email'] = $wp_user->user_email;
|
|
|
|
$this->_data['username'] = $wp_user->user_login;
|
|
|
|
}
|
|
|
|
|
2016-03-08 21:44:28 +00:00
|
|
|
$this->_data['id'] = $id;
|
|
|
|
|
|
|
|
// Set some defaults if some of our values are still not set.
|
|
|
|
if ( empty( $this->_data['country'] ) ) {
|
|
|
|
$this->_data['country'] = $this->get_default_country();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $this->_data['shipping_country'] ) ) {
|
|
|
|
$this->_data['shipping_country'] = $this->_data['country'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $this->_data['state'] ) ) {
|
|
|
|
$this->_data['state'] = $this->get_default_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $this->_data['shipping_state'] ) ) {
|
|
|
|
$this->_data['shipping_state'] = $this->_data['state'];
|
|
|
|
}
|
2016-03-09 20:49:02 +00:00
|
|
|
|
|
|
|
unset( $this->_data['password'] ); // password is write only, never ever read it
|
|
|
|
|
|
|
|
error_log( 'read' );
|
|
|
|
error_log( print_r ( $this->get_id(), 1 ) );
|
|
|
|
error_log( print_r ( $this->_data, 1 ) );
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a customer.
|
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function update() {
|
2016-03-09 20:49:02 +00:00
|
|
|
$customer_ID = $this->get_id();
|
2016-03-08 21:44:28 +00:00
|
|
|
|
2016-03-09 20:49:02 +00:00
|
|
|
wp_update_user( array( 'ID' => $customer_ID, 'user_email' => $this->get_email() ) );
|
|
|
|
// Only update password if a new one was set with set_password
|
|
|
|
if ( isset( $this->_data['password'] ) ) {
|
|
|
|
wp_update_user( array( 'ID' => $customer_ID, 'user_pass' => $this->_data['password'] ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
update_user_meta( $this->get_id(), 'billing_postcode', $this->get_postcode() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_city', $this->get_city() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_1', $this->get_address() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_address_2', $this->get_address_2() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_state', $this->get_state() );
|
|
|
|
update_user_meta( $this->get_id(), 'billing_country', $this->get_country() );
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
wp_delete_user( $this->get_id() );
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-09 20:49:02 +00:00
|
|
|
* Save data (either create or update depending on if we are working on an existing customer).
|
2016-03-08 21:44:28 +00:00
|
|
|
* @since 2.7.0
|
|
|
|
*/
|
|
|
|
public function save() {
|
2016-03-09 20:49:02 +00:00
|
|
|
if ( ! $this->_is_user ) {
|
|
|
|
$this->create();
|
|
|
|
} else {
|
|
|
|
if ( ! $this->get_id() ) {
|
|
|
|
$this->create();
|
|
|
|
} else {
|
|
|
|
$this->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
if ( ! $this->_from_session ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$data = array();
|
|
|
|
foreach ( $this->_session_keys as $session_key ) {
|
|
|
|
$data[ $session_key ] = $this->_data[ $session_key ];
|
|
|
|
}
|
|
|
|
WC()->session->set( 'customer', $data );
|
2016-03-08 21:44:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 17:05:19 +00:00
|
|
|
}
|