2016-11-14 18:18:08 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC Customer Data Store which stores the data in session.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2016-11-14 18:18:08 +00:00
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2016-11-22 13:54:51 +00:00
|
|
|
class WC_Customer_Data_Store_Session extends WC_Data_Store_WP implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store_Interface {
|
2016-11-14 18:18:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keys which are also stored in a session (so we can make sure they get updated...)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $session_keys = array(
|
2017-05-30 16:03:55 +00:00
|
|
|
'id',
|
2016-11-14 18:18:08 +00:00
|
|
|
'billing_postcode',
|
|
|
|
'billing_city',
|
|
|
|
'billing_address_1',
|
|
|
|
'billing_address',
|
|
|
|
'billing_address_2',
|
|
|
|
'billing_state',
|
|
|
|
'billing_country',
|
|
|
|
'shipping_postcode',
|
|
|
|
'shipping_city',
|
|
|
|
'shipping_address_1',
|
|
|
|
'shipping_address',
|
|
|
|
'shipping_address_2',
|
|
|
|
'shipping_state',
|
|
|
|
'shipping_country',
|
|
|
|
'is_vat_exempt',
|
|
|
|
'calculated_shipping',
|
|
|
|
'billing_first_name',
|
|
|
|
'billing_last_name',
|
|
|
|
'billing_company',
|
|
|
|
'billing_phone',
|
|
|
|
'billing_email',
|
|
|
|
'shipping_first_name',
|
|
|
|
'shipping_last_name',
|
|
|
|
'shipping_company',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simply update the session.
|
|
|
|
*
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function create( &$customer ) {
|
|
|
|
$this->save_to_session( $customer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simply update the session.
|
|
|
|
*
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function update( &$customer ) {
|
|
|
|
$this->save_to_session( $customer );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves all customer data to the session.
|
|
|
|
*
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function save_to_session( $customer ) {
|
|
|
|
$data = array();
|
|
|
|
foreach ( $this->session_keys as $session_key ) {
|
|
|
|
$function_key = $session_key;
|
|
|
|
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
|
|
|
|
$session_key = str_replace( 'billing_', '', $session_key );
|
|
|
|
}
|
|
|
|
$data[ $session_key ] = $customer->{"get_$function_key"}( 'edit' );
|
|
|
|
}
|
|
|
|
if ( WC()->session->get( 'customer' ) !== $data ) {
|
|
|
|
WC()->session->set( 'customer', $data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-30 16:03:55 +00:00
|
|
|
* Read customer data from the session unless the user has logged in, in
|
|
|
|
* which case the stored ID will differ from the actual ID.
|
2016-11-14 18:18:08 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function read( &$customer ) {
|
|
|
|
$data = (array) WC()->session->get( 'customer' );
|
2017-05-30 16:03:55 +00:00
|
|
|
if ( ! empty( $data ) && isset( $data['id'] ) && $data['id'] === $customer->get_id() ) {
|
2016-11-14 18:18:08 +00:00
|
|
|
foreach ( $this->session_keys as $session_key ) {
|
|
|
|
$function_key = $session_key;
|
|
|
|
if ( 'billing_' === substr( $session_key, 0, 8 ) ) {
|
|
|
|
$session_key = str_replace( 'billing_', '', $session_key );
|
|
|
|
}
|
|
|
|
if ( ! empty( $data[ $session_key ] ) && is_callable( array( $customer, "set_{$function_key}" ) ) ) {
|
2017-11-06 15:48:11 +00:00
|
|
|
$customer->{"set_{$function_key}"}( wp_unslash( $data[ $session_key ] ) );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->set_defaults( $customer );
|
|
|
|
$customer->set_object_read( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load default values if props are unset.
|
|
|
|
*
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
protected function set_defaults( &$customer ) {
|
2017-04-05 15:36:28 +00:00
|
|
|
try {
|
|
|
|
$default = wc_get_customer_default_location();
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2017-04-05 15:36:28 +00:00
|
|
|
if ( ! $customer->get_billing_country() ) {
|
|
|
|
$customer->set_billing_country( $default['country'] );
|
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2017-04-05 15:36:28 +00:00
|
|
|
if ( ! $customer->get_shipping_country() ) {
|
|
|
|
$customer->set_shipping_country( $customer->get_billing_country() );
|
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2017-04-05 15:36:28 +00:00
|
|
|
if ( ! $customer->get_billing_state() ) {
|
|
|
|
$customer->set_billing_state( $default['state'] );
|
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2017-04-05 15:36:28 +00:00
|
|
|
if ( ! $customer->get_shipping_state() ) {
|
|
|
|
$customer->set_shipping_state( $customer->get_billing_state() );
|
|
|
|
}
|
2016-11-25 21:46:34 +00:00
|
|
|
|
2017-04-05 15:36:28 +00:00
|
|
|
if ( ! $customer->get_billing_email() && is_user_logged_in() ) {
|
|
|
|
$current_user = wp_get_current_user();
|
|
|
|
$customer->set_billing_email( $current_user->user_email );
|
|
|
|
}
|
|
|
|
} catch ( WC_Data_Exception $e ) {}
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a customer from the database.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param WC_Customer $customer
|
2016-11-15 18:11:25 +00:00
|
|
|
* @param array $args Array of args to pass to the delete method.
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
2016-11-15 18:11:25 +00:00
|
|
|
public function delete( &$customer, $args = array() ) {
|
2016-11-14 18:18:08 +00:00
|
|
|
WC()->session->set( 'customer', null );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the customers last order.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-14 18:18:08 +00:00
|
|
|
* @param WC_Customer
|
|
|
|
* @return WC_Order|false
|
|
|
|
*/
|
|
|
|
public function get_last_order( &$customer ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of orders this customer has.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-14 18:18:08 +00:00
|
|
|
* @param WC_Customer
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function get_order_count( &$customer ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return how much money this customer has spent.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-14 18:18:08 +00:00
|
|
|
* @param WC_Customer
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_total_spent( &$customer ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|