! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ?? '' ) ), 'wc_create_account' ) ) { return new \WP_Error( 'invalid_nonce', __( 'Unable to create account. Please try again.', 'woocommerce' ) ); } $user_email = sanitize_email( wp_unslash( $_POST['email'] ) ); // Does order already have user? if ( $order->get_customer_id() ) { return new \WP_Error( 'order_already_has_user', __( 'This order is already linked to a user account.', 'woocommerce' ) ); } // Check given details match the current viewed order. if ( $order->get_billing_email() !== $user_email ) { return new \WP_Error( 'email_mismatch', __( 'The email address provided does not match the email address on this order.', 'woocommerce' ) ); } $generate_password = filter_var( get_option( 'woocommerce_registration_generate_password', 'no' ), FILTER_VALIDATE_BOOLEAN ); if ( $generate_password ) { $password = ''; // Will be generated by wc_create_new_customer. } else { $password = wp_unslash( $_POST['password'] ?? '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( empty( $password ) || strlen( $password ) < 8 ) { return new \WP_Error( 'password_too_short', __( 'Password must be at least 8 characters.', 'woocommerce' ) ); } } $customer_id = wc_create_new_customer( $user_email, '', $password, [ 'first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), 'source' => 'delayed-account-creation', ] ); if ( is_wp_error( $customer_id ) ) { return $customer_id; } // Associate customer with the order. $order->set_customer_id( $customer_id ); $order->save(); // Associate addresses from the order with the customer. $order_controller = new OrderController(); $order_controller->sync_customer_data_with_order( $order ); // Set the customer auth cookie. wc_set_customer_auth_cookie( $customer_id ); return $customer_id; } /** * This renders the content of the block within the wrapper. * * @param \WC_Order $order Order object. * @param string|false $permission If the current user can view the order details or not. * @param array $attributes Block attributes. * @param string $content Original block content. * @return string */ protected function render_content( $order, $permission = false, $attributes = [], $content = '' ) { if ( ! $permission || ! $this->is_feature_enabled() ) { return ''; } // Check registration is possible for this order/customer, and if not, return early. if ( is_user_logged_in() || email_exists( $order->get_billing_email() ) ) { return ''; } $result = $this->process_form_post( $order ); $notice = ''; if ( is_wp_error( $result ) ) { $notice = wc_print_notice( $result->get_error_message(), 'error', [], true ); } elseif ( $result ) { return $this->render_confirmation(); } $processor = new \WP_HTML_Tag_Processor( $content . '
' . $notice . '
' . '
' ); if ( ! $processor->next_tag( array( 'class_name' => 'wp-block-woocommerce-order-confirmation-create-account' ) ) ) { return $content; } $processor->set_attribute( 'class', '' ); $processor->set_attribute( 'style', '' ); $processor->add_class( 'wc-block-order-confirmation-create-account-content' ); if ( ! $processor->next_tag( array( 'class_name' => 'wc-block-order-confirmation-create-account-form' ) ) ) { return $content; } $processor->set_attribute( 'data-customer-email', $order->get_billing_email() ); $processor->set_attribute( 'data-nonce-token', wp_create_nonce( 'wc_create_account' ) ); if ( ! empty( $attributes['hasDarkControls'] ) ) { $processor->add_class( 'has-dark-controls' ); } return $processor->get_updated_html(); } /** * Render the block when an account has been registered. * * @return string */ protected function render_confirmation() { $content = '
'; $content .= '

' . esc_html__( 'Your account has been successfully created', 'woocommerce' ) . '

'; $content .= '

' . sprintf( /* translators: 1: link to my account page, 2: link to shipping and billing addresses, 3: link to account details, 4: closing tag */ esc_html__( 'You can now %1$sview your recent orders%4$s, manage your %2$sshipping and billing addresses%4$s, and edit your %3$spassword and account details%4$s.', 'woocommerce' ), '', '', '', '' ) . '

'; $content .= '
'; return $content; } /** * Extra data passed through from server to client for block. * * @param array $attributes Any attributes that currently are available from the block. * Note, this will be empty in the editor context when the block is * not in the post content on editor load. */ protected function enqueue_data( array $attributes = [] ) { parent::enqueue_data( $attributes ); $this->asset_data_registry->add( 'delayedAccountCreationEnabled', $this->is_feature_enabled() ); $this->asset_data_registry->add( 'registrationGeneratePassword', filter_var( get_option( 'woocommerce_registration_generate_password' ), FILTER_VALIDATE_BOOLEAN ) ); } }