2011-08-09 15:16:18 +00:00
< ? php
/**
* Customer
*
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
2011-08-10 17:11:11 +00:00
* @ package WooCommerce
* @ category Class
* @ author WooThemes
2011-08-09 15:16:18 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Customer {
2011-08-09 15:16:18 +00:00
/** constructor */
2011-09-06 11:11:22 +00:00
function __construct () {
2011-08-09 15:16:18 +00:00
if ( ! isset ( $_SESSION [ 'customer' ]) ) :
2011-08-10 17:11:11 +00:00
$default = get_option ( 'woocommerce_default_country' );
2011-08-09 15:16:18 +00:00
if ( strstr ( $default , ':' )) :
$country = current ( explode ( ':' , $default ));
$state = end ( explode ( ':' , $default ));
else :
$country = $default ;
$state = '' ;
endif ;
$data = array (
2011-09-14 14:55:03 +00:00
'country' => $country ,
2012-04-11 12:08:04 +00:00
'state' => '' ,
2011-10-26 10:03:24 +00:00
'postcode' => '' ,
2011-09-14 14:55:03 +00:00
'shipping_country' => $country ,
2012-04-11 12:08:04 +00:00
'shipping_state' => '' ,
2011-10-26 10:03:24 +00:00
'shipping_postcode' => '' ,
2011-09-14 14:55:03 +00:00
'is_vat_exempt' => false
2011-08-09 15:16:18 +00:00
);
$_SESSION [ 'customer' ] = $data ;
2011-10-31 16:27:41 +00:00
$_SESSION [ 'calculated_shipping' ] = false ;
2011-08-09 15:16:18 +00:00
endif ;
2011-12-14 23:50:32 +00:00
}
/** set location to base location */
function set_to_base () {
$default = get_option ( 'woocommerce_default_country' );
if ( strstr ( $default , ':' )) :
$country = current ( explode ( ':' , $default ));
$state = end ( explode ( ':' , $default ));
else :
$country = $default ;
$state = '' ;
endif ;
$_SESSION [ 'customer' ][ 'country' ] = $country ;
$_SESSION [ 'customer' ][ 'state' ] = $state ;
$_SESSION [ 'customer' ][ 'postcode' ] = '' ;
}
/** set shipping location to base location */
function set_shipping_to_base () {
$default = get_option ( 'woocommerce_default_country' );
if ( strstr ( $default , ':' )) :
$country = current ( explode ( ':' , $default ));
$state = end ( explode ( ':' , $default ));
else :
$country = $default ;
$state = '' ;
endif ;
$_SESSION [ 'customer' ][ 'shipping_country' ] = $country ;
$_SESSION [ 'customer' ][ 'shipping_state' ] = $state ;
$_SESSION [ 'customer' ][ 'shipping_postcode' ] = '' ;
2011-08-09 15:16:18 +00:00
}
/** Is customer outside base country? */
2011-09-06 11:11:22 +00:00
function is_customer_outside_base () {
2011-08-09 15:16:18 +00:00
if ( isset ( $_SESSION [ 'customer' ][ 'country' ])) :
2011-08-10 17:11:11 +00:00
$default = get_option ( 'woocommerce_default_country' );
2011-08-09 15:16:18 +00:00
if ( strstr ( $default , ':' )) :
$country = current ( explode ( ':' , $default ));
$state = end ( explode ( ':' , $default ));
else :
$country = $default ;
$state = '' ;
endif ;
2011-12-06 10:49:26 +00:00
if ( $country !== $_SESSION [ 'customer' ][ 'shipping_country' ]) return true ;
if ( $state && $state !== $_SESSION [ 'customer' ][ 'shipping_state' ]) return true ;
2011-08-09 15:16:18 +00:00
endif ;
return false ;
}
2011-09-14 14:55:03 +00:00
/** Is customer VAT exempt? */
function is_vat_exempt () {
if ( isset ( $_SESSION [ 'customer' ][ 'is_vat_exempt' ]) && $_SESSION [ 'customer' ][ 'is_vat_exempt' ]) return true ;
return false ;
}
2011-08-09 15:16:18 +00:00
/** Gets the state from the current session */
2011-09-06 11:11:22 +00:00
function get_state () {
2011-08-09 15:16:18 +00:00
if ( isset ( $_SESSION [ 'customer' ][ 'state' ])) return $_SESSION [ 'customer' ][ 'state' ];
}
/** Gets the country from the current session */
2011-09-06 11:11:22 +00:00
function get_country () {
2011-08-09 15:16:18 +00:00
if ( isset ( $_SESSION [ 'customer' ][ 'country' ])) return $_SESSION [ 'customer' ][ 'country' ];
}
/** Gets the postcode from the current session */
2011-09-06 11:11:22 +00:00
function get_postcode () {
2012-02-11 10:52:29 +00:00
global $woocommerce ;
$validation = $woocommerce -> validation ();
if ( isset ( $_SESSION [ 'customer' ][ 'postcode' ]) && $_SESSION [ 'customer' ][ 'postcode' ] !== false ) return $validation -> format_postcode ( $_SESSION [ 'customer' ][ 'postcode' ], $this -> get_country ());
2011-08-09 15:16:18 +00:00
}
/** Gets the state from the current session */
2011-09-06 11:11:22 +00:00
function get_shipping_state () {
2011-08-09 15:16:18 +00:00
if ( isset ( $_SESSION [ 'customer' ][ 'shipping_state' ])) return $_SESSION [ 'customer' ][ 'shipping_state' ];
}
/** Gets the country from the current session */
2011-09-06 11:11:22 +00:00
function get_shipping_country () {
2011-08-09 15:16:18 +00:00
if ( isset ( $_SESSION [ 'customer' ][ 'shipping_country' ])) return $_SESSION [ 'customer' ][ 'shipping_country' ];
}
/** Gets the postcode from the current session */
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 ();
if ( isset ( $_SESSION [ 'customer' ][ 'shipping_postcode' ])) return $validation -> format_postcode ( $_SESSION [ 'customer' ][ 'shipping_postcode' ], $this -> get_shipping_country ());
2011-08-09 15:16:18 +00:00
}
/** Sets session data for the location */
2011-09-06 11:11:22 +00:00
function set_location ( $country , $state , $postcode = '' ) {
2011-08-09 15:16:18 +00:00
$data = ( array ) $_SESSION [ 'customer' ];
$data [ 'country' ] = $country ;
$data [ 'state' ] = $state ;
$data [ 'postcode' ] = $postcode ;
$_SESSION [ 'customer' ] = $data ;
}
/** Sets session data for the country */
2011-09-06 11:11:22 +00:00
function set_country ( $country ) {
2011-08-09 15:16:18 +00:00
$_SESSION [ 'customer' ][ 'country' ] = $country ;
}
/** Sets session data for the state */
2011-09-06 11:11:22 +00:00
function set_state ( $state ) {
2011-08-09 15:16:18 +00:00
$_SESSION [ 'customer' ][ 'state' ] = $state ;
}
/** Sets session data for the postcode */
2011-09-06 11:11:22 +00:00
function set_postcode ( $postcode ) {
2011-08-09 15:16:18 +00:00
$_SESSION [ 'customer' ][ 'postcode' ] = $postcode ;
}
/** Sets session data for the location */
2011-09-06 11:11:22 +00:00
function set_shipping_location ( $country , $state = '' , $postcode = '' ) {
2011-08-09 15:16:18 +00:00
$data = ( array ) $_SESSION [ 'customer' ];
$data [ 'shipping_country' ] = $country ;
$data [ 'shipping_state' ] = $state ;
$data [ 'shipping_postcode' ] = $postcode ;
$_SESSION [ 'customer' ] = $data ;
}
/** Sets session data for the country */
2011-09-06 11:11:22 +00:00
function set_shipping_country ( $country ) {
2011-08-09 15:16:18 +00:00
$_SESSION [ 'customer' ][ 'shipping_country' ] = $country ;
}
/** Sets session data for the state */
2011-09-06 11:11:22 +00:00
function set_shipping_state ( $state ) {
2011-08-09 15:16:18 +00:00
$_SESSION [ 'customer' ][ 'shipping_state' ] = $state ;
}
/** Sets session data for the postcode */
2011-09-06 11:11:22 +00:00
function set_shipping_postcode ( $postcode ) {
2011-08-09 15:16:18 +00:00
$_SESSION [ 'customer' ][ 'shipping_postcode' ] = $postcode ;
}
2011-09-14 14:55:03 +00:00
/** Sets session data for the tax exemption */
function set_is_vat_exempt ( $is_vat_exempt ) {
$_SESSION [ 'customer' ][ 'is_vat_exempt' ] = $is_vat_exempt ;
}
2011-08-09 15:16:18 +00:00
/**
* Gets a user ' s downloadable products if they are logged in
*
* @ return array downloads Array of downloadable products
*/
2011-09-06 11:11:22 +00:00
function get_downloadable_products () {
2011-08-09 15:16:18 +00:00
global $wpdb ;
$downloads = array ();
if ( is_user_logged_in ()) :
2011-11-16 12:15:41 +00:00
$user_info = get_userdata ( get_current_user_id ());
2011-11-09 23:06:17 +00:00
2011-11-16 12:15:41 +00:00
$results = $wpdb -> get_results ( $wpdb -> prepare ( " SELECT * FROM " . $wpdb -> prefix . " woocommerce_downloadable_product_permissions WHERE user_id = '%s'; " , get_current_user_id ()) );
if ( $results ) foreach ( $results as $result ) :
2011-11-17 00:30:46 +00:00
if ( $result -> order_id > 0 ) :
2011-11-16 12:15:41 +00:00
2012-01-27 16:38:39 +00:00
$order = new WC_Order ( $result -> order_id );
2011-11-16 12:15:41 +00:00
2011-11-16 19:34:38 +00:00
if ( $order -> status != 'completed' && $order -> status != 'processing' ) continue ;
2012-03-26 17:30:00 +00:00
2011-11-16 12:15:41 +00:00
$product_post = get_post ( $result -> product_id );
2012-03-26 17:30:00 +00:00
if ( ! $product_post ) continue ;
2011-11-16 12:15:41 +00:00
2012-03-26 17:30:00 +00:00
if ( $product_post -> post_type == 'product_variation' ) :
2012-01-27 16:38:39 +00:00
$_product = new WC_Product_Variation ( $result -> product_id );
2011-11-16 12:15:41 +00:00
else :
2012-01-27 16:38:39 +00:00
$_product = new WC_Product ( $result -> product_id );
2011-11-16 12:15:41 +00:00
endif ;
if ( $_product -> exists ) :
$download_name = $_product -> get_title ();
else :
$download_name = '#' . $result -> product_id ;
endif ;
$downloads [] = array (
2012-03-12 13:28:34 +00:00
'download_url' => add_query_arg ( 'download_file' , $result -> product_id , add_query_arg ( 'order' , $result -> order_key , add_query_arg ( 'email' , $result -> user_email , home_url ()))),
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
);
endif ;
2011-08-09 15:16:18 +00:00
endforeach ;
endif ;
2011-09-30 09:20:51 +00:00
return apply_filters ( 'woocommerce_customer_get_downloadable_products' , $downloads );
2012-03-04 12:37:41 +00:00
}
2012-01-27 16:38:39 +00:00
}
2011-12-08 19:45:24 +00:00
2012-01-27 16:38:39 +00:00
/** Depreciated */
2012-03-14 20:35:42 +00:00
class woocommerce_customer extends WC_Customer {
2012-01-27 16:38:39 +00:00
public function __construct () {
_deprecated_function ( 'woocommerce_customer' , '1.4' , 'WC_Customer()' );
parent :: __construct ();
}
2011-08-09 15:16:18 +00:00
}