woocommerce/classes/customer.class.php

197 lines
5.8 KiB
PHP
Raw Normal View History

2011-08-09 15:16:18 +00:00
<?php
/**
* Customer
*
2011-08-10 17:11:11 +00:00
* The WooCommerce custoemr class handles storage of the current customer's data, such as location.
2011-08-09 15:16:18 +00:00
*
2011-08-10 17:11:11 +00:00
* @class woocommerce_customer
* @package WooCommerce
* @category Class
* @author WooThemes
2011-08-09 15:16:18 +00:00
*/
2011-08-10 17:11:11 +00:00
class woocommerce_customer {
2011-08-09 15:16:18 +00:00
/** constructor */
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(
'country' => $country,
'state' => $state,
'postcode' => '',
'shipping_country' => $country,
'shipping_state' => $state,
'shipping_postcode' => '',
'is_vat_exempt' => false
2011-08-09 15:16:18 +00:00
);
$_SESSION['customer'] = $data;
endif;
}
/** Is customer outside base country? */
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;
if ($country!==$_SESSION['customer']['country']) return true;
endif;
return false;
}
/** 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 */
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 */
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 */
function get_postcode() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['postcode'])) return strtolower(str_replace(' ', '', $_SESSION['customer']['postcode']));
}
/** Gets the state from the current session */
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 */
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 */
function get_shipping_postcode() {
2011-08-09 15:16:18 +00:00
if (isset($_SESSION['customer']['shipping_postcode'])) return strtolower(str_replace(' ', '', $_SESSION['customer']['shipping_postcode']));
}
/** Sets session data for the location */
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 */
function set_country( $country ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['country'] = $country;
}
/** Sets session data for the state */
function set_state( $state ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['state'] = $state;
}
/** Sets session data for the postcode */
function set_postcode( $postcode ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['postcode'] = $postcode;
}
/** Sets session data for the location */
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 */
function set_shipping_country( $country ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['shipping_country'] = $country;
}
/** Sets session data for the state */
function set_shipping_state( $state ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['shipping_state'] = $state;
}
/** Sets session data for the postcode */
function set_shipping_postcode( $postcode ) {
2011-08-09 15:16:18 +00:00
$_SESSION['customer']['shipping_postcode'] = $postcode;
}
/** 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
*/
function get_downloadable_products() {
2011-08-09 15:16:18 +00:00
global $wpdb;
$downloads = array();
if (is_user_logged_in()) :
2011-08-10 17:11:11 +00:00
$woocommerce_orders = &new woocommerce_orders();
$woocommerce_orders->get_customer_orders( get_current_user_id() );
if ($woocommerce_orders->orders) foreach ($woocommerce_orders->orders as $order) :
2011-08-09 15:16:18 +00:00
if ( $order->status == 'completed' ) {
2011-08-10 17:11:11 +00:00
$results = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."woocommerce_downloadable_product_permissions WHERE order_key = \"".$order->order_key."\" AND user_id = ".get_current_user_id().";" );
2011-08-09 15:16:18 +00:00
$user_info = get_userdata(get_current_user_id());
if ($results) foreach ($results as $result) :
2011-08-10 17:11:11 +00:00
$_product = &new woocommerce_product( $result->product_id );
2011-08-09 15:16:18 +00:00
if ($_product->exists) :
$download_name = $_product->get_title();
else :
$download_name = '#' . $result->product_id;
endif;
$downloads[] = array(
'download_url' => add_query_arg('download_file', $result->product_id, add_query_arg('order', $result->order_key, add_query_arg('email', $user_info->user_email, home_url()))),
'product_id' => $result->product_id,
'download_name' => $download_name,
'order_key' => $result->order_key,
'downloads_remaining' => $result->downloads_remaining
);
endforeach;
}
endforeach;
endif;
return $downloads;
}
}