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

Comments

flo81 created an issue. See original summary.

flo81’s picture

Title: User fields on chackout registration form » Display custom user fields on checkout registration form
flo81’s picture

Issue summary: View changes
flo81’s picture

Issue summary: View changes
flo81’s picture

Issue summary: View changes
a.dmitriiev’s picture

I 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).

a.dmitriiev’s picture

I 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.

mehul.shah’s picture

+1

mattjones86’s picture

Category: Support request » Bug report

I 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.

drupalnesia’s picture

Priority: Normal » Critical

I 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.

drupalnesia’s picture

Is there a workaround or example to solve this issue?
I need to add Phone Number on user registration form as required field.

mattjones86’s picture

@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 RegisterForm instead, I imagine it must be possible.

drupalnesia’s picture

Version: 8.x-2.x-dev » 8.x-2.11

@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.

mattjones86’s picture

We use something like this:


function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  if ($form_id === 'commerce_checkout_flow_multistep_default' && $form['#step_id'] === 'login') {
    $form['login']['returning_customer']['forgot_password'] = [
      '#type' => 'link',
      '#url' => Url::fromRoute('user.pass'),
      '#title' => t('Forgotten password?'),
      '#attributes' => [
        'class' => ['button']
      ],
    ];
    $form['login']['register'] = [
      '#type' => 'fieldset',
      '#title' => $form['login']['register']['#title'],
      '#attributes' => $form['login']['register']['#attributes'],
    ];
    $form['login']['register']['register'] = [
      '#type' => 'markup',
      '#markup' => Markup::create(t('<p>In order to proceed to checkout you must have an active account with us. Please <a href="@register">click here</a> to register for a website account.</p>', ['@register' => Url::fromRoute('user.register')->toString()])),
    ];
    $form['login']['register']['register_link'] = [
      '#type' => 'link',
      '#url' => Url::fromRoute('user.register'),
      '#title' => t('Register now'),
      '#attributes' => [
        'class' => ['button']
      ],
    ];
  }
}
mglaman’s picture

Version: 8.x-2.11 » 8.x-2.x-dev
Category: Bug report » Feature request
Priority: Critical » Normal

This 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.

mglaman’s picture

Status: Active » Needs review
StatusFileSize
new2.65 KB

I need to add test coverage. But this should expose fields.

Status: Needs review » Needs work

The last submitted patch, 16: 2921312-12.patch, failed testing. View results

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new8.38 KB

Adds testing for custom user fields; fixes a bug in last patch.

Status: Needs review » Needs work

The last submitted patch, 18: 2921312-18.patch, failed testing. View results

mglaman’s picture

Status: Needs work » Needs review
bojanz’s picture

Status: Needs review » Needs work
+
+  /**
+   * Tests that you can register from the checkout pane.
+   */
+  public function testRegisterOrderCheckoutWithCustomUserFields() {

Looks 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.

maseyuk’s picture

Thanks 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)

mglaman’s picture

That makes sense. type=>action should have a weight of 50, or whatever the normal pattern is.

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new59.37 KB

This addresses #21 and #22.

Status: Needs review » Needs work

The last submitted patch, 24: 2921312-24.patch, failed testing. View results

mglaman’s picture

Status: Needs work » Needs review
StatusFileSize
new8.51 KB

Generated a very bad patch in #24.

  • bojanz committed 94c17ef on 8.x-2.x authored by mglaman
    Issue #2921312 by mglaman: Display custom user fields on checkout...
bojanz’s picture

Status: Needs review » Fixed

Thank you, mglaman!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

sooraj sahai’s picture

Hi 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)."

jglynn’s picture

Was 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