*/ private function process_customer( \WP_REST_Request $request ) { try { if ( $this->should_create_customer_account( $request ) ) { $customer_id = $this->create_customer_account( $request['billing_address']['email'], $request['billing_address']['first_name'], $request['billing_address']['last_name'] ); // Log the customer in. wc_set_customer_auth_cookie( $customer_id ); // Associate customer with the order. $this->order->set_customer_id( $customer_id ); $this->order->save(); } } catch ( \Exception $error ) { switch ( $error->getMessage() ) { case 'registration-error-invalid-email': throw new RouteException( 'registration-error-invalid-email', __( 'Please provide a valid email address.', 'woocommerce' ), 400 ); case 'registration-error-email-exists': throw new RouteException( 'registration-error-email-exists', __( 'An account is already registered with your email address. Please log in before proceeding.', 'woocommerce' ), 400 ); } } // Persist customer address data to account. $this->order_controller->sync_customer_data_with_order( $this->order ); } /** * Check request options and store (shop) config to determine if a user account should be created as part of order * processing. * * @param \WP_REST_Request $request The current request object being handled. * @return boolean True if a new user account should be created. */ private function should_create_customer_account( \WP_REST_Request $request ) { if ( is_user_logged_in() ) { return false; } // Return false if registration is not enabled for the store. if ( false === filter_var( wc()->checkout()->is_registration_enabled(), FILTER_VALIDATE_BOOLEAN ) ) { return false; } // Return true if the store requires an account for all purchases. Note - checkbox is not displayed to shopper in this case. if ( true === filter_var( wc()->checkout()->is_registration_required(), FILTER_VALIDATE_BOOLEAN ) ) { return true; } // Create an account if requested via the endpoint. if ( true === filter_var( $request['create_account'], FILTER_VALIDATE_BOOLEAN ) ) { // User has requested an account as part of checkout processing. return true; } return false; } /** * Create a new account for a customer. * * The account is created with a generated username. The customer is sent * an email notifying them about the account and containing a link to set * their (initial) password. * * Intended as a replacement for wc_create_new_customer in WC core. * * @throws \Exception If an error is encountered when creating the user account. * * @param string $user_email The email address to use for the new account. * @param string $first_name The first name to use for the new account. * @param string $last_name The last name to use for the new account. * * @return int User id if successful */ private function create_customer_account( $user_email, $first_name, $last_name ) { if ( empty( $user_email ) || ! is_email( $user_email ) ) { throw new \Exception( 'registration-error-invalid-email' ); } if ( email_exists( $user_email ) ) { throw new \Exception( 'registration-error-email-exists' ); } $username = wc_create_new_customer_username( $user_email ); // Handle password creation. $password = wp_generate_password(); $password_generated = true; // Use WP_Error to handle registration errors. $errors = new \WP_Error(); /** * Fires before a customer account is registered. * * This hook fires before customer accounts are created and passes the form data (username, email) and an array * of errors. * * This could be used to add extra validation logic and append errors to the array. * * @internal Matches filter name in WooCommerce core. * * @param string $username Customer username. * @param string $user_email Customer email address. * @param \WP_Error $errors Error object. */ do_action( 'woocommerce_register_post', $username, $user_email, $errors ); /** * Filters registration errors before a customer account is registered. * * This hook filters registration errors. This can be used to manipulate the array of errors before * they are displayed. * * @internal Matches filter name in WooCommerce core. * * @param \WP_Error $errors Error object. * @param string $username Customer username. * @param string $user_email Customer email address. * @return \WP_Error */ $errors = apply_filters( 'woocommerce_registration_errors', $errors, $username, $user_email ); if ( is_wp_error( $errors ) && $errors->get_error_code() ) { throw new \Exception( $errors->get_error_code() ); } /** * Filters customer data before a customer account is registered. * * This hook filters customer data. It allows user data to be changed, for example, username, password, email, * first name, last name, and role. * * @param array $customer_data An array of customer (user) data. * @return array */ $new_customer_data = apply_filters( 'woocommerce_new_customer_data', array( 'user_login' => $username, 'user_pass' => $password, 'user_email' => $user_email, 'first_name' => $first_name, 'last_name' => $last_name, 'role' => 'customer', 'source' => 'store-api,', ) ); $customer_id = wp_insert_user( $new_customer_data ); if ( is_wp_error( $customer_id ) ) { throw $this->map_create_account_error( $customer_id ); } // Set account flag to remind customer to update generated password. update_user_option( $customer_id, 'default_password_nag', true, true ); /** * Fires after a customer account has been registered. * * This hook fires after customer accounts are created and passes the customer data. * * @internal Matches filter name in WooCommerce core. * * @param integer $customer_id New customer (user) ID. * @param array $new_customer_data Array of customer (user) data. * @param string $password_generated The generated password for the account. */ do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated ); return $customer_id; } /** * Convert an account creation error to an exception. * * @param \WP_Error $error An error object. * @return \Exception. */ private function map_create_account_error( \WP_Error $error ) { switch ( $error->get_error_code() ) { // WordPress core error codes. case 'empty_username': case 'invalid_username': case 'empty_email': case 'invalid_email': case 'email_exists': case 'registerfail': return new \Exception( 'woocommerce_rest_checkout_create_account_failure' ); } return new \Exception( 'woocommerce_rest_checkout_create_account_failure' ); } } accepted_args Optional. The number of arguments the function accepts. Default 1. */ protected static function add_filter( string $hook_name, $callback, int $priority = 10, int $accepted_args = 1 ): void { self::process_callback_before_hooking( $callback ); add_filter( $hook_name, $callback, $priority, $accepted_args ); } /** * Do the required processing to a callback before invoking the WordPress 'add_action' or 'add_filter' function. * * @param callable $callback The callback to process. * @return void */ protected static function process_callback_before_hooking( $callback ): void { if ( ! is_array( $callback ) || count( $callback ) < 2 ) { return; } $first_item = $callback[0]; if ( __CLASS__ === $first_item ) { static::mark_static_method_as_accessible( $callback[1] ); } elseif ( is_object( $first_item ) && get_class( $first_item ) === __CLASS__ ) { $first_item->mark_method_as_accessible( $callback[1] ); } } /** * Register a private or protected instance method of this class as externally accessible. * * @param string $method_name Method name. * @return bool True if the method has been marked as externally accessible, false if the method doesn't exist. */ protected function mark_method_as_accessible( string $method_name ): bool { // Note that an "is_callable" check would be useless here: // "is_callable" always returns true if the class implements __call. if ( method_exists( $this, $method_name ) ) { $this->_accessible_private_methods[ $method_name ] = $method_name; return true; } return false; } /** * Register a private or protected static method of this class as externally accessible. * * @param string $method_name Method name. * @return bool True if the method has been marked as externally accessible, false if the method doesn't exist. */ protected static function mark_static_method_as_accessible( string $method_name ): bool { if ( method_exists( __CLASS__, $method_name ) ) { static::$_accessible_static_private_methods[ $method_name ] = $method_name; return true; } return false; } /** * Undefined/inaccessible instance method call handler. * * @param string $name Called method name. * @param array $arguments Called method arguments. * @return mixed * @throws \Error The called instance method doesn't exist or is private/protected and not marked as externally accessible. */ public function __call( $name, $arguments ) { if ( isset( $this->_accessible_private_methods[ $name ] ) ) { return call_user_func_array( array( $this, $name ), $arguments ); } elseif ( is_callable( array( 'parent', '__call' ) ) ) { return parent::__call( $name, $arguments ); } elseif ( method_exists( $this, $name ) ) { throw new \Error( 'Call to private method ' . get_class( $this ) . '::' . $name ); } else { throw new \Error( 'Call to undefined method ' . get_class( $this ) . '::' . $name ); } } /** * Undefined/inaccessible static method call handler. * * @param string $name Called method name. * @param array $arguments Called method arguments. * @return mixed * @throws \Error The called static method doesn't exist or is private/protected and not marked as externally accessible. */ public static function __callStatic( $name, $arguments ) { if ( isset( static::$_accessible_static_private_methods[ $name ] ) ) { return call_user_func_array( array( __CLASS__, $name ), $arguments ); } elseif ( is_callable( array( 'parent', '__callStatic' ) ) ) { return parent::__callStatic( $name, $arguments ); } elseif ( 'add_action' === $name || 'add_filter' === $name ) { $proper_method_name = 'add_static_' . substr( $name, 4 ); throw new \Error( __CLASS__ . '::' . $name . " can't be called statically, did you mean '$proper_method_name'?" ); } elseif ( method_exists( __CLASS__, $name ) ) { throw new \Error( 'Call to private method ' . __CLASS__ . '::' . $name ); } else { throw new \Error( 'Call to undefined method ' . __CLASS__ . '::' . $name ); } } }