Hi,
I need to allow my users to register while in checkout. This is in core (checkout pane : login or new customer, great !), but on the registration form I don't know how to display my user fields (only username, email, password for now). These fields are required on my regular register form (and are displayed), but it would be great if settings on /admin/config/people/accounts/form-display (for register display mode) were respected on the /checkout/*/login page.
How could I make my custom field appear on the checkout registration form ?
EDIT
I create my own checkout pane, based on modules/contrib/commerce/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php, where user can login or register (but with only username and password fields). Now I can add more fields to the register form, correctly saved in user.
But how could I build, render, and display the regular user registration form (which contains taxonomy term reference, select lists) here instead of manually construct inputs ? It would be nice to be able to respect the user form as it is configured on /admin/config/people/accounts/form-display, and to be able to add/remove fields without to have to modify my custom pane.
It could use this code :
$entity = \Drupal::entityTypeManager()->getStorage('user')->create(array());
$formObject = \Drupal::entityTypeManager()
->getFormObject('user', 'register')
->setEntity($entity);
$form = \Drupal::formBuilder()->getForm($formObject);
...and inject the form into $pane_form['register'], but this is incorrect since we are in a multistep form and this leads to a form nesting (as far as I understand).
Thanks
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | 2921312-26.patch | 8.51 KB | mglaman |
Comments
Comment #2
flo81 commentedComment #3
flo81 commentedComment #4
flo81 commentedComment #5
flo81 commentedComment #6
a.dmitriiev commentedI am also interesting in displaying the full register form, as well as the login form in the same state as real form (e.g. if you user email_registration module).
Comment #7
a.dmitriiev commentedI think we should try to create a RenderElement with PluginFormInterface that will add UserLogin and UserRegister form obtained with help of the snippet in the description.
Comment #8
mehul.shah commented+1
Comment #9
mattjones86I think I would class this as a bug rather than a support request.
If your registration form has additional required fields then enabling this checkout register form lets you bypass them. I was very surprised when I looked at this and saw it didn't follow the standard registration form.
Comment #10
drupalnesia commentedI think this is Critical issue since Commerce must use Drupal standard registration form.
Imagine if Drupal Registration form contain a Required Field which will be by passed by Commerce form.
Comment #11
drupalnesia commentedIs there a workaround or example to solve this issue?
I need to add Phone Number on user registration form as required field.
Comment #12
mattjones86@drupalisme
Yes there is a work-around, we chose to use use hook_form_alter() to replace the checkout register form entirely with
#markup, which just links the user to the normal register page, and redirects them back to the checkout after registration.You could also use the same technique to add fields back into the form, but you would need to handle the submission/validating/saving of these manually.
I would be interested to hear if anyone has managed to seamlessly replace that form entirely with the
RegisterForminstead, I imagine it must be possible.Comment #13
drupalnesia commented@mattjones86 can you share the code? I face this issue and still try to alter /contrib/commerce/modules/checkout/src/Plugin/Commerce/CheckoutPane/Login.php this week.
NB: Just updated to Commerce latest version 8.x-2.11 but does not include solution for this issue.
Comment #14
mattjones86We use something like this:
Comment #15
mglamanThis is a feature request, and normal. This is a request to expand the currently functionality.
I have worked on this over in #2857157: Implement registration after guest checkout and will review adding this to the Login pane's registration functionality.
Comment #16
mglamanI need to add test coverage. But this should expose fields.
Comment #18
mglamanAdds testing for custom user fields; fixes a bug in last patch.
Comment #20
mglamanComment #21
bojanz commentedLooks like this method has a newline too many both before and after.
The docblock is also incorrect (doesn't mention custom user fields).
Code changes look good.
Comment #22
maseyuk commentedThanks the patch in #18 seems to work well.
The only thing I had to add was a weight to the submit button so it always stayed at the bottom of the form. Otherwise it wasn't possible to reliably add registration form fields after the commerce password field but before the submit button (I'm my case a newsletter signup tickbox)
Comment #23
mglamanThat makes sense. type=>action should have a weight of 50, or whatever the normal pattern is.
Comment #24
mglamanThis addresses #21 and #22.
Comment #26
mglamanGenerated a very bad patch in #24.
Comment #28
bojanz commentedThank you, mglaman!
Comment #30
sooraj sahai commentedHi Mglaman,
Thanks for your great work,its working fine now I am able to get all my accounts fields in checkout registration form.
But I am getting an error in the last step of checkout i.e checkout/%/payment page.
I am getting the following error can you please help me to fix this issue
Errorr:
"Drupal\Core\Entity\EntityStorageException: Query condition 'users_field_data.mail IN ()' cannot be empty. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 805 of /mnt/md1/support-docseducation/support_backend/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php)."
Comment #31
jglynn commentedWas there any progress on this issue? It definitely is a bug if it doesn't show the custom fields on the user account.
I'm also wondering if there is a way to choose which registration form is used if using multiple_registration module https://www.drupal.org/project/multiple_registration