set_username() 공개 메소드

Set customer's username.
부터: 2.7.0
public set_username ( string $username )
$username string
 /**
  * Create a customer
  *
  * @since 2.2
  * @param array $data
  * @return array
  */
 public function create_customer($data)
 {
     try {
         if (!isset($data['customer'])) {
             throw new WC_API_Exception('woocommerce_api_missing_customer_data', sprintf(__('No %1$s data specified to create %1$s', 'woocommerce'), 'customer'), 400);
         }
         $data = $data['customer'];
         // Checks with can create new users.
         if (!current_user_can('create_users')) {
             throw new WC_API_Exception('woocommerce_api_user_cannot_create_customer', __('You do not have permission to create this customer', 'woocommerce'), 401);
         }
         $data = apply_filters('woocommerce_api_create_customer_data', $data, $this);
         // Checks with the email is missing.
         if (!isset($data['email'])) {
             throw new WC_API_Exception('woocommerce_api_missing_customer_email', sprintf(__('Missing parameter %s', 'woocommerce'), 'email'), 400);
         }
         // Create customer.
         $customer = new WC_Customer();
         $customer->set_username(!empty($data['username']) ? $data['username'] : '');
         $customer->set_password(!empty($data['password']) ? $data['password'] : '');
         $customer->set_email($data['email']);
         $customer->save();
         if (!$customer->get_id()) {
             throw new WC_API_Exception('woocommerce_api_user_cannot_create_customer', __('This resource cannot be created.', 'woocommerce'), 400);
         }
         // Added customer data.
         $this->update_customer_data($customer->get_id(), $data, $customer);
         $customer->save();
         do_action('woocommerce_api_create_customer', $customer->get_id(), $data);
         $this->server->send_status(201);
         return $this->get_customer($customer->get_id());
     } catch (Exception $e) {
         return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
     }
 }
예제 #2
0
 /**
  * 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;
 }