Merge branch 'master' into product-crud
# Conflicts: # includes/class-wc-data-store.php # woocommerce.php
This commit is contained in:
commit
af3496402d
|
@ -120,12 +120,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 ) {
|
public function delete( $force_delete = false ) {
|
||||||
if ( $this->data_store ) {
|
if ( $this->data_store ) {
|
||||||
$this->data_store->delete( $this, $force_delete );
|
$this->data_store->delete( $this, array( 'force_delete' => $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_username( $request['username'] );
|
||||||
$customer->set_password( $request['password'] );
|
$customer->set_password( $request['password'] );
|
||||||
$customer->set_email( $request['email'] );
|
$customer->set_email( $request['email'] );
|
||||||
$customer->create();
|
$customer->save();
|
||||||
|
|
||||||
if ( ! $customer->get_id() ) {
|
if ( ! $customer->get_id() ) {
|
||||||
throw new WC_REST_Exception( 'woocommerce_rest_cannot_create', __( 'This resource cannot be created.', 'woocommerce' ), 400 );
|
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
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function geolocation_ajax_get_location_hash() {
|
public static function geolocation_ajax_get_location_hash() {
|
||||||
$customer = new WC_Customer();
|
$customer = new WC_Customer( 0, true );
|
||||||
$customer->load_session();
|
|
||||||
$location = array();
|
$location = array();
|
||||||
$location['country'] = $customer->get_billing_country();
|
$location['country'] = $customer->get_billing_country();
|
||||||
$location['state'] = $customer->get_billing_state();
|
$location['state'] = $customer->get_billing_state();
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -34,6 +34,8 @@ class WC_Data_Store {
|
||||||
'product_grouped' => 'WC_Product_Grouped_Data_Store_CPT',
|
'product_grouped' => 'WC_Product_Grouped_Data_Store_CPT',
|
||||||
'product_variable' => 'WC_Product_Variable_Data_Store_CPT',
|
'product_variable' => 'WC_Product_Variable_Data_Store_CPT',
|
||||||
'product_variation' => 'WC_Product_Variation_Data_Store_CPT',
|
'product_variation' => 'WC_Product_Variation_Data_Store_CPT',
|
||||||
|
'customer' => 'WC_Customer_Data_Store',
|
||||||
|
'customer-session' => 'WC_Customer_Data_Store_Session',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -128,10 +130,10 @@ class WC_Data_Store {
|
||||||
*
|
*
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
* @param WC_Data
|
* @param WC_Data
|
||||||
* @param bool $force_delete True to permently delete, false to trash.
|
* @param array $args Array of args to pass to the delete method.
|
||||||
*/
|
*/
|
||||||
public function delete( &$data, $force_delete = false ) {
|
public function delete( &$data, $args = array() ) {
|
||||||
$this->instance->delete( $data, $force_delete );
|
$this->instance->delete( $data, $args );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -36,6 +36,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_CPT implements WC_Coupon_Da
|
||||||
$coupon->set_id( $coupon_id );
|
$coupon->set_id( $coupon_id );
|
||||||
$this->update_post_meta( $coupon );
|
$this->update_post_meta( $coupon );
|
||||||
$coupon->save_meta_data();
|
$coupon->save_meta_data();
|
||||||
|
$coupon->apply_changes();
|
||||||
do_action( 'woocommerce_new_coupon', $coupon_id );
|
do_action( 'woocommerce_new_coupon', $coupon_id );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,6 +99,7 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_CPT implements WC_Coupon_Da
|
||||||
wp_update_post( $post_data );
|
wp_update_post( $post_data );
|
||||||
$this->update_post_meta( $coupon );
|
$this->update_post_meta( $coupon );
|
||||||
$coupon->save_meta_data();
|
$coupon->save_meta_data();
|
||||||
|
$coupon->apply_changes();
|
||||||
do_action( 'woocommerce_update_coupon', $coupon->get_id() );
|
do_action( 'woocommerce_update_coupon', $coupon->get_id() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,11 +108,16 @@ class WC_Coupon_Data_Store_CPT extends WC_Data_Store_CPT implements WC_Coupon_Da
|
||||||
*
|
*
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
* @param WC_Coupon
|
* @param WC_Coupon
|
||||||
* @param bool $force_delete True to permently delete, false to trash.
|
* @param array $args Array of args to pass to the delete method.
|
||||||
*/
|
*/
|
||||||
public function delete( &$coupon, $force_delete = false ) {
|
public function delete( &$coupon, $args = array() ) {
|
||||||
|
$args = wp_parse_args( $args, array(
|
||||||
|
'force_delete' => false,
|
||||||
|
) );
|
||||||
|
|
||||||
$id = $coupon->get_id();
|
$id = $coupon->get_id();
|
||||||
if ( $force_delete ) {
|
|
||||||
|
if ( $args['force_delete'] ) {
|
||||||
wp_delete_post( $coupon->get_id() );
|
wp_delete_post( $coupon->get_id() );
|
||||||
$coupon->set_id( 0 );
|
$coupon->set_id( 0 );
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -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 array $args Array of args to pass to the delete method.
|
||||||
|
*/
|
||||||
|
public function delete( &$customer, $args = array() ) {
|
||||||
|
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,244 @@
|
||||||
|
<?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->save_meta_data();
|
||||||
|
$customer->apply_changes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$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();
|
||||||
|
$customer->apply_changes();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes a customer from the database.
|
||||||
|
*
|
||||||
|
* @since 2.7.0
|
||||||
|
* @param WC_Customer
|
||||||
|
* @param array $args Array of args to pass to the delete method.
|
||||||
|
*/
|
||||||
|
public function delete( &$customer, $args = array() ) {
|
||||||
|
if ( ! $customer->get_id() ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$args = wp_parse_args( $args, array(
|
||||||
|
'reassign' => 0,
|
||||||
|
) );
|
||||||
|
return wp_delete_user( $customer->get_id(), $args['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 );
|
||||||
|
}
|
||||||
|
}
|
|
@ -33,7 +33,8 @@ interface WC_Object_Data_Store {
|
||||||
/**
|
/**
|
||||||
* Deletes a record from the database.
|
* Deletes a record from the database.
|
||||||
* @param WC_Data
|
* @param WC_Data
|
||||||
* @param bool $force_delete True to permently delete, false to trash.
|
* @param array $args Array of args to pass to the delete method.
|
||||||
|
* @return bool result
|
||||||
*/
|
*/
|
||||||
public function delete( &$data, $force_delete = false );
|
public function delete( &$data, $args = array() );
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?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 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 );
|
||||||
|
}
|
|
@ -153,6 +153,13 @@ abstract class WC_Legacy_Customer extends WC_Data {
|
||||||
_deprecated_function( 'WC_Customer::set_default_data', '2.7' );
|
_deprecated_function( 'WC_Customer::set_default_data', '2.7' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save data function.
|
||||||
|
*/
|
||||||
|
public function save_data() {
|
||||||
|
$this->save();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is the user a paying customer?
|
* Is the user a paying customer?
|
||||||
* @return bool
|
* @return bool
|
||||||
|
|
|
@ -38,8 +38,7 @@ class WC_Shortcode_Cart {
|
||||||
}
|
}
|
||||||
|
|
||||||
WC()->customer->set_calculated_shipping( true );
|
WC()->customer->set_calculated_shipping( true );
|
||||||
|
WC()->customer->save();
|
||||||
WC()->customer->save_to_session();
|
|
||||||
|
|
||||||
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
|
wc_add_notice( __( 'Shipping costs updated.', 'woocommerce' ), 'notice' );
|
||||||
|
|
||||||
|
|
|
@ -578,6 +578,17 @@ function wc_timezone_string() {
|
||||||
return $timezone;
|
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' ) ) {
|
if ( ! function_exists( 'wc_rgb_from_hex' ) ) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,7 @@ class WC_Dummy_Data_Store_CPT implements WC_Object_Data_Store {
|
||||||
public function create( &$data ) { }
|
public function create( &$data ) { }
|
||||||
public function read( &$data ) { }
|
public function read( &$data ) { }
|
||||||
public function update( &$data ) { }
|
public function update( &$data ) { }
|
||||||
public function delete( &$data, $force_delete = false ) { }
|
public function delete( &$data, $args = array() ) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,5 +32,5 @@ class WC_Dummy_Data_Store_Custom_Table implements WC_Object_Data_Store {
|
||||||
public function create( &$data ) { }
|
public function create( &$data ) { }
|
||||||
public function read( &$data ) { }
|
public function read( &$data ) { }
|
||||||
public function update( &$data ) { }
|
public function update( &$data ) { }
|
||||||
public function delete( &$data, $force_delete = false ) { }
|
public function delete( &$data, $args = array() ) { }
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,8 +33,7 @@ class WC_Helper_Customer {
|
||||||
|
|
||||||
WC_Helper_Customer::set_customer_details( $customer_data );
|
WC_Helper_Customer::set_customer_details( $customer_data );
|
||||||
|
|
||||||
$customer = new WC_Customer();
|
$customer = new WC_Customer( 0, true );
|
||||||
$customer->load_session();
|
|
||||||
return $customer;
|
return $customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
||||||
$customer->set_username( 'testusername-' . time() );
|
$customer->set_username( 'testusername-' . time() );
|
||||||
$customer->set_password( 'test123' );
|
$customer->set_password( 'test123' );
|
||||||
$customer->set_email( 'test@woo.local' );
|
$customer->set_email( 'test@woo.local' );
|
||||||
$customer->create();
|
$customer->save();
|
||||||
$wp_user = new WP_User( $customer->get_id() );
|
$wp_user = new WP_User( $customer->get_id() );
|
||||||
|
|
||||||
$this->assertEquals( $username, $customer->get_username() );
|
$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_email( 'test@wc.local' );
|
||||||
$customer->set_first_name( 'Justin' );
|
$customer->set_first_name( 'Justin' );
|
||||||
$customer->set_billing_address_2( 'Apt 5' );
|
$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
|
$customer = new WC_Customer( $customer_id ); // so we can read fresh copies from the DB
|
||||||
$this->assertEquals( 'test@wc.local', $customer->get_email() );
|
$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();
|
$customer_id = $customer->get_id();
|
||||||
$this->assertNotEquals( 0, $customer->get_id() );
|
$this->assertNotEquals( 0, $customer->get_id() );
|
||||||
$customer->delete();
|
$customer->delete();
|
||||||
$customer->read( $customer_id );
|
|
||||||
$this->assertEquals( 0, $customer->get_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_username( $username );
|
||||||
$customer->set_email( 'test@woo.local' );
|
$customer->set_email( 'test@woo.local' );
|
||||||
$customer->set_password( 'hunter2' );
|
$customer->set_password( 'hunter2' );
|
||||||
$customer->set_first_name( 'Bob' );
|
$customer->set_first_name( 'Billy' );
|
||||||
$customer->set_last_name( 'Bob' );
|
$customer->set_last_name( 'Bob' );
|
||||||
$customer->create();
|
$customer->save();
|
||||||
$customer_id = $customer->get_id();
|
$customer_id = $customer->get_id();
|
||||||
$customer_read = new WC_Customer();
|
$customer_read = new WC_Customer( $customer_id );
|
||||||
$customer_read->read( $customer_id );
|
|
||||||
|
|
||||||
$this->assertEquals( $customer_id, $customer_read->get_id() );
|
$this->assertEquals( $customer_id, $customer_read->get_id() );
|
||||||
$this->assertEquals( 'test@woo.local', $customer_read->get_email() );
|
$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( 'Bob', $customer_read->get_last_name() );
|
||||||
$this->assertEquals( $username, $customer_read->get_username() );
|
$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 = WC_Helper_Customer::create_customer();
|
||||||
$customer_id = $customer->get_id();
|
$customer_id = $customer->get_id();
|
||||||
$order = WC_Helper_Order::create_order( $customer_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();
|
$last_order = $customer->get_last_order();
|
||||||
$this->assertEquals( $order->get_id(), $last_order ? $last_order->get_id() : 0 );
|
$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 );
|
$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 );
|
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() );
|
$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 = WC_Helper_Customer::create_customer();
|
||||||
$customer_id = $customer->get_id();
|
$customer_id = $customer->get_id();
|
||||||
$order = WC_Helper_Order::create_order( $customer_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() );
|
$this->assertEquals( 0, $customer->get_total_spent() );
|
||||||
$order->update_status( 'wc-completed' );
|
$order->update_status( 'wc-completed' );
|
||||||
$customer->read( $customer_id );
|
$customer = new WC_Customer( $customer_id );
|
||||||
$this->assertEquals( 40, $customer->get_total_spent() );
|
$this->assertEquals( 40, $customer->get_total_spent() );
|
||||||
$order->delete();
|
$order->delete();
|
||||||
}
|
}
|
||||||
|
@ -306,7 +304,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
||||||
$customer->set_shipping_postcode( '11111' );
|
$customer->set_shipping_postcode( '11111' );
|
||||||
$customer->set_shipping_city( 'Test' );
|
$customer->set_shipping_city( 'Test' );
|
||||||
$customer->save();
|
$customer->save();
|
||||||
$customer->read( $customer_id );
|
$customer = new WC_Customer( $customer_id );
|
||||||
|
|
||||||
update_option( 'woocommerce_tax_based_on', 'shipping' );
|
update_option( 'woocommerce_tax_based_on', 'shipping' );
|
||||||
$taxable = $customer->get_taxable_address();
|
$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() );
|
$this->assertEquals( 'Philadelphia', $session->get_billing_city() );
|
||||||
|
|
||||||
$session->set_billing_address( '124 South Street' );
|
$session->set_billing_address( '124 South Street' );
|
||||||
$session->save_to_session();
|
$session->save();
|
||||||
|
|
||||||
$session = new WC_Customer( 0, true );
|
$session = new WC_Customer( 0, true );
|
||||||
$session->load_session();
|
|
||||||
$this->assertEquals( '124 South Street', $session->get_billing_address() );
|
$this->assertEquals( '124 South Street', $session->get_billing_address() );
|
||||||
|
|
||||||
$session = new WC_Customer( 0, true );
|
$session = new WC_Customer( 0, true );
|
||||||
$session->load_session();
|
|
||||||
$session->set_billing_postcode( '32191' );
|
$session->set_billing_postcode( '32191' );
|
||||||
$session->save();
|
$session->save();
|
||||||
|
|
||||||
|
@ -462,7 +458,7 @@ class WC_Tests_CustomerCRUD extends WC_Unit_Test_Case {
|
||||||
$customer_id = $customer->get_id();
|
$customer_id = $customer->get_id();
|
||||||
$meta_value = time() . '-custom-value';
|
$meta_value = time() . '-custom-value';
|
||||||
add_user_meta( $customer_id, 'test_field', $meta_value, true );
|
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();
|
$fields = $customer->get_meta_data();
|
||||||
$this->assertEquals( $meta_value, $customer->get_meta( 'test_field' ) );
|
$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';
|
$meta_value = time() . '-custom-value';
|
||||||
$customer->add_meta_data( 'my-field', $meta_value, true );
|
$customer->add_meta_data( 'my-field', $meta_value, true );
|
||||||
$customer->save();
|
$customer->save();
|
||||||
$customer->read( $customer_id );
|
$customer = new WC_Customer( $customer_id );
|
||||||
$this->assertEquals( $meta_value, $customer->get_meta( 'my-field' ) );
|
$this->assertEquals( $meta_value, $customer->get_meta( 'my-field' ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -289,6 +289,7 @@ final class WooCommerce {
|
||||||
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/interface-wc-coupon-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-product-data-store-interface.php' );
|
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/wc-product-data-store-interface.php' );
|
||||||
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/wc-product-variable-data-store-interface.php' );
|
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/wc-product-variable-data-store-interface.php' );
|
||||||
|
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/wc-customer-data-store-interface.php' );
|
||||||
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/class-wc-payment-token-data-store-interface.php' );
|
include_once( WC_ABSPATH . 'includes/data-stores/interfaces/class-wc-payment-token-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-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-coupon-data-store-cpt.php' );
|
||||||
|
@ -297,6 +298,8 @@ final class WooCommerce {
|
||||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php' );
|
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php' );
|
||||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php' );
|
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php' );
|
||||||
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-data-store-cpt.php' );
|
include_once( WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-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' );
|
||||||
|
|
||||||
$this->query = new WC_Query();
|
$this->query = new WC_Query();
|
||||||
$this->api = new WC_API();
|
$this->api = new WC_API();
|
||||||
|
@ -353,8 +356,9 @@ final class WooCommerce {
|
||||||
// Classes/actions loaded for the frontend and for ajax requests.
|
// Classes/actions loaded for the frontend and for ajax requests.
|
||||||
if ( $this->is_request( 'frontend' ) ) {
|
if ( $this->is_request( 'frontend' ) ) {
|
||||||
$this->cart = new WC_Cart(); // Cart class, stores the cart contents
|
$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->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();
|
$this->load_webhooks();
|
||||||
|
|
Loading…
Reference in New Issue