Automattic\WooCommerce\Blocks\BlockTypes\OrderConfirmation
CreateAccount::process_form_post
Process posted account form.
Method of the class: CreateAccount{}
No Hooks.
Returns
\WP_Error|Int.
Usage
// protected - for code of main (parent) or child class $result = $this->process_form_post( $order );
- $order(WC_Order) (required)
- Order object.
CreateAccount::process_form_post() CreateAccount::process form post code WC 10.5.0
protected function process_form_post( $order ) {
if ( ! isset( $_POST['create-account'], $_POST['email'], $_POST['_wpnonce'] ) ) {
return 0;
}
if ( ! 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;
}