2011-08-09 15:16:18 +00:00
< ? php
/**
* Customer
2012-08-14 19:42:38 +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
*
2012-01-27 16:38:39 +00:00
* @ class WC_Customer
2012-08-14 19:42:38 +00:00
* @ version 1.6 . 4
2012-08-14 22:43:48 +00:00
* @ package WooCommerce / Classes
2012-08-14 19:42:38 +00:00
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Customer {
2012-09-07 18:28:27 +00:00
/** Stores customer data as an array */
2012-09-23 16:16:39 +00:00
protected $_data ;
2012-09-07 18:28:27 +00:00
2012-08-14 19:42:38 +00:00
/**
2012-09-07 18:28:27 +00:00
* Constructor for the customer class loads the customer data .
2012-08-14 19:42:38 +00:00
*
* @ access public
* @ return void
*/
2011-09-06 11:11:22 +00:00
function __construct () {
2012-09-07 18:28:27 +00:00
global $woocommerce ;
if ( empty ( $woocommerce -> session -> customer ) ) {
2012-08-14 19:42:38 +00:00
2012-09-07 18:28:27 +00:00
$default = apply_filters ( 'woocommerce_customer_default_location' , get_option ( 'woocommerce_default_country' ) );
2012-08-14 19:42:38 +00:00
if ( strstr ( $default , ':' ) ) {
2012-09-07 18:28:27 +00:00
list ( $country , $state ) = explode ( ':' , $default );
2012-08-14 19:42:38 +00:00
} else {
2011-08-09 15:16:18 +00:00
$country = $default ;
$state = '' ;
2012-08-14 19:42:38 +00:00
}
2012-09-23 16:16:39 +00:00
$this -> _data = array (
2012-09-07 18:28:27 +00:00
'country' => $country ,
'state' => '' ,
'postcode' => '' ,
2012-09-23 16:16:39 +00:00
'city' => '' ,
2012-09-07 18:28:27 +00:00
'shipping_country' => $country ,
'shipping_state' => '' ,
'shipping_postcode' => '' ,
2012-09-23 16:16:39 +00:00
'shipping_city' => '' ,
2012-09-07 18:28:27 +00:00
'is_vat_exempt' => false ,
'calculated_shipping' => false
2012-08-14 19:42:38 +00:00
);
2012-09-07 18:28:27 +00:00
} else {
2012-09-23 16:16:39 +00:00
$this -> _data = $woocommerce -> session -> customer ;
2012-08-14 19:42:38 +00:00
}
2012-09-07 18:28:27 +00:00
// When leaving or ending page load, store data
add_action ( 'shutdown' , array ( & $this , 'save_data' ), 10 );
}
2012-09-23 16:16:39 +00:00
/**
* __get function .
*
* @ access public
* @ param mixed $property
* @ return mixed
*/
public function __get ( $property ) {
return isset ( $this -> _data [ $property ] ) ? $this -> _data [ $property ] : null ;
}
/**
* __set function .
*
* @ access public
* @ param mixed $property
* @ param mixed $value
* @ return void
*/
public function __set ( $property , $value ) {
$this -> _data [ $property ] = $value ;
}
2012-09-07 18:28:27 +00:00
/**
* save_data function .
*
* @ access public
* @ return void
*/
function save_data () {
global $woocommerce ;
2012-09-23 16:16:39 +00:00
$woocommerce -> session -> customer = $this -> _data ;
2011-12-14 23:50:32 +00:00
}
2012-08-14 19:42:38 +00:00
2012-05-09 17:29:22 +00:00
/**
* has_calculated_shipping function .
2012-08-14 19:42:38 +00:00
*
2012-05-09 17:29:22 +00:00
* @ access public
* @ return bool
*/
function has_calculated_shipping () {
2012-09-23 16:16:39 +00:00
return ( ! empty ( $this -> _data [ 'calculated_shipping' ] ) && $this -> _data [ 'calculated_shipping' ] ) ? true : false ;
2012-05-09 17:29:22 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Set customer address to match shop base address .
*
* @ access public
* @ return void
*/
2011-12-14 23:50:32 +00:00
function set_to_base () {
2012-09-07 18:28:27 +00:00
global $woocommerce ;
2012-08-31 17:40:58 +00:00
$default = apply_filters ( 'woocommerce_customer_default_location' , get_option ( 'woocommerce_default_country' ) );
2012-09-07 18:28:27 +00:00
if ( strstr ( $default , ':' ) ) {
list ( $country , $state ) = explode ( ':' , $default );
} else {
2011-12-14 23:50:32 +00:00
$country = $default ;
$state = '' ;
2012-09-07 18:28:27 +00:00
}
2012-09-23 16:16:39 +00:00
$this -> _data [ 'country' ] = $country ;
$this -> _data [ 'state' ] = $state ;
$this -> _data [ 'postcode' ] = '' ;
$this -> _data [ 'city' ] = '' ;
2011-12-14 23:50:32 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Set customer shipping address to base address .
*
* @ access public
* @ return void
*/
2011-12-14 23:50:32 +00:00
function set_shipping_to_base () {
2012-09-07 18:28:27 +00:00
global $woocommerce ;
2011-12-14 23:50:32 +00:00
$default = get_option ( 'woocommerce_default_country' );
2012-09-07 18:28:27 +00:00
if ( strstr ( $default , ':' ) ) {
list ( $country , $state ) = explode ( ':' , $default );
} else {
2011-12-14 23:50:32 +00:00
$country = $default ;
$state = '' ;
2012-09-07 18:28:27 +00:00
}
2012-09-23 16:16:39 +00:00
$this -> _data [ 'shipping_country' ] = $country ;
$this -> _data [ 'shipping_state' ] = $state ;
$this -> _data [ 'shipping_postcode' ] = '' ;
$this -> _data [ 'shipping_city' ] = '' ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Is customer outside base country ?
*
* @ access public
* @ return bool
*/
2011-09-06 11:11:22 +00:00
function is_customer_outside_base () {
2012-09-07 18:28:27 +00:00
global $woocommerce ;
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'country' ] ) ) {
2012-08-14 19:42:38 +00:00
2011-08-10 17:11:11 +00:00
$default = get_option ( 'woocommerce_default_country' );
2012-09-07 18:28:27 +00:00
if ( strstr ( $default , ':' ) ) {
list ( $country , $state ) = explode ( ':' , $default );
} else {
$country = $default ;
$state = '' ;
}
2012-08-14 19:42:38 +00:00
2012-09-23 16:16:39 +00:00
if ( $country !== $this -> _data [ 'shipping_country' ] ) return true ;
if ( $state && $state !== $this -> _data [ 'shipping_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 ;
}
2012-08-14 19:42:38 +00:00
/**
* Is customer VAT exempt ?
*
* @ access public
* @ return bool
*/
2011-09-14 14:55:03 +00:00
function is_vat_exempt () {
2012-09-23 16:16:39 +00:00
return ( isset ( $this -> _data [ 'is_vat_exempt' ] ) && $this -> _data [ 'is_vat_exempt' ] ) ? true : false ;
2011-09-14 14:55:03 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the state from the current session .
*
* @ access public
* @ return string
*/
2011-09-06 11:11:22 +00:00
function get_state () {
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'state' ] ) ) return $this -> _data [ 'state' ];
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the country from the current session
*
* @ access public
* @ return string
*/
2011-09-06 11:11:22 +00:00
function get_country () {
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'country' ] ) ) return $this -> _data [ 'country' ];
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the postcode from the current session .
*
* @ access public
* @ return string
*/
2011-09-06 11:11:22 +00:00
function get_postcode () {
2012-02-11 10:52:29 +00:00
global $woocommerce ;
$validation = $woocommerce -> validation ();
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'postcode' ]) && $this -> _data [ 'postcode' ] !== false ) return $validation -> format_postcode ( $this -> _data [ 'postcode' ], $this -> get_country ());
}
/**
* Get the city from the current session .
*
* @ access public
* @ return void
*/
function get_city () {
if ( isset ( $this -> _data [ 'city' ] ) ) return $this -> _data [ 'city' ];
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Gets the state from the current session .
*
* @ access public
* @ return string
*/
2011-09-06 11:11:22 +00:00
function get_shipping_state () {
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'shipping_state' ] ) ) return $this -> _data [ '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 .
*
* @ access public
* @ return string
*/
2011-09-06 11:11:22 +00:00
function get_shipping_country () {
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'shipping_country' ] ) ) return $this -> _data [ '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 .
*
* @ access public
* @ return string
*/
2011-09-06 11:11:22 +00:00
function get_shipping_postcode () {
2012-02-11 10:52:29 +00:00
global $woocommerce ;
$validation = $woocommerce -> validation ();
2012-09-23 16:16:39 +00:00
if ( isset ( $this -> _data [ 'shipping_postcode' ])) return $validation -> format_postcode ( $this -> _data [ 'shipping_postcode' ], $this -> get_shipping_country ());
}
/**
* Gets the city from the current session .
*
* @ access public
* @ return void
*/
function get_shipping_city () {
if ( isset ( $this -> _data [ 'shipping_city' ] ) ) return $this -> _data [ 'shipping_city' ];
2011-08-09 15:16:18 +00:00
}
2012-10-01 09:45:07 +00:00
/**
* get_taxable_address function .
*
* @ access public
* @ return void
*/
function get_taxable_address () {
if ( get_option ( 'woocommerce_tax_shipping_address' ) == 'yes' ) {
$country = $this -> get_shipping_country ();
$state = $this -> get_shipping_state ();
$postcode = $this -> get_shipping_postcode ();
$city = $this -> get_shipping_city ();
} else {
$country = $this -> get_country ();
$state = $this -> get_state ();
$postcode = $this -> get_postcode ();
$city = $this -> get_city ();
}
return apply_filters ( 'woocommerce_customer_taxable_address' , array ( $country , $state , $postcode , $city ) );
}
2012-08-14 19:42:38 +00:00
2012-09-23 16:16:39 +00:00
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the location .
2012-09-23 16:16:39 +00:00
*
2012-08-14 19:42:38 +00:00
* @ access public
* @ param mixed $country
* @ param mixed $state
* @ param string $postcode ( default : '' )
2012-09-23 16:16:39 +00:00
* @ param string $city ( default : '' )
2012-08-14 19:42:38 +00:00
* @ return void
*/
2012-09-23 16:16:39 +00:00
function set_location ( $country , $state , $postcode = '' , $city = '' ) {
$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
/**
* Sets session data for the country .
*
* @ access public
* @ param mixed $country
* @ return void
*/
2011-09-06 11:11:22 +00:00
function set_country ( $country ) {
2012-09-23 16:16:39 +00:00
$this -> _data [ 'country' ] = $country ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the state .
*
* @ access public
* @ param mixed $state
* @ return void
*/
2011-09-06 11:11:22 +00:00
function set_state ( $state ) {
2012-09-23 16:16:39 +00:00
$this -> _data [ 'state' ] = $state ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the postcode .
*
* @ access public
* @ param mixed $postcode
* @ return void
*/
2011-09-06 11:11:22 +00:00
function set_postcode ( $postcode ) {
2012-09-23 16:16:39 +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
/**
* Sets session data for the city .
*
* @ access public
* @ param mixed $postcode
* @ return void
*/
function set_city ( $city ) {
$this -> _data [ 'city' ] = $city ;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the location .
*
* @ access public
* @ param mixed $country
* @ param string $state ( default : '' )
* @ param string $postcode ( default : '' )
2012-09-23 16:16:39 +00:00
* @ param string $city ( default : '' )
2012-08-14 19:42:38 +00:00
* @ return void
*/
2012-09-23 16:16:39 +00:00
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 ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the country .
*
* @ access public
* @ param mixed $country
* @ return void
*/
2011-09-06 11:11:22 +00:00
function set_shipping_country ( $country ) {
2012-09-23 16:16:39 +00:00
$this -> _data [ '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 .
*
* @ access public
* @ param mixed $state
* @ return void
*/
2011-09-06 11:11:22 +00:00
function set_shipping_state ( $state ) {
2012-09-23 16:16:39 +00:00
$this -> _data [ '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 .
*
* @ access public
* @ param mixed $postcode
* @ return void
*/
2011-09-06 11:11:22 +00:00
function set_shipping_postcode ( $postcode ) {
2012-09-23 16:16:39 +00:00
$this -> _data [ 'shipping_postcode' ] = $postcode ;
}
/**
* Sets session data for the city .
*
* @ access public
* @ param mixed $postcode
* @ return void
*/
function set_shipping_city ( $city ) {
$this -> _data [ 'shipping_city' ] = $city ;
2011-08-09 15:16:18 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the tax exemption .
*
* @ access public
* @ param mixed $is_vat_exempt
* @ return void
*/
2011-09-14 14:55:03 +00:00
function set_is_vat_exempt ( $is_vat_exempt ) {
2012-09-23 16:16:39 +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
/**
* calculated_shipping function .
*
* @ access public
* @ param mixed $calculated
* @ return void
*/
function calculated_shipping ( $calculated = true ) {
2012-09-23 16:16:39 +00:00
$this -> _data [ 'calculated_shipping' ] = $calculated ;
2012-09-07 18:28:27 +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
* @ access public
* @ return array Array of downloadable products
2011-08-09 15:16:18 +00:00
*/
2011-09-06 11:11:22 +00:00
function get_downloadable_products () {
2012-09-07 18:28:27 +00:00
global $wpdb , $woocommerce ;
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
$downloads = array ();
2012-08-14 19:42:38 +00:00
2012-09-07 18:28:27 +00:00
if ( is_user_logged_in () ) :
2012-08-14 19:42:38 +00:00
2012-09-23 16:16:39 +00:00
$user_info = get_userdata ( get_current_user_id () );
2011-11-09 23:06:17 +00:00
2012-08-28 15:21:54 +00:00
$results = $wpdb -> get_results ( $wpdb -> prepare ( " SELECT * FROM " . $wpdb -> prefix . " woocommerce_downloadable_product_permissions WHERE user_id = '%s' ORDER BY order_id, product_id, download_id " , get_current_user_id ()) );
$_product = null ;
$order = null ;
$file_number = 0 ;
2011-11-16 12:15:41 +00:00
if ( $results ) foreach ( $results as $result ) :
2012-08-14 19:42:38 +00:00
2011-11-17 00:30:46 +00:00
if ( $result -> order_id > 0 ) :
2012-08-14 19:42:38 +00:00
2012-08-28 15:21:54 +00:00
if ( ! $order || $order -> id != $result -> order_id ) {
// new order
$order = new WC_Order ( $result -> order_id );
$_product = null ;
}
2012-08-14 19:42:38 +00:00
2012-08-28 15:21:54 +00:00
// order exists and downloads permitted?
if ( ! $order -> id || ! $order -> is_download_permitted () ) continue ;
2012-08-14 19:42:38 +00:00
2012-08-28 15:21:54 +00:00
if ( ! $_product || $_product -> id != $result -> product_id ) :
// new product
$file_number = 0 ;
2012-01-27 16:38:39 +00:00
$_product = new WC_Product ( $result -> product_id );
2012-08-14 19:42:38 +00:00
endif ;
2012-08-28 15:21:54 +00:00
if ( ! $_product -> exists () ) continue ;
if ( ! $_product -> has_file ( $result -> download_id ) ) continue ;
// Download name will be 'Product Name' for products with a single downloadable file, and 'Product Name - File X' for products with multiple files
$download_name = $_product -> get_title () . ( $file_number > 0 ? ' — ' . sprintf ( __ ( 'File %d' , 'woocommerce' ), $file_number + 1 ) : '' );
if ( $file_number == 1 ) $downloads [ count ( $downloads ) - 1 ][ 'download_name' ] .= ' — ' . sprintf ( __ ( 'File %d' , 'woocommerce' ), $file_number );
2012-08-14 19:42:38 +00:00
2011-11-16 12:15:41 +00:00
$downloads [] = array (
2012-08-28 15:21:54 +00:00
'download_url' => add_query_arg ( array ( 'download_file' => $result -> product_id , 'order' => $result -> order_key , 'email' => $result -> user_email , 'key' => $result -> download_id ), trailingslashit ( home_url () ) ),
'download_id' => $result -> download_id ,
2011-11-16 12:15:41 +00:00
'product_id' => $result -> product_id ,
'download_name' => $download_name ,
'order_id' => $order -> id ,
'order_key' => $order -> order_key ,
'downloads_remaining' => $result -> downloads_remaining
);
2012-08-14 19:42:38 +00:00
2012-08-28 15:21:54 +00:00
$file_number ++ ;
2011-11-16 12:15:41 +00:00
endif ;
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
endforeach ;
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
endif ;
2012-08-14 19:42:38 +00:00
2011-09-30 09:20:51 +00:00
return apply_filters ( 'woocommerce_customer_get_downloadable_products' , $downloads );
2012-08-14 19:42:38 +00:00
}
2011-08-09 15:16:18 +00:00
}