/** * 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()); }
/** * Create a single customer. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function create_item($request) { try { if (!empty($request['id'])) { throw new WC_REST_Exception('woocommerce_rest_customer_exists', __('Cannot create existing resource.', 'woocommerce'), 400); } // Sets the username. $request['username'] = !empty($request['username']) ? $request['username'] : ''; // Sets the password. $request['password'] = !empty($request['password']) ? $request['password'] : ''; // Create customer. $customer = new WC_Customer(); $customer->set_username($request['username']); $customer->set_password($request['password']); $customer->set_email($request['email']); $customer->create(); if (!$customer->get_id()) { throw new WC_REST_Exception('woocommerce_rest_cannot_create', __('This resource cannot be created.', 'woocommerce'), 400); } $this->update_customer_meta_fields($customer, $request); $customer->save(); $user_data = get_user_by('id', $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 $user_data 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, true); $request->set_param('context', 'edit'); $response = $this->prepare_item_for_response($user_data, $request); $response = rest_ensure_response($response); $response->set_status(201); $response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $customer->get_id()))); return $response; } catch (Exception $e) { return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode())); } }
/** * Creates a customer in the tests DB. */ public static function create_customer($username = '******', $password = '******', $email = '*****@*****.**') { $customer = new WC_Customer(); $customer->set_billing_country('US'); $customer->set_first_name('Justin'); $customer->set_billing_state('PA'); $customer->set_billing_postcode('19123'); $customer->set_billing_city('Philadelphia'); $customer->set_billing_address('123 South Street'); $customer->set_billing_address_2('Apt 1'); $customer->set_shipping_country('US'); $customer->set_shipping_state('PA'); $customer->set_shipping_postcode('19123'); $customer->set_shipping_city('Philadelphia'); $customer->set_shipping_address('123 South Street'); $customer->set_shipping_address_2('Apt 1'); $customer->set_username($username); $customer->set_password($password); $customer->set_email($email); $customer->create(); return $customer; }