/** * 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()); }
/** * Update a single user. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function update_item($request) { try { $id = (int) $request['id']; $customer = new WC_Customer($id); if (!$customer->get_id()) { throw new WC_REST_Exception('woocommerce_rest_invalid_id', __('Invalid resource ID.', 'woocommerce'), 400); } if (!empty($request['email']) && email_exists($request['email']) && $request['email'] !== $customer->get_email()) { throw new WC_REST_Exception('woocommerce_rest_customer_invalid_email', __('Email address is invalid.', 'woocommerce'), 400); } if (!empty($request['username']) && $request['username'] !== $customer->get_username()) { throw new WC_REST_Exception('woocommerce_rest_customer_invalid_argument', __("Username isn't editable.", 'woocommerce'), 400); } // Customer email. if (isset($request['email'])) { $customer->set_email(sanitize_email($request['email'])); } // Customer password. if (isset($request['password'])) { $customer->set_password(wc_clean($request['password'])); } $this->update_customer_meta_fields($customer, $request); $customer->save(); $user_data = get_userdata($customer->get_id()); $this->update_additional_fields_for_object($user_data, $request); /** * Fires after a customer is created or updated via the REST API. * * @param WP_User $customer Data used to create the customer. * @param WP_REST_Request $request Request object. * @param boolean $creating True when creating customer, false when updating customer. */ do_action('woocommerce_rest_insert_customer', $user_data, $request, false); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($user_data, $request); $response = rest_ensure_response($response); return $response; } catch (Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * 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); }