woocommerce/classes/class-wc-customer.php

415 lines
9.9 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
/**
* Customer
2012-08-14 19:42:38 +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-08-14 19:42:38 +00:00
/**
2012-08-15 17:08:42 +00:00
* Constructor for the customer class loads the customer from the PHP session.
2012-08-14 19:42:38 +00:00
*
* @access public
* @return void
*/
function __construct() {
2012-08-14 19:42:38 +00:00
if ( ! isset( $_SESSION['customer'] ) ) {
2012-08-31 17:40:58 +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, ':' ) ) {
$country = current( explode( ':', $default ) );
$state = end( explode( ':', $default ) );
} else {
2011-08-09 15:16:18 +00:00
$country = $default;
$state = '';
2012-08-14 19:42:38 +00:00
}
2011-08-09 15:16:18 +00:00
$data = array(
'country' => $country,
2012-04-11 12:08:04 +00:00
'state' => '',
2011-10-26 10:03:24 +00:00
'postcode' => '',
'shipping_country' => $country,
2012-04-11 12:08:04 +00:00
'shipping_state' => '',
2011-10-26 10:03:24 +00:00
'shipping_postcode' => '',
'is_vat_exempt' => false
2012-08-14 19:42:38 +00:00
);
2011-08-09 15:16:18 +00:00
$_SESSION['customer'] = $data;
2011-10-31 16:27:41 +00:00
$_SESSION['calculated_shipping'] = false;
2012-08-14 19:42:38 +00:00
}
}
2012-08-14 19:42:38 +00:00
/**
* has_calculated_shipping function.
2012-08-14 19:42:38 +00:00
*
* @access public
* @return bool
*/
function has_calculated_shipping() {
2012-08-14 19:42:38 +00:00
return ( ! empty( $_SESSION['calculated_shipping'] ) && $_SESSION['calculated_shipping'] ) ? true : false;
}
2012-08-14 19:42:38 +00:00
/**
* Set customer address to match shop base address.
*
* @access public
* @return void
*/
function set_to_base() {
2012-08-31 17:40:58 +00:00
$default = apply_filters( 'woocommerce_customer_default_location', 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'] = '';
}
2012-08-14 19:42:38 +00:00
/**
* Set customer shipping address to base address.
*
* @access public
* @return void
*/
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
}
2012-08-14 19:42:38 +00:00
/**
* Is customer outside base country?
*
* @access public
* @return bool
*/
function is_customer_outside_base() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['country'])) :
2012-08-14 19:42:38 +00:00
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;
2012-08-14 19:42:38 +00:00
if ($country!==$_SESSION['customer']['shipping_country']) return true;
if ($state && $state!==$_SESSION['customer']['shipping_state']) return true;
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
endif;
return false;
}
2012-08-14 19:42:38 +00:00
/**
* Is customer VAT exempt?
*
* @access public
* @return bool
*/
function is_vat_exempt() {
2012-08-14 19:42:38 +00:00
return ( isset( $_SESSION['customer']['is_vat_exempt'] ) && $_SESSION['customer']['is_vat_exempt'] ) ? true : false;
}
2012-08-14 19:42:38 +00:00
/**
* Gets the state from the current session.
*
* @access public
* @return string
*/
function get_state() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['state'])) return $_SESSION['customer']['state'];
}
2012-08-14 19:42:38 +00:00
/**
* Gets the country from the current session
*
* @access public
* @return string
*/
function get_country() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['country'])) return $_SESSION['customer']['country'];
}
2012-08-14 19:42:38 +00:00
/**
* Gets the postcode from the current session.
*
* @access public
* @return string
*/
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
}
2012-08-14 19:42:38 +00:00
/**
* Gets the state from the current session.
*
* @access public
* @return string
*/
function get_shipping_state() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['shipping_state'])) return $_SESSION['customer']['shipping_state'];
}
2012-08-14 19:42:38 +00:00
/**
* Gets the country from the current session.
*
* @access public
* @return string
*/
function get_shipping_country() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['shipping_country'])) return $_SESSION['customer']['shipping_country'];
}
2012-08-14 19:42:38 +00:00
/**
* Gets the postcode from the current session.
*
* @access public
* @return string
*/
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
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the location.
*
* @access public
* @param mixed $country
* @param mixed $state
* @param string $postcode (default: '')
* @return void
*/
function set_location( $country, $state, $postcode = '' ) {
2011-08-09 15:16:18 +00:00
$data = (array) $_SESSION['customer'];
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
$data['country'] = $country;
$data['state'] = $state;
$data['postcode'] = $postcode;
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
$_SESSION['customer'] = $data;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the country.
*
* @access public
* @param mixed $country
* @return void
*/
function set_country( $country ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['country'] = $country;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the state.
*
* @access public
* @param mixed $state
* @return void
*/
function set_state( $state ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['state'] = $state;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the postcode.
*
* @access public
* @param mixed $postcode
* @return void
*/
function set_postcode( $postcode ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['postcode'] = $postcode;
}
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: '')
* @return void
*/
function set_shipping_location( $country, $state = '', $postcode = '' ) {
2011-08-09 15:16:18 +00:00
$data = (array) $_SESSION['customer'];
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
$data['shipping_country'] = $country;
$data['shipping_state'] = $state;
$data['shipping_postcode'] = $postcode;
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
$_SESSION['customer'] = $data;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the country.
*
* @access public
* @param mixed $country
* @return void
*/
function set_shipping_country( $country ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['shipping_country'] = $country;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the state.
*
* @access public
* @param mixed $state
* @return void
*/
function set_shipping_state( $state ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['shipping_state'] = $state;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the postcode.
*
* @access public
* @param mixed $postcode
* @return void
*/
function set_shipping_postcode( $postcode ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['shipping_postcode'] = $postcode;
}
2012-08-14 19:42:38 +00:00
/**
* Sets session data for the tax exemption.
*
* @access public
* @param mixed $is_vat_exempt
* @return void
*/
function set_is_vat_exempt( $is_vat_exempt ) {
$_SESSION['customer']['is_vat_exempt'] = $is_vat_exempt;
}
2012-08-14 19:42:38 +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
*/
function get_downloadable_products() {
2012-08-14 19:42:38 +00:00
2011-08-09 15:16:18 +00:00
global $wpdb;
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
2011-08-09 15:16:18 +00:00
if (is_user_logged_in()) :
2012-08-14 19:42:38 +00:00
$user_info = get_userdata(get_current_user_id());
$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;
if ($results) foreach ($results as $result) :
2012-08-14 19:42:38 +00:00
if ($result->order_id>0) :
2012-08-14 19:42:38 +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
// order exists and downloads permitted?
if ( ! $order->id || ! $order->is_download_permitted() ) continue;
2012-08-14 19:42:38 +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;
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 ? ' &mdash; ' . sprintf( __( 'File %d', 'woocommerce' ), $file_number + 1 ) : '' );
if ( $file_number == 1 ) $downloads[ count( $downloads ) - 1 ]['download_name'] .= ' &mdash; ' . sprintf( __( 'File %d', 'woocommerce' ), $file_number );
2012-08-14 19:42:38 +00:00
$downloads[] = array(
'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,
'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
$file_number++;
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
return apply_filters('woocommerce_customer_get_downloadable_products', $downloads);
2012-08-14 19:42:38 +00:00
}
2012-01-27 16:38:39 +00:00
}
2012-08-14 19:42:38 +00:00
/**
* woocommerce_customer class.
*
2012-08-15 17:08:42 +00:00
* @extends WC_Customer
* @deprecated 1.4
* @package WooCommerce/Classes
2012-08-14 19:42:38 +00:00
*/
class woocommerce_customer extends WC_Customer {
2012-08-14 19:42:38 +00:00
public function __construct() {
2012-01-27 16:38:39 +00:00
_deprecated_function( 'woocommerce_customer', '1.4', 'WC_Customer()' );
2012-08-14 19:42:38 +00:00
parent::__construct();
}
2011-08-09 15:16:18 +00:00
}