woocommerce/includes/class-wc-customer.php

660 lines
15 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
include_once( 'legacy/class-wc-legacy-customer.php' );
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
2015-11-06 09:22:19 +00:00
}
2011-08-09 15:16:18 +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
*
* @class WC_Customer
* @version 2.7.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
2011-08-09 15:16:18 +00:00
*/
class WC_Customer extends WC_Legacy_Customer implements WC_Data {
2012-11-27 16:22:47 +00:00
/**
2015-11-03 13:31:20 +00:00
* Stores customer data.
* @var array
*/
protected $_data = array(
'id' => 0,
'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' => false,
'calculated_shipping' => false,
'is_user' => false,
);
/**
* 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
/**
* Was data changed in the database for this class?
* @var boolean
*/
private $_changed = false;
2012-08-14 19:42:38 +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
*/
private $_from_session = false;
2012-11-27 16:22:47 +00:00
/**
* Load customer data based on how WC_Customer is called.
*/
public function __construct( $customer = '' ) {
if ( $customer instanceof WC_Customer ) {
$this->_data['is_user'] = true;
$this->read( absint( $customer->get_id() ) );
} else if ( is_numeric( $customer ) ) {
$this->_data['is_user'] = true;
$this->read( $customer );
} else if ( empty( $customer ) ) {
$this->_from_session = true;
if ( is_user_logged_in() ) {
$this->_data['is_user'] = true;
$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
if ( $this->_from_session ) {
add_action( 'shutdown', array( $this, 'save_session' ), 10 );
}
2012-09-07 18:28:27 +00:00
}
2012-09-23 16:16:39 +00:00
/**
* Saves customer information to the current session if any data changed.
* @since 2.7.0
*/
public function save_session() {
2014-05-08 09:35:51 +00:00
if ( $this->_changed ) {
$data = array();
foreach ( $this->_session_keys as $session_key ) {
$data[ $session_key ] = $this->_data[ $session_key ];
}
WC()->session->set( 'customer', $data );
2014-05-08 09:35:51 +00:00
}
}
/**
* Return a customer's user ID.
* @since 2.7.0
* @return integer
*/
public function get_id() {
}
/**
* Get all class data in array format.
* @since 2.7.0
* @return array
*/
public function get_data() {
}
/**
2015-11-03 13:31:20 +00:00
* Get default country for a customer.
2015-03-27 16:43:04 +00:00
*
* @return string
*/
public function get_default_country() {
$default = wc_get_customer_default_location();
return $default['country'];
}
/**
2015-11-03 13:31:20 +00:00
* Get default state for a customer.
2015-03-27 16:43:04 +00:00
*
* @return string
*/
public function get_default_state() {
$default = wc_get_customer_default_location();
return $default['state'];
}
2012-11-27 16:22:47 +00:00
/**
* Has calculated shipping?
2012-08-14 19:42:38 +00:00
*
* @return bool
*/
public function has_calculated_shipping() {
2015-06-22 13:55:15 +00:00
return ! empty( $this->calculated_shipping );
}
2012-08-14 19:42:38 +00:00
/**
* Set customer address to match shop base address.
*/
public function set_to_base() {
$this->country = $this->get_default_country();
$this->state = $this->get_default_state();
$this->postcode = '';
$this->city = '';
}
2012-08-14 19:42:38 +00:00
/**
* Set customer shipping address to base address.
*/
public function set_shipping_to_base() {
$this->shipping_country = $this->get_default_country();
$this->shipping_state = $this->get_default_state();
$this->shipping_postcode = '';
$this->shipping_city = '';
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Is customer outside base country (for tax purposes)?
2012-08-14 19:42:38 +00:00
*
* @return bool
*/
public function is_customer_outside_base() {
list( $country, $state ) = $this->get_taxable_address();
2012-11-27 16:22:47 +00:00
if ( $country ) {
2012-08-14 19:42:38 +00:00
$default = wc_get_base_location();
2012-11-27 16:22:47 +00:00
if ( $default['country'] !== $country ) {
return true;
}
2012-08-14 19:42:38 +00:00
if ( $default['state'] && $default['state'] !== $state ) {
return true;
}
2012-08-14 19:42:38 +00:00
2012-09-23 16:16:39 +00:00
}
2011-08-09 15:16:18 +00:00
return false;
}
/**
* Is the user a paying customer?
*
* @return bool
*/
function is_paying_customer( $user_id ) {
2014-04-04 13:23:15 +00:00
return '1' === get_user_meta( $user_id, 'paying_customer', true );
}
2012-08-14 19:42:38 +00:00
/**
* Is customer VAT exempt?
*
* @return bool
*/
public function is_vat_exempt() {
return ( ! empty( $this->is_vat_exempt ) ) ? true : false;
}
2012-08-14 19:42:38 +00:00
/**
* Gets the state from the current session.
*
* @return string
*/
public function get_state() {
return $this->state;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
2015-11-03 13:31:20 +00:00
* Gets the country from the current session.
2012-08-14 19:42:38 +00:00
*
* @return string
*/
public function get_country() {
return $this->country;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the postcode from the current session.
*
* @return string
*/
public function get_postcode() {
return empty( $this->postcode ) ? '' : wc_format_postcode( $this->postcode, $this->get_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
/**
* Get the city from the current session.
2012-11-27 16:22:47 +00:00
*
* @return string
2012-09-23 16:16:39 +00:00
*/
public function get_city() {
return $this->city;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the address from the current session.
2012-11-27 16:22:47 +00:00
*
* @return string
*/
public function get_address() {
return $this->address_1;
}
2012-11-27 16:22:47 +00:00
/**
* Gets the address_2 from the current session.
2012-11-27 16:22:47 +00:00
*
* @return string
*/
public function get_address_2() {
return $this->address_2;
}
2012-08-14 19:42:38 +00:00
/**
* Gets the state from the current session.
*
* @return string
*/
public function get_shipping_state() {
return $this->shipping_state;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the country from the current session.
*
* @return string
*/
public function get_shipping_country() {
return $this->shipping_country;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the postcode from the current session.
*
* @return string
*/
public function get_shipping_postcode() {
return empty( $this->shipping_postcode ) ? '' : wc_format_postcode( $this->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
/**
* Gets the city from the current session.
2012-11-27 16:22:47 +00:00
*
* @return string
2012-09-23 16:16:39 +00:00
*/
public function get_shipping_city() {
return $this->shipping_city;
2011-08-09 15:16:18 +00:00
}
2012-11-27 16:22:47 +00:00
/**
* Gets the address from the current session.
2012-11-27 16:22:47 +00:00
*
* @return string
*/
public function get_shipping_address() {
return $this->shipping_address_1;
}
2012-11-27 16:22:47 +00:00
/**
* Gets the address_2 from the current session.
2012-11-27 16:22:47 +00:00
*
* @return string
*/
public function get_shipping_address_2() {
return $this->shipping_address_2;
}
2012-11-27 16:22:47 +00:00
/**
* Get taxable address.
2012-11-27 16:22:47 +00:00
*
* @return array
*/
public function get_taxable_address() {
2012-12-03 15:13:59 +00:00
$tax_based_on = get_option( 'woocommerce_tax_based_on' );
// Check shipping method at this point to see if we need special handling
if ( true == 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 ) {
$tax_based_on = 'base';
}
if ( 'base' === $tax_based_on ) {
2012-12-03 15:13:59 +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();
2012-12-03 15:13:59 +00:00
2015-06-17 14:19:04 +00:00
} elseif ( 'billing' === $tax_based_on ) {
$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 {
$country = $this->get_shipping_country();
$state = $this->get_shipping_state();
$postcode = $this->get_shipping_postcode();
$city = $this->get_shipping_city();
}
2012-12-03 15:13:59 +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
/**
2015-11-03 13:31:20 +00:00
* Set default data for a customer.
2015-03-27 16:43:04 +00:00
*/
public function set_default_data( $get_user_profile_data = true ) {
2015-03-27 16:43:04 +00:00
$this->_data = array(
'postcode' => '',
'city' => '',
'address_1' => '',
2015-03-27 16:43:04 +00:00
'address_2' => '',
'state' => '',
'country' => '',
'shipping_postcode' => '',
'shipping_city' => '',
'shipping_address_1' => '',
2015-03-27 16:43:04 +00:00
'shipping_address_2' => '',
'shipping_state' => '',
'shipping_country' => '',
'is_vat_exempt' => false,
'calculated_shipping' => false
);
if ( is_user_logged_in() && $get_user_profile_data ) {
2015-03-27 16:43:04 +00:00
foreach ( $this->_data as $key => $value ) {
$meta_value = get_user_meta( get_current_user_id(), ( false === strstr( $key, 'shipping_' ) ? 'billing_' : '' ) . $key, true );
$this->_data[ $key ] = $meta_value ? $meta_value : $this->_data[ $key ];
}
}
if ( empty( $this->_data['country'] ) ) {
$this->_data['country'] = $this->get_default_country();
}
2015-03-27 16:52:21 +00:00
2015-03-27 16:43:04 +00:00
if ( empty( $this->_data['shipping_country'] ) ) {
$this->_data['shipping_country'] = $this->_data['country'];
2015-03-27 16:43:04 +00:00
}
2015-03-27 16:52:21 +00:00
2015-03-27 16:43:04 +00:00
if ( empty( $this->_data['state'] ) ) {
$this->_data['state'] = $this->get_default_state();
}
2015-03-27 16:52:21 +00:00
2015-03-27 16:43:04 +00:00
if ( empty( $this->_data['shipping_state'] ) ) {
$this->_data['shipping_state'] = $this->_data['state'];
2015-03-27 16:43:04 +00:00
}
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the location.
2012-11-27 16:22:47 +00:00
*
2014-09-07 23:37:55 +00:00
* @param string $country
* @param string $state
2012-08-14 19:42:38 +00:00
* @param string $postcode (default: '')
2012-09-23 16:16:39 +00:00
* @param string $city (default: '')
2012-08-14 19:42:38 +00:00
*/
public function set_location( $country, $state, $postcode = '', $city = '' ) {
$this->country = $country;
$this->state = $state;
$this->postcode = $postcode;
$this->city = $city;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the country.
*
* @param mixed $country
*/
public function set_country( $country ) {
$this->country = $country;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the state.
*
* @param mixed $state
*/
public function set_state( $state ) {
$this->state = $state;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the postcode.
*
* @param mixed $postcode
*/
public function set_postcode( $postcode ) {
$this->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
/**
* Sets session data for the city.
*
2014-09-02 19:50:19 +00:00
* @param mixed $city
2012-09-23 16:16:39 +00:00
*/
public function set_city( $city ) {
$this->city = $city;
2012-09-23 16:16:39 +00:00
}
/**
* Sets session data for the address.
*
* @param mixed $address
*/
public function set_address( $address ) {
$this->address_1 = $address;
}
/**
* Sets session data for the $address.
*
* @param mixed $address
*/
public function set_address_2( $address ) {
$this->address_2 = $address;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the location.
*
2014-09-07 23:37:55 +00:00
* @param string $country
2012-08-14 19:42:38 +00:00
* @param string $state (default: '')
* @param string $postcode (default: '')
2012-11-27 16:22:47 +00:00
* @param string $city (default: '')
2012-08-14 19:42:38 +00:00
*/
public function set_shipping_location( $country, $state = '', $postcode = '', $city = '' ) {
$this->shipping_country = $country;
$this->shipping_state = $state;
$this->shipping_postcode = $postcode;
$this->shipping_city = $city;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the country.
*
* @param string $country
2012-08-14 19:42:38 +00:00
*/
public function set_shipping_country( $country ) {
$this->shipping_country = $country;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the state.
*
* @param string $state
2012-08-14 19:42:38 +00:00
*/
public function set_shipping_state( $state ) {
$this->shipping_state = $state;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the postcode.
*
* @param string $postcode
2012-08-14 19:42:38 +00:00
*/
public function set_shipping_postcode( $postcode ) {
$this->shipping_postcode = $postcode;
2012-09-23 16:16:39 +00:00
}
/**
* Sets session data for the city.
*
2014-09-02 19:50:19 +00:00
* @param string $city
2012-09-23 16:16:39 +00:00
*/
public function set_shipping_city( $city ) {
$this->shipping_city = $city;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the address.
*
* @param string $address
*/
public function set_shipping_address( $address ) {
$this->shipping_address_1 = $address;
}
/**
* Sets session data for the address_2.
*
* @param string $address
*/
public function set_shipping_address_2( $address ) {
$this->shipping_address_2 = $address;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the tax exemption.
*
* @param bool $is_vat_exempt
2012-08-14 19:42:38 +00:00
*/
public function set_is_vat_exempt( $is_vat_exempt ) {
$this->is_vat_exempt = $is_vat_exempt;
}
2012-08-14 19:42:38 +00:00
2012-09-07 18:28:27 +00:00
/**
* Calculated shipping.
2012-11-27 16:22:47 +00:00
*
2014-09-07 23:37:55 +00:00
* @param boolean $calculated
2012-09-07 18:28:27 +00:00
*/
public function calculated_shipping( $calculated = true ) {
$this->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
/**
2012-08-14 19:42:38 +00:00
* Gets a user's downloadable products if they are logged in.
2011-08-09 15:16:18 +00:00
*
2012-08-14 19:42:38 +00:00
* @return array Array of downloadable products
2011-08-09 15:16:18 +00:00
*/
public function get_downloadable_products() {
$downloads = array();
if ( is_user_logged_in() ) {
$downloads = wc_get_customer_available_downloads( get_current_user_id() );
}
return apply_filters( 'woocommerce_customer_get_downloadable_products', $downloads );
2012-08-14 19:42:38 +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() {
}
/**
* 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 ];
}
}
$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'];
}
}
/**
* Update a customer.
* @since 2.7.0
*/
public function update() {
}
/**
* Delete a customer.
* @since 2.7.0
*/
public function delete() {
}
/**
* Save data (either create or update depending on if we are working on an existing coupon).
* @since 2.7.0
*/
public function save() {
}
}