WC_API_Customers::edit_customer()publicWC 2.2

Edit a customer

Method of the class: WC_API_Customers{}

Return

Array|WP_Error.

Usage

$WC_API_Customers = new WC_API_Customers();
$WC_API_Customers->edit_customer( $id, $data );
$id(int) (required)
the customer ID
$data(array) (required)
-

Changelog

Since 2.2 Introduced.

WC_API_Customers::edit_customer() code WC 8.9.0

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() ) );
	}
}