2016-11-14 18:18:08 +00:00
|
|
|
<?php
|
2018-03-08 19:27:54 +00:00
|
|
|
/**
|
|
|
|
* Class WC_Customer_Data_Store file.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\DataStores
|
|
|
|
*/
|
|
|
|
|
2016-11-14 18:18:08 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC Customer Data Store.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @version 3.0.0
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
2016-11-22 13:54:51 +00:00
|
|
|
class WC_Customer_Data_Store extends WC_Data_Store_WP implements WC_Customer_Data_Store_Interface, WC_Object_Data_Store_Interface {
|
2016-11-21 23:48:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data stored in meta keys, but not considered "meta".
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-21 23:48:49 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $internal_meta_keys = array(
|
2016-12-07 20:23:23 +00:00
|
|
|
'locale',
|
2016-11-21 23:48:49 +00:00
|
|
|
'billing_postcode',
|
|
|
|
'billing_city',
|
|
|
|
'billing_address_1',
|
|
|
|
'billing_address_2',
|
|
|
|
'billing_state',
|
|
|
|
'billing_country',
|
|
|
|
'shipping_postcode',
|
|
|
|
'shipping_city',
|
|
|
|
'shipping_address_1',
|
|
|
|
'shipping_address_2',
|
|
|
|
'shipping_state',
|
|
|
|
'shipping_country',
|
|
|
|
'paying_customer',
|
|
|
|
'last_update',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
2017-05-23 23:12:58 +00:00
|
|
|
'display_name',
|
2016-11-21 23:48:49 +00:00
|
|
|
'show_admin_bar_front',
|
|
|
|
'use_ssl',
|
|
|
|
'admin_color',
|
|
|
|
'rich_editing',
|
|
|
|
'comment_shortcuts',
|
|
|
|
'dismissed_wp_pointers',
|
|
|
|
'show_welcome_panel',
|
|
|
|
'session_tokens',
|
|
|
|
'nickname',
|
|
|
|
'description',
|
|
|
|
'billing_first_name',
|
|
|
|
'billing_last_name',
|
|
|
|
'billing_company',
|
|
|
|
'billing_phone',
|
|
|
|
'billing_email',
|
|
|
|
'shipping_first_name',
|
2016-11-25 21:46:52 +00:00
|
|
|
'shipping_last_name',
|
2017-04-28 12:44:22 +00:00
|
|
|
'shipping_company',
|
2016-11-21 23:48:49 +00:00
|
|
|
'wptests_capabilities',
|
|
|
|
'wptests_user_level',
|
2017-11-16 18:58:09 +00:00
|
|
|
'syntax_highlighting',
|
2017-03-06 22:03:26 +00:00
|
|
|
'_order_count',
|
|
|
|
'_money_spent',
|
2016-11-21 23:48:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal meta type used to store user data.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $meta_type = 'user';
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2016-11-22 12:20:25 +00:00
|
|
|
/**
|
|
|
|
* Callback to remove unwanted meta data.
|
|
|
|
*
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param object $meta Meta object.
|
2016-11-22 12:20:25 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function exclude_internal_meta_keys( $meta ) {
|
|
|
|
global $wpdb;
|
2017-04-06 13:11:00 +00:00
|
|
|
|
|
|
|
$table_prefix = $wpdb->prefix ? $wpdb->prefix : 'wp_';
|
|
|
|
|
2018-03-08 19:27:54 +00:00
|
|
|
return ! in_array( $meta->meta_key, $this->internal_meta_keys, true )
|
2017-06-08 12:47:23 +00:00
|
|
|
&& 0 !== strpos( $meta->meta_key, '_woocommerce_persistent_cart' )
|
2016-11-22 12:20:25 +00:00
|
|
|
&& 0 !== strpos( $meta->meta_key, 'closedpostboxes_' )
|
|
|
|
&& 0 !== strpos( $meta->meta_key, 'metaboxhidden_' )
|
|
|
|
&& 0 !== strpos( $meta->meta_key, 'manageedit-' )
|
2017-04-06 13:11:00 +00:00
|
|
|
&& ! strstr( $meta->meta_key, $table_prefix )
|
2017-02-01 00:43:52 +00:00
|
|
|
&& 0 !== stripos( $meta->meta_key, 'wp_' );
|
2018-03-07 19:16:01 +00:00
|
|
|
}
|
2016-11-22 12:20:25 +00:00
|
|
|
|
2016-11-14 18:18:08 +00:00
|
|
|
/**
|
|
|
|
* Method to create a new customer in the database.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-05-12 08:52:26 +00:00
|
|
|
*
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2017-05-12 08:52:26 +00:00
|
|
|
*
|
2018-03-08 19:27:54 +00:00
|
|
|
* @throws WC_Data_Exception If unable to create new customer.
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function create( &$customer ) {
|
2016-11-18 07:53:18 +00:00
|
|
|
$id = wc_create_new_customer( $customer->get_email(), $customer->get_username(), $customer->get_password() );
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2016-11-18 07:53:18 +00:00
|
|
|
if ( is_wp_error( $id ) ) {
|
|
|
|
throw new WC_Data_Exception( $id->get_error_code(), $id->get_error_message() );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
2016-11-18 07:53:18 +00:00
|
|
|
|
|
|
|
$customer->set_id( $id );
|
|
|
|
$this->update_user_meta( $customer );
|
2017-04-28 12:44:22 +00:00
|
|
|
|
2018-03-08 19:27:54 +00:00
|
|
|
// Prevent wp_update_user calls in the same request and customer trigger the 'Notice of Password Changed' email.
|
2017-04-22 18:06:56 +00:00
|
|
|
$customer->set_password( '' );
|
2017-04-28 12:44:22 +00:00
|
|
|
|
2018-03-07 19:16:01 +00:00
|
|
|
wp_update_user(
|
|
|
|
apply_filters(
|
|
|
|
'woocommerce_update_customer_args', array(
|
|
|
|
'ID' => $customer->get_id(),
|
|
|
|
'role' => $customer->get_role(),
|
|
|
|
'display_name' => $customer->get_display_name(),
|
|
|
|
), $customer
|
|
|
|
)
|
|
|
|
);
|
2016-11-18 07:53:18 +00:00
|
|
|
$wp_user = new WP_User( $customer->get_id() );
|
2017-03-23 00:10:29 +00:00
|
|
|
$customer->set_date_created( $wp_user->user_registered );
|
2016-11-18 07:53:18 +00:00
|
|
|
$customer->set_date_modified( get_user_meta( $customer->get_id(), 'last_update', true ) );
|
|
|
|
$customer->save_meta_data();
|
|
|
|
$customer->apply_changes();
|
|
|
|
do_action( 'woocommerce_new_customer', $customer->get_id() );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to read a customer object.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
|
|
|
* @throws Exception If invalid customer.
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function read( &$customer ) {
|
2018-03-09 11:26:13 +00:00
|
|
|
$user_object = $customer->get_id() ? get_user_by( 'id', $customer->get_id() ) : false;
|
2018-03-08 19:27:54 +00:00
|
|
|
|
2016-11-14 18:18:08 +00:00
|
|
|
// User object is required.
|
2018-03-09 11:26:13 +00:00
|
|
|
if ( ! $user_object || empty( $user_object->ID ) ) {
|
2016-11-14 18:18:08 +00:00
|
|
|
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();
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2017-04-05 22:16:45 +00:00
|
|
|
// Load meta but exclude deprecated props.
|
2018-04-25 16:21:21 +00:00
|
|
|
$user_meta = array_diff_key(
|
|
|
|
array_map( 'wc_flatten_meta_callback', get_user_meta( $customer_id ) ),
|
|
|
|
array_flip( array( 'country', 'state', 'postcode', 'city', 'address', 'address_2', 'default', 'location' ) )
|
|
|
|
);
|
|
|
|
|
2017-04-05 22:16:45 +00:00
|
|
|
$customer->set_props( $user_meta );
|
2018-03-07 19:16:01 +00:00
|
|
|
$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,
|
|
|
|
'display_name' => $user_object->display_name,
|
|
|
|
'date_created' => $user_object->user_registered, // Mysql string in local format.
|
|
|
|
'date_modified' => get_user_meta( $customer_id, 'last_update', true ),
|
|
|
|
'role' => ! empty( $user_object->roles[0] ) ? $user_object->roles[0] : 'customer',
|
|
|
|
)
|
|
|
|
);
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer->read_meta_data();
|
|
|
|
$customer->set_object_read( true );
|
2016-11-18 07:53:18 +00:00
|
|
|
do_action( 'woocommerce_customer_loaded', $customer );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a customer in the database.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
public function update( &$customer ) {
|
2018-03-07 19:16:01 +00:00
|
|
|
wp_update_user(
|
|
|
|
apply_filters(
|
|
|
|
'woocommerce_update_customer_args', array(
|
|
|
|
'ID' => $customer->get_id(),
|
|
|
|
'user_email' => $customer->get_email(),
|
|
|
|
'display_name' => $customer->get_display_name(),
|
|
|
|
), $customer
|
|
|
|
)
|
|
|
|
);
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2016-11-23 14:41:40 +00:00
|
|
|
// Only update password if a new one was set with set_password.
|
|
|
|
if ( $customer->get_password() ) {
|
2018-03-07 19:16:01 +00:00
|
|
|
wp_update_user(
|
|
|
|
array(
|
|
|
|
'ID' => $customer->get_id(),
|
|
|
|
'user_pass' => $customer->get_password(),
|
|
|
|
)
|
|
|
|
);
|
2016-11-14 18:18:08 +00:00
|
|
|
$customer->set_password( '' );
|
|
|
|
}
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2016-11-14 18:18:08 +00:00
|
|
|
$this->update_user_meta( $customer );
|
|
|
|
$customer->set_date_modified( get_user_meta( $customer->get_id(), 'last_update', true ) );
|
|
|
|
$customer->save_meta_data();
|
2016-11-15 18:11:25 +00:00
|
|
|
$customer->apply_changes();
|
2016-11-18 07:53:18 +00:00
|
|
|
do_action( 'woocommerce_update_customer', $customer->get_id() );
|
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
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2018-03-07 19:16:01 +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
|
|
|
if ( ! $customer->get_id() ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2018-03-07 19:16:01 +00:00
|
|
|
$args = wp_parse_args(
|
|
|
|
$args, array(
|
|
|
|
'reassign' => 0,
|
|
|
|
)
|
|
|
|
);
|
2016-11-18 07:53:18 +00:00
|
|
|
|
|
|
|
$id = $customer->get_id();
|
|
|
|
wp_delete_user( $id, $args['reassign'] );
|
|
|
|
|
|
|
|
do_action( 'woocommerce_delete_customer', $id );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method that updates all the meta for a customer. Used for update & create.
|
2018-03-07 19:16:01 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2016-11-14 18:18:08 +00:00
|
|
|
*/
|
|
|
|
private function update_user_meta( $customer ) {
|
|
|
|
$updated_props = array();
|
2016-11-17 16:02:09 +00:00
|
|
|
$changed_props = $customer->get_changes();
|
2016-11-14 18:18:08 +00:00
|
|
|
|
|
|
|
$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 ) {
|
2016-11-17 16:02:09 +00:00
|
|
|
if ( ! array_key_exists( $prop, $changed_props ) ) {
|
2016-11-14 18:18:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( update_user_meta( $customer->get_id(), $meta_key, $customer->{"get_$prop"}( 'edit' ) ) ) {
|
|
|
|
$updated_props[] = $prop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 15:32:52 +00:00
|
|
|
$billing_address_props = array(
|
|
|
|
'billing_first_name' => 'billing_first_name',
|
|
|
|
'billing_last_name' => 'billing_last_name',
|
|
|
|
'billing_company' => 'billing_company',
|
|
|
|
'billing_address_1' => 'billing_address_1',
|
|
|
|
'billing_address_2' => 'billing_address_2',
|
|
|
|
'billing_city' => 'billing_city',
|
|
|
|
'billing_state' => 'billing_state',
|
|
|
|
'billing_postcode' => 'billing_postcode',
|
|
|
|
'billing_country' => 'billing_country',
|
|
|
|
'billing_email' => 'billing_email',
|
|
|
|
'billing_phone' => 'billing_phone',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $billing_address_props as $meta_key => $prop ) {
|
|
|
|
$prop_key = substr( $prop, 8 );
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2016-11-17 16:02:09 +00:00
|
|
|
if ( ! isset( $changed_props['billing'] ) || ! array_key_exists( $prop_key, $changed_props['billing'] ) ) {
|
2016-11-17 15:32:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2016-11-17 15:32:52 +00:00
|
|
|
if ( update_user_meta( $customer->get_id(), $meta_key, $customer->{"get_$prop"}( 'edit' ) ) ) {
|
|
|
|
$updated_props[] = $prop;
|
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 15:32:52 +00:00
|
|
|
$shipping_address_props = array(
|
|
|
|
'shipping_first_name' => 'shipping_first_name',
|
|
|
|
'shipping_last_name' => 'shipping_last_name',
|
|
|
|
'shipping_company' => 'shipping_company',
|
|
|
|
'shipping_address_1' => 'shipping_address_1',
|
|
|
|
'shipping_address_2' => 'shipping_address_2',
|
|
|
|
'shipping_city' => 'shipping_city',
|
|
|
|
'shipping_state' => 'shipping_state',
|
|
|
|
'shipping_postcode' => 'shipping_postcode',
|
|
|
|
'shipping_country' => 'shipping_country',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $shipping_address_props as $meta_key => $prop ) {
|
|
|
|
$prop_key = substr( $prop, 9 );
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2016-11-17 16:02:09 +00:00
|
|
|
if ( ! isset( $changed_props['shipping'] ) || ! array_key_exists( $prop_key, $changed_props['shipping'] ) ) {
|
2016-11-17 15:32:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-04-25 16:21:21 +00:00
|
|
|
|
2016-11-17 15:32:52 +00:00
|
|
|
if ( update_user_meta( $customer->get_id(), $meta_key, $customer->{"get_$prop"}( 'edit' ) ) ) {
|
|
|
|
$updated_props[] = $prop;
|
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
2017-01-24 21:38:02 +00:00
|
|
|
|
|
|
|
do_action( 'woocommerce_customer_object_updated_props', $customer, $updated_props );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the customers last order.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2016-11-14 18:18:08 +00:00
|
|
|
* @return WC_Order|false
|
|
|
|
*/
|
|
|
|
public function get_last_order( &$customer ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2018-05-07 20:54:42 +00:00
|
|
|
// On WC 3.5.0 the ID of the user that placed the order was moved from the post meta _customer_user to the post_author field in the wp_posts table.
|
|
|
|
if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '>=' ) ) {
|
|
|
|
$query = "SELECT ID
|
|
|
|
FROM $wpdb->posts
|
|
|
|
WHERE post_author = '" . esc_sql( $customer->get_id() ) . "'
|
|
|
|
AND post_type = 'shop_order'
|
|
|
|
AND post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
|
|
|
|
ORDER BY ID DESC";
|
|
|
|
} else {
|
|
|
|
$query = "SELECT posts.ID
|
2016-11-14 18:18:08 +00:00
|
|
|
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() ) ) ) . "' )
|
2018-05-07 20:54:42 +00:00
|
|
|
ORDER BY posts.ID DESC";
|
|
|
|
}
|
|
|
|
|
|
|
|
// phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
|
|
|
|
$last_order = $wpdb->get_var( $query );
|
2016-11-14 18:18:08 +00:00
|
|
|
|
2018-04-25 16:21:21 +00:00
|
|
|
if ( ! $last_order ) {
|
2016-11-14 18:18:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-25 16:21:21 +00:00
|
|
|
|
|
|
|
return wc_get_order( absint( $last_order ) );
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of orders this customer has.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2016-11-14 18:18:08 +00:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function get_order_count( &$customer ) {
|
|
|
|
$count = get_user_meta( $customer->get_id(), '_order_count', true );
|
|
|
|
|
|
|
|
if ( '' === $count ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
2018-05-07 20:54:42 +00:00
|
|
|
// On WC 3.5.0 the ID of the user that placed the order was moved from the post meta _customer_user to the post_author field in the wp_posts table.
|
|
|
|
if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '>=' ) ) {
|
|
|
|
$query = "SELECT COUNT(*)
|
|
|
|
FROM $wpdb->posts
|
|
|
|
WHERE post_type = 'shop_order'
|
|
|
|
AND post_status IN ( '" . implode( "','", array_map( 'esc_sql', array_keys( wc_get_order_statuses() ) ) ) . "' )
|
|
|
|
AND post_author = " . esc_sql( $customer->get_id() );
|
|
|
|
} else {
|
|
|
|
$query = "SELECT COUNT(*)
|
2016-11-14 18:18:08 +00:00
|
|
|
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() ) ) ) . "' )
|
2018-05-07 20:54:42 +00:00
|
|
|
AND meta_value = '" . esc_sql( $customer->get_id() ) . "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
|
|
|
|
$count = $wpdb->get_var( $query );
|
2016-11-14 18:18:08 +00:00
|
|
|
update_user_meta( $customer->get_id(), '_order_count', $count );
|
|
|
|
}
|
|
|
|
|
|
|
|
return absint( $count );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return how much money this customer has spent.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param WC_Customer $customer Customer object.
|
2016-11-14 18:18:08 +00:00
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_total_spent( &$customer ) {
|
2018-04-25 16:21:21 +00:00
|
|
|
$spent = apply_filters(
|
|
|
|
'woocommerce_customer_get_total_spent',
|
|
|
|
get_user_meta( $customer->get_id(), '_money_spent', true ),
|
|
|
|
$customer
|
|
|
|
);
|
2016-11-14 18:18:08 +00:00
|
|
|
|
|
|
|
if ( '' === $spent ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
|
2018-05-07 20:54:42 +00:00
|
|
|
|
|
|
|
// On WC 3.5.0 the ID of the user that placed the order was moved from the post meta _customer_user to the post_author field in the wp_posts table.
|
|
|
|
if ( version_compare( get_option( 'woocommerce_db_version' ), '3.5.0', '>=' ) ) {
|
|
|
|
$query = "SELECT SUM(meta.meta_value)
|
|
|
|
FROM $wpdb->posts as posts
|
|
|
|
LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
|
|
|
|
WHERE posts.post_author = '" . esc_sql( $customer->get_id() ) . "'
|
|
|
|
AND posts.post_type = 'shop_order'
|
|
|
|
AND posts.post_status IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
|
|
|
|
AND meta.meta_key = '_order_total'";
|
|
|
|
} else {
|
|
|
|
$query = "SELECT SUM(meta2.meta_value)
|
2018-03-08 19:27:54 +00:00
|
|
|
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 ) . "' )
|
2018-05-07 20:54:42 +00:00
|
|
|
AND meta2.meta_key = '_order_total'";
|
|
|
|
}
|
|
|
|
|
|
|
|
// phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
|
|
|
|
$spent = $wpdb->get_var( apply_filters( 'woocommerce_customer_get_total_spent_query', $query, $customer ) );
|
2016-11-14 18:18:08 +00:00
|
|
|
|
|
|
|
if ( ! $spent ) {
|
|
|
|
$spent = 0;
|
|
|
|
}
|
|
|
|
update_user_meta( $customer->get_id(), '_money_spent', $spent );
|
|
|
|
}
|
|
|
|
|
|
|
|
return wc_format_decimal( $spent, 2 );
|
|
|
|
}
|
2016-11-24 15:31:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Search customers and return customer IDs.
|
|
|
|
*
|
2018-03-08 19:27:54 +00:00
|
|
|
* @param string $term Search term.
|
|
|
|
* @param int|string $limit Limit search results.
|
|
|
|
* @since 3.0.7
|
2017-07-19 02:44:41 +00:00
|
|
|
*
|
2016-11-24 15:31:05 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-05-16 09:16:57 +00:00
|
|
|
public function search_customers( $term, $limit = '' ) {
|
2017-08-28 19:23:12 +00:00
|
|
|
$results = apply_filters( 'woocommerce_customer_pre_search_customers', false, $term, $limit );
|
|
|
|
if ( is_array( $results ) ) {
|
2017-08-28 19:14:13 +00:00
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2018-03-07 19:16:01 +00:00
|
|
|
$query = new WP_User_Query(
|
|
|
|
apply_filters(
|
|
|
|
'woocommerce_customer_search_customers', array(
|
|
|
|
'search' => '*' . esc_attr( $term ) . '*',
|
|
|
|
'search_columns' => array( 'user_login', 'user_url', 'user_email', 'user_nicename', 'display_name' ),
|
|
|
|
'fields' => 'ID',
|
|
|
|
'number' => $limit,
|
|
|
|
), $term, $limit, 'main_query'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$query2 = new WP_User_Query(
|
|
|
|
apply_filters(
|
|
|
|
'woocommerce_customer_search_customers', array(
|
|
|
|
'fields' => 'ID',
|
|
|
|
'number' => $limit,
|
|
|
|
'meta_query' => array(
|
|
|
|
'relation' => 'OR',
|
|
|
|
array(
|
|
|
|
'key' => 'first_name',
|
|
|
|
'value' => $term,
|
|
|
|
'compare' => 'LIKE',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'key' => 'last_name',
|
|
|
|
'value' => $term,
|
|
|
|
'compare' => 'LIKE',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
), $term, $limit, 'meta_query'
|
|
|
|
)
|
|
|
|
);
|
2017-05-16 09:16:57 +00:00
|
|
|
|
2017-08-28 19:15:19 +00:00
|
|
|
$results = wp_parse_id_list( array_merge( (array) $query->get_results(), (array) $query2->get_results() ) );
|
2017-05-16 09:16:57 +00:00
|
|
|
|
|
|
|
if ( $limit && count( $results ) > $limit ) {
|
|
|
|
$results = array_slice( $results, 0, $limit );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
2016-11-24 15:31:05 +00:00
|
|
|
}
|
2016-11-14 18:18:08 +00:00
|
|
|
}
|