Data stores
This commit is contained in:
parent
c1677867fe
commit
00ed627b29
|
@ -110,12 +110,18 @@ abstract class WC_Data {
|
|||
}
|
||||
|
||||
/**
|
||||
* Updates object data in the database.
|
||||
* Delete an object, set the ID to 0, and return result.
|
||||
*
|
||||
* @param bool $force_delete
|
||||
* @return bool result
|
||||
*/
|
||||
public function delete( $force_delete = false ) {
|
||||
if ( $this->data_store ) {
|
||||
$this->data_store->delete( $this, $force_delete );
|
||||
$this->set_id( 0 );
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -311,7 +311,7 @@ class WC_REST_Customers_Controller extends WC_REST_Controller {
|
|||
$customer->set_username( $request['username'] );
|
||||
$customer->set_password( $request['password'] );
|
||||
$customer->set_email( $request['email'] );
|
||||
$customer->create();
|
||||
$customer->save();
|
||||
|
||||
if ( ! $customer->get_id() ) {
|
||||
throw new WC_REST_Exception( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), 400 );
|
||||
|
|
|
@ -55,8 +55,7 @@ class WC_Cache_Helper {
|
|||
* @return string
|
||||
*/
|
||||
public static function geolocation_ajax_get_location_hash() {
|
||||
$customer = new WC_Customer();
|
||||
$customer->load_session();
|
||||
$customer = new WC_Customer( 0, true );
|
||||
$location = array();
|
||||
$location['country'] = $customer->get_billing_country();
|
||||
$location['state'] = $customer->get_billing_state();
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -28,7 +28,9 @@ class WC_Data_Store {
|
|||
* Ran through `woocommerce_data_stores`.
|
||||
*/
|
||||
private $stores = array(
|
||||
'coupon' => 'WC_Coupon_Data_Store_CPT',
|
||||
'coupon' => 'WC_Coupon_Data_Store_CPT',
|
||||
'customer' => 'WC_Customer_Data_Store',
|
||||
'customer-session' => 'WC_Customer_Data_Store_Session',
|
||||
);
|
||||
/**
|
||||
* Contains the name of the current data store's class name.
|
||||
|
|
|
@ -0,0 +1,175 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC Customer Data Store which stores the data in session.
|
||||
*
|
||||
* @version 2.7.0
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
*/
|
||||
class WC_Customer_Data_Store_Session implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store {
|
||||
|
||||
/**
|
||||
* Keys which are also stored in a session (so we can make sure they get updated...)
|
||||
* @var array
|
||||
*/
|
||||
protected $session_keys = array(
|
||||
'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.
|
||||
*
|
||||
* @param WC_Customer
|
||||
*/
|
||||
public function create( &$customer ) {
|
||||
$this->save_to_session( $customer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Simply update the session.
|
||||
*
|
||||
* @param WC_Customer
|
||||
*/
|
||||
public function update( &$customer ) {
|
||||
$this->save_to_session( $customer );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves all customer data to the session.
|
||||
*
|
||||
* @param WC_Customer
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read customer data from the session.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
*/
|
||||
public function read( &$customer ) {
|
||||
$data = (array) WC()->session->get( 'customer' );
|
||||
if ( ! empty( $data ) ) {
|
||||
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}" ) ) ) {
|
||||
$customer->{"set_{$function_key}"}( $data[ $session_key ] );
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->set_defaults( $customer );
|
||||
$customer->set_object_read( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load default values if props are unset.
|
||||
*
|
||||
* @param WC_Customer
|
||||
*/
|
||||
protected function set_defaults( &$customer ) {
|
||||
$default = wc_get_customer_default_location();
|
||||
|
||||
// Set some defaults if some of our values are still not set.
|
||||
if ( ! $customer->get_billing_country() ) {
|
||||
$customer->set_billing_country( $default['country'] );
|
||||
}
|
||||
|
||||
if ( ! $customer->get_shipping_country() ) {
|
||||
$customer->set_shipping_country( $customer->get_billing_country() );
|
||||
}
|
||||
|
||||
if ( ! $customer->get_billing_state() ) {
|
||||
$customer->set_billing_state( $default['state'] );
|
||||
}
|
||||
|
||||
if ( ! $customer->get_shipping_state() ) {
|
||||
$customer->set_shipping_state( $customer->get_billing_state() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a customer from the database.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @param int|null $reassign Who to reassign posts to.
|
||||
*/
|
||||
public function delete( &$customer, $force_delete = true, $reassign = null ) {
|
||||
WC()->session->set( 'customer', null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the customers last order.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @return WC_Order|false
|
||||
*/
|
||||
public function get_last_order( &$customer ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of orders this customer has.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @return integer
|
||||
*/
|
||||
public function get_order_count( &$customer ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return how much money this customer has spent.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @return float
|
||||
*/
|
||||
public function get_total_spent( &$customer ) {
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,242 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC Customer Data Store.
|
||||
*
|
||||
* @version 2.7.0
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
*/
|
||||
class WC_Customer_Data_Store implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store {
|
||||
|
||||
/**
|
||||
* Method to create a new customer in the database.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
*/
|
||||
public function create( &$customer ) {
|
||||
$customer_id = wc_create_new_customer( $customer->get_email(), $customer->get_username(), $customer->get_password() );
|
||||
|
||||
if ( ! is_wp_error( $customer_id ) ) {
|
||||
$customer->set_id( $customer_id );
|
||||
$this->update_user_meta( $customer );
|
||||
wp_update_user( array( 'ID' => $customer->get_id(), 'role' => $customer->get_role() ) );
|
||||
$wp_user = new WP_User( $customer->get_id() );
|
||||
$customer->set_date_created( strtotime( $wp_user->user_registered ) );
|
||||
$customer->set_date_modified( get_user_meta( $customer->get_id(), 'last_update', true ) );
|
||||
$customer->read_meta_data();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to read a customer object.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
*/
|
||||
public function read( &$customer ) {
|
||||
global $wpdb;
|
||||
|
||||
// User object is required.
|
||||
if ( ! $customer->get_id() || ! ( $user_object = get_user_by( 'id', $customer->get_id() ) ) || empty( $user_object->ID ) ) {
|
||||
throw new Exception( __( 'Invalid customer.', 'woocommerce' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
// Only users on this site should be read.
|
||||
if ( is_multisite() && ! is_user_member_of_blog( $customer->get_id() ) ) {
|
||||
throw new Exception( __( 'Invalid customer.', 'woocommerce' ) );
|
||||
return;
|
||||
}
|
||||
|
||||
$customer_id = $customer->get_id();
|
||||
$customer->set_props( array_map( 'wc_flatten_meta_callback', get_user_meta( $customer_id ) ) );
|
||||
$customer->set_props( array(
|
||||
'is_paying_customer' => get_user_meta( $customer_id, 'paying_customer', true ),
|
||||
'email' => $user_object->user_email,
|
||||
'username' => $user_object->user_login,
|
||||
'date_created' => strtotime( $user_object->user_registered ),
|
||||
'date_modified' => get_user_meta( $customer_id, 'last_update', true ),
|
||||
'role' => ! empty( $user_object->roles[0] ) ? $user_object->roles[0] : 'customer',
|
||||
) );
|
||||
$customer->read_meta_data();
|
||||
$customer->set_object_read( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a customer in the database.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
*/
|
||||
public function update( &$customer ) {
|
||||
wp_update_user( array( 'ID' => $customer->get_id(), 'user_email' => $customer->get_email() ) );
|
||||
// Only update password if a new one was set with set_password
|
||||
if ( ! empty( $customer->get_password() ) ) {
|
||||
wp_update_user( array( 'ID' => $customer->get_id(), 'user_pass' => $customer->get_password() ) );
|
||||
$customer->set_password( '' );
|
||||
}
|
||||
$this->update_user_meta( $customer );
|
||||
$customer->set_date_modified( get_user_meta( $customer->get_id(), 'last_update', true ) );
|
||||
$customer->save_meta_data();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a customer from the database.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @param bool $force_delete Unused for customers.
|
||||
* @param int|null $reassign Who to reassign posts to.
|
||||
*/
|
||||
public function delete( &$customer, $force_delete = true, $reassign = null ) {
|
||||
if ( ! $customer->get_id() ) {
|
||||
return;
|
||||
}
|
||||
return wp_delete_user( $customer->get_id(), $reassign );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method that updates all the meta for a customer. Used for update & create.
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
*/
|
||||
private function update_user_meta( $customer ) {
|
||||
$updated_props = array();
|
||||
$changed_props = array_keys( $customer->get_changes() );
|
||||
|
||||
$meta_key_to_props = array(
|
||||
'paying_customer' => 'is_paying_customer',
|
||||
'first_name' => 'first_name',
|
||||
'last_name' => 'last_name',
|
||||
);
|
||||
|
||||
foreach ( $meta_key_to_props as $meta_key => $prop ) {
|
||||
if ( ! in_array( $prop, $changed_props ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( update_user_meta( $customer->get_id(), $meta_key, $customer->{"get_$prop"}( 'edit' ) ) ) {
|
||||
$updated_props[] = $prop;
|
||||
}
|
||||
}
|
||||
|
||||
if ( in_array( 'billing', $changed_props ) ) {
|
||||
update_user_meta( $customer->get_id(), 'billing_first_name', $customer->get_billing_first_name( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_last_name', $customer->get_billing_last_name( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_company', $customer->get_billing_company( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_phone', $customer->get_billing_phone( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_email', $customer->get_billing_email( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_postcode', $customer->get_billing_postcode( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_city', $customer->get_billing_city( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_address_1', $customer->get_billing_address( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_address_2', $customer->get_billing_address_2( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_state', $customer->get_billing_state( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'billing_country', $customer->get_billing_country( 'edit' ) );
|
||||
}
|
||||
|
||||
if ( in_array( 'shipping', $changed_props ) ) {
|
||||
update_user_meta( $customer->get_id(), 'shipping_first_name', $customer->get_shipping_first_name( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_last_name', $customer->get_shipping_last_name( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_company', $customer->get_shipping_company( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_postcode', $customer->get_shipping_postcode( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_city', $customer->get_shipping_city( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_address_1', $customer->get_shipping_address( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_address_2', $customer->get_shipping_address_2( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_state', $customer->get_shipping_state( 'edit' ) );
|
||||
update_user_meta( $customer->get_id(), 'shipping_country', $customer->get_shipping_country( 'edit' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the customers last order.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @return WC_Order|false
|
||||
*/
|
||||
public function get_last_order( &$customer ) {
|
||||
global $wpdb;
|
||||
|
||||
$last_order = $wpdb->get_var( "SELECT posts.ID
|
||||
FROM $wpdb->posts AS posts
|
||||
LEFT JOIN {$wpdb->postmeta} AS meta on posts.ID = meta.post_id
|
||||
WHERE meta.meta_key = '_customer_user'
|
||||
AND meta.meta_value = '" . esc_sql( $customer->get_id() ) . "'
|
||||
AND posts.post_type = 'shop_order'
|
||||
AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
|
||||
ORDER BY posts.ID DESC
|
||||
" );
|
||||
|
||||
if ( $last_order ) {
|
||||
return wc_get_order( absint( $last_order ) );
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of orders this customer has.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @return integer
|
||||
*/
|
||||
public function get_order_count( &$customer ) {
|
||||
$count = get_user_meta( $customer->get_id(), '_order_count', true );
|
||||
|
||||
if ( '' === $count ) {
|
||||
global $wpdb;
|
||||
|
||||
$count = $wpdb->get_var( "SELECT COUNT(*)
|
||||
FROM $wpdb->posts as posts
|
||||
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
|
||||
WHERE meta.meta_key = '_customer_user'
|
||||
AND posts.post_type = 'shop_order'
|
||||
AND posts.post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
|
||||
AND meta_value = '" . esc_sql( $customer->get_id() ) . "'
|
||||
" );
|
||||
update_user_meta( $customer->get_id(), '_order_count', $count );
|
||||
}
|
||||
|
||||
return absint( $count );
|
||||
}
|
||||
|
||||
/**
|
||||
* Return how much money this customer has spent.
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param WC_Customer
|
||||
* @return float
|
||||
*/
|
||||
public function get_total_spent( &$customer ) {
|
||||
$spent = get_user_meta( $customer->get_id(), '_money_spent', true );
|
||||
|
||||
if ( '' === $spent ) {
|
||||
global $wpdb;
|
||||
|
||||
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
|
||||
$spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
|
||||
FROM $wpdb->posts as posts
|
||||
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
|
||||
LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
|
||||
WHERE meta.meta_key = '_customer_user'
|
||||
AND meta.meta_value = '" . esc_sql( $customer->get_id() ) . "'
|
||||
AND posts.post_type = 'shop_order'
|
||||
AND posts.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
|
||||
AND meta2.meta_key = '_order_total'
|
||||
" );
|
||||
|
||||
if ( ! $spent ) {
|
||||
$spent = 0;
|
||||
}
|
||||
update_user_meta( $customer->get_id(), '_money_spent', $spent );
|
||||
}
|
||||
|
||||
return wc_format_decimal( $spent, 2 );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* WC Customer Data Store Interface
|
||||
*
|
||||
* Functions that must be defined by customer store classes.
|
||||
*
|
||||
* @version 2.7.0
|
||||
* @category Interface
|
||||
* @author WooThemes
|
||||
*/
|
||||
interface WC_Customer_Data_Store_Interface {
|
||||
|
||||
/**
|
||||
* Deletes a customer from the database.
|
||||
*
|
||||
* @param WC_Customer
|
||||
* @param int|null $reassign Who to reassign posts to.
|
||||
*/
|
||||
public function delete( &$data, $reassign = null );
|
||||
|
||||
/**
|
||||
* Gets the customers last order.
|
||||
*
|
||||
* @param WC_Customer
|
||||
* @return WC_Order|false
|
||||
*/
|
||||
public function get_last_order( &$customer );
|
||||
|
||||
/**
|
||||
* Return the number of orders this customer has.
|
||||
*
|
||||
* @param WC_Customer
|
||||
* @return integer
|
||||
*/
|
||||
public function get_order_count( &$customer );
|
||||
|
||||
/**
|
||||
* Return how much money this customer has spent.
|
||||
*
|
||||
* @param WC_Customer
|
||||
* @return float
|
||||
*/
|
||||
public function get_total_spent( &$customer );
|
||||
}
|
|
@ -155,6 +155,13 @@ abstract class WC_Legacy_Customer extends WC_Data {
|
|||
_deprecated_function( 'WC_Customer::set_default_data', '2.7' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Save data function.
|
||||
*/
|
||||
public function save_data() {
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the user a paying customer?
|
||||
* @todo should this be moved to a get_ readonly?
|
||||
|
|
|
@ -38,8 +38,7 @@ class WC_Shortcode_Cart {
|
|||
}
|
||||
|
||||
WC()->customer->set_calculated_shipping( true );
|
||||
|
||||
WC()->customer->save_to_session();
|
||||
WC()->customer->save();
|
||||
|
||||
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
|
||||
|
||||
|
|
|
@ -568,6 +568,17 @@ function wc_timezone_string() {
|
|||
return $timezone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback which can flatten post meta (gets the first value if it's an array).
|
||||
*
|
||||
* @since 2.7.0
|
||||
* @param array $value
|
||||
* @return mixed
|
||||
*/
|
||||
function wc_flatten_meta_callback( $value ) {
|
||||
return is_array( $value ) ? current( $value ) : $value;
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'wc_rgb_from_hex' ) ) {
|
||||
|
||||
/**
|
||||
|
|
|
@ -33,8 +33,7 @@ class WC_Helper_Customer {
|
|||
|
||||
WC_Helper_Customer::set_customer_details( $customer_data );
|
||||
|
||||
$customer = new WC_Customer();
|
||||
$customer->load_session();
|
||||
$customer = new WC_Customer( 0, true );
|
||||
return $customer;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer->set_username( 'testusername-' . time() );
|
||||
$customer->set_password( 'test123' );
|
||||
$customer->set_email( 'test@woo.local' );
|
||||
$customer->create();
|
||||
$customer->save();
|
||||
$wp_user = new WP_User( $customer->get_id() );
|
||||
|
||||
$this->assertEquals( $username, $customer->get_username() );
|
||||
|
@ -36,7 +36,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer->set_email( 'test@wc.local' );
|
||||
$customer->set_first_name( 'Justin' );
|
||||
$customer->set_billing_address_2( 'Apt 5' );
|
||||
$customer->update();
|
||||
$customer->save();
|
||||
|
||||
$customer = new WC_Customer( $customer_id ); // so we can read fresh copies from the DB
|
||||
$this->assertEquals( 'test@wc.local', $customer->get_email() );
|
||||
|
@ -80,7 +80,6 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer_id = $customer->get_id();
|
||||
$this->assertNotEquals( 0, $customer->get_id() );
|
||||
$customer->delete();
|
||||
$customer->read( $customer_id );
|
||||
$this->assertEquals( 0, $customer->get_id() );
|
||||
}
|
||||
|
||||
|
@ -94,16 +93,15 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer->set_username( $username );
|
||||
$customer->set_email( 'test@woo.local' );
|
||||
$customer->set_password( 'hunter2' );
|
||||
$customer->set_first_name( 'Bob' );
|
||||
$customer->set_first_name( 'Billy' );
|
||||
$customer->set_last_name( 'Bob' );
|
||||
$customer->create();
|
||||
$customer->save();
|
||||
$customer_id = $customer->get_id();
|
||||
$customer_read = new WC_Customer();
|
||||
$customer_read->read( $customer_id );
|
||||
$customer_read = new WC_Customer( $customer_id );
|
||||
|
||||
$this->assertEquals( $customer_id, $customer_read->get_id() );
|
||||
$this->assertEquals( 'test@woo.local', $customer_read->get_email() );
|
||||
$this->assertEquals( 'Bob', $customer_read->get_first_name() );
|
||||
$this->assertEquals( 'Billy', $customer_read->get_first_name() );
|
||||
$this->assertEquals( 'Bob', $customer_read->get_last_name() );
|
||||
$this->assertEquals( $username, $customer_read->get_username() );
|
||||
}
|
||||
|
@ -221,7 +219,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer = WC_Helper_Customer::create_customer();
|
||||
$customer_id = $customer->get_id();
|
||||
$order = WC_Helper_Order::create_order( $customer_id );
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
$last_order = $customer->get_last_order();
|
||||
$this->assertEquals( $order->get_id(), $last_order ? $last_order->get_id() : 0 );
|
||||
$this->assertEquals( $order->get_date_created(), $last_order ? $last_order->get_date_created() : 0 );
|
||||
|
@ -238,7 +236,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
WC_Helper_Order::create_order( $customer_id );
|
||||
WC_Helper_Order::create_order( $customer_id );
|
||||
WC_Helper_Order::create_order( $customer_id );
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
$this->assertEquals( 3, $customer->get_order_count() );
|
||||
}
|
||||
|
||||
|
@ -250,10 +248,10 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer = WC_Helper_Customer::create_customer();
|
||||
$customer_id = $customer->get_id();
|
||||
$order = WC_Helper_Order::create_order( $customer_id );
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
$this->assertEquals( 0, $customer->get_total_spent() );
|
||||
$order->update_status( 'wc-completed' );
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
$this->assertEquals( 40, $customer->get_total_spent() );
|
||||
$order->delete();
|
||||
}
|
||||
|
@ -306,7 +304,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer->set_shipping_postcode( '11111' );
|
||||
$customer->set_shipping_city( 'Test' );
|
||||
$customer->save();
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
|
||||
update_option( 'woocommerce_tax_based_on', 'shipping' );
|
||||
$taxable = $customer->get_taxable_address();
|
||||
|
@ -437,14 +435,12 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$this->assertEquals( 'Philadelphia', $session->get_billing_city() );
|
||||
|
||||
$session->set_billing_address( '124 South Street' );
|
||||
$session->save_to_session();
|
||||
$session->save();
|
||||
|
||||
$session = new WC_Customer( 0, true );
|
||||
$session->load_session();
|
||||
$this->assertEquals( '124 South Street', $session->get_billing_address() );
|
||||
|
||||
$session = new WC_Customer( 0, true );
|
||||
$session->load_session();
|
||||
$session->set_billing_postcode( '32191' );
|
||||
$session->save();
|
||||
|
||||
|
@ -462,7 +458,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$customer_id = $customer->get_id();
|
||||
$meta_value = time() . '-custom-value';
|
||||
add_user_meta( $customer_id, 'test_field', $meta_value, true );
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
$fields = $customer->get_meta_data();
|
||||
$this->assertEquals( $meta_value, $customer->get_meta( 'test_field' ) );
|
||||
}
|
||||
|
@ -477,7 +473,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
|||
$meta_value = time() . '-custom-value';
|
||||
$customer->add_meta_data( 'my-field', $meta_value, true );
|
||||
$customer->save();
|
||||
$customer->read( $customer_id );
|
||||
$customer = new WC_Customer( $customer_id );
|
||||
$this->assertEquals( $meta_value, $customer->get_meta( 'my-field' ) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -287,8 +287,11 @@ final class WooCommerce {
|
|||
include_once( WC_ABSPATH . 'includes/class-wc-data-store.php' ); // WC_Data_Store for CRUD
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/interface-wc-object-data-store.php' );
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/interface-wc-coupon-data-store.php' );
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/wc-customer-data-store-interface.php' );
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-data-store-cpt.php' );
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-coupon-data-store-cpt.php' );
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store.php' );
|
||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store-session.php' );
|
||||
|
||||
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
||||
include_once( WC_ABSPATH . 'includes/class-wc-cli.php' );
|
||||
|
@ -349,8 +352,9 @@ final class WooCommerce {
|
|||
// Classes/actions loaded for the frontend and for ajax requests.
|
||||
if ( $this->is_request( 'frontend' ) ) {
|
||||
$this->cart = new WC_Cart(); // Cart class, stores the cart contents
|
||||
$this->customer = new WC_Customer( get_current_user_id(), true ); // Customer class, handles data such as customer location
|
||||
$this->structured_data = new WC_Structured_Data(); // Structured Data class, generates and handles structured data
|
||||
$this->customer = new WC_Customer( get_current_user_id(), true ); // Customer class, handles data such as customer location
|
||||
add_action( 'shutdown', array( $this->customer, 'save' ), 10 ); // Customer should be saved during shutdown.
|
||||
}
|
||||
|
||||
$this->load_webhooks();
|
||||
|
|
Loading…
Reference in New Issue