/** * Test reading a customer. * @since 2.7.0 */ public function test_read_customer() { $username = '******' . time(); $customer = new WC_Customer(); $customer->set_username($username); $customer->set_email('*****@*****.**'); $customer->set_password('hunter2'); $customer->set_first_name('Bob'); $customer->set_last_name('Bob'); $customer->create(); $customer_id = $customer->get_id(); $customer_read = new WC_Customer(); $customer_read->read($customer_id); $this->assertEquals($customer_id, $customer_read->get_id()); $this->assertEquals('*****@*****.**', $customer_read->get_email()); $this->assertEquals('Bob', $customer_read->get_first_name()); $this->assertEquals('Bob', $customer_read->get_last_name()); $this->assertEquals($username, $customer_read->get_username()); }
/** * Search for customers and return json. */ public static function json_search_customers() { ob_start(); check_ajax_referer('search-customers', 'security'); if (!current_user_can('edit_shop_orders')) { die(-1); } $term = wc_clean(stripslashes($_GET['term'])); $exclude = array(); if (empty($term)) { die; } $data_store = WC_Data_Store::load('customer'); $ids = $data_store->search_customers($term); $found_customers = array(); if (!empty($_GET['exclude'])) { $ids = array_diff($ids, (array) $_GET['exclude']); } foreach ($ids as $id) { $customer = new WC_Customer($id); /* translators: 1: user display name 2: user ID 3: user email */ $found_customers[$id] = sprintf(esc_html__('%1$s (#%2$s – %3$s)', 'woocommerce'), $customer->get_first_name() . ' ' . $customer->get_last_name(), $customer->get_id(), $customer->get_email()); } wp_send_json(apply_filters('woocommerce_json_search_found_customers', $found_customers)); }
/** * Get the customer for the given ID * * @since 2.1 * @param int $id the customer ID * @param array $fields * @return array */ public function get_customer($id, $fields = null) { global $wpdb; $id = $this->validate_request($id, 'customer', 'read'); if (is_wp_error($id)) { return $id; } $customer = new WC_Customer($id); $last_order = $customer->get_last_order(); $customer_data = array('id' => $customer->get_id(), 'created_at' => $this->server->format_datetime($customer->get_date_created(), false, true), 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'role' => $customer->get_role(), 'last_order_id' => is_object($last_order) ? $last_order->get_id() : null, 'last_order_date' => is_object($last_order) ? $this->server->format_datetime($last_order->get_date_created(), false, true) : null, 'orders_count' => $customer->get_order_count(), 'total_spent' => wc_format_decimal($customer->get_total_spent(), 2), 'avatar_url' => $customer->get_avatar_url(), 'billing_address' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping_address' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_shipping_country())); return array('customer' => apply_filters('woocommerce_api_customer_response', $customer_data, $customer, $fields, $this->server)); }
/** * Prepare a single customer output for response. * * @param WP_User $user_data User object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response Response data. */ public function prepare_item_for_response($user_data, $request) { $customer = new WC_Customer($user_data->ID); $last_order_data = $customer->get_last_order(); $last_order = null; if ($last_order_data) { $last_order = array('id' => $last_order_data->get_id(), 'date' => wc_rest_prepare_date_response($last_order_data->get_date_created())); } $data = array('id' => $customer->get_id(), 'date_created' => wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_created())), 'date_modified' => $customer->get_date_modified() ? wc_rest_prepare_date_response(date('Y-m-d H:i:s', $customer->get_date_modified())) : null, 'email' => $customer->get_email(), 'first_name' => $customer->get_first_name(), 'last_name' => $customer->get_last_name(), 'username' => $customer->get_username(), 'last_order' => $last_order, 'orders_count' => $customer->get_order_count(), 'total_spent' => $customer->get_total_spent(), 'avatar_url' => $customer->get_avatar_url(), 'billing' => array('first_name' => $customer->get_billing_first_name(), 'last_name' => $customer->get_billing_last_name(), 'company' => $customer->get_billing_company(), 'address_1' => $customer->get_billing_address_1(), 'address_2' => $customer->get_billing_address_2(), 'city' => $customer->get_billing_city(), 'state' => $customer->get_billing_state(), 'postcode' => $customer->get_billing_postcode(), 'country' => $customer->get_billing_country(), 'email' => $customer->get_billing_email(), 'phone' => $customer->get_billing_phone()), 'shipping' => array('first_name' => $customer->get_shipping_first_name(), 'last_name' => $customer->get_shipping_last_name(), 'company' => $customer->get_shipping_company(), 'address_1' => $customer->get_shipping_address_1(), 'address_2' => $customer->get_shipping_address_2(), 'city' => $customer->get_shipping_city(), 'state' => $customer->get_shipping_state(), 'postcode' => $customer->get_shipping_postcode(), 'country' => $customer->get_shipping_country())); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links($this->prepare_links($user_data)); /** * Filter customer data returned from the REST API. * * @param WP_REST_Response $response The response object. * @param WP_User $user_data User object used to create response. * @param WP_REST_Request $request Request object. */ return apply_filters('woocommerce_rest_prepare_customer', $response, $user_data, $request); }