set_email() public method

Set customer's email.
Since: 2.7.0
public set_email ( string $value )
$value string
 /**
  * Edit a customer
  *
  * @since 2.2
  * @param int $id the customer ID
  * @param array $data
  * @return array
  */
 public function edit_customer($id, $data)
 {
     try {
         if (!isset($data['customer'])) {
             throw new WC_API_Exception('woocommerce_api_missing_customer_data', sprintf(__('No %1$s data specified to edit %1$s', 'woocommerce'), 'customer'), 400);
         }
         $data = $data['customer'];
         // Validate the customer ID.
         $id = $this->validate_request($id, 'customer', 'edit');
         // Return the validate error.
         if (is_wp_error($id)) {
             throw new WC_API_Exception($id->get_error_code(), $id->get_error_message(), 400);
         }
         $data = apply_filters('woocommerce_api_edit_customer_data', $data, $this);
         $customer = new WC_Customer($id);
         // Customer email.
         if (isset($data['email'])) {
             $customer->set_email($data['email']);
         }
         // Customer password.
         if (isset($data['password'])) {
             $customer->set_password($data['password']);
         }
         // Update customer data.
         $this->update_customer_data($customer->get_id(), $data, $customer);
         $customer->save();
         do_action('woocommerce_api_edit_customer', $customer->get_id(), $data);
         return $this->get_customer($customer->get_id());
     } catch (Exception $e) {
         return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
     }
 }
Ejemplo n.º 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());
 }
 /**
  * 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()));
     }
 }
 /**
  * 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;
 }