/** * Test creating a new customer. * @since 2.7.0 */ public function test_create_customer() { $username = '******' . time(); $customer = new WC_Customer(); $customer->set_username('testusername-' . time()); $customer->set_password('test123'); $customer->set_email('*****@*****.**'); $customer->create(); $wp_user = new WP_User($customer->get_id()); $this->assertEquals($username, $customer->get_username()); $this->assertNotEquals(0, $customer->get_id()); $this->assertEquals(strtotime($wp_user->user_registered), $customer->get_date_created()); }
/** * 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); }