If an admin user is creating a new order for a customer believed to be new and it turns out the customer's email already exists, a nasty "Integrity constraint violation" is displayed. It would be nice to get a friendlier "customer already exists" error message or even automatic assignment of that existing customer to the new order.

If no change is made, all admin users will need to be trained to always assume customers are existing and start by searching for the existing customer. If the customer doesn't exist, an error message saying, "There are no entities matching email_address" appears. Then the admin user would proceed by creating the new customer.

Issue fork commerce-2868368

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lisastreeter created an issue. See original summary.

czigor’s picture

The last submitted patch, 2: commerce-2868368-new_customer-2-ONLY_TEST.patch, failed testing. View results

ericchew’s picture

+      // The mail is used for both the new user's name and mail. Only the name
+      // must be unique.
+      if (user_load_by_name($values['mail'])) {
+        $form_state->setError($customer_form, 'A user with this name exists already.');
+      }

While name/mail fields are both set to mail value in the Commerce form, that is not guaranteed to be true if the User was created a different way (such as the standard User forms). I tested the patch on my site and I was able to create a duplicate user. Here is a patch that includes a check for users that have the same email address. I also updated the error text to match the message given by core when validating for unique email on User.

Adding related issue from Core regarding base field constraints.

s.messaris’s picture

This problem still exists today, closing https://www.drupal.org/project/commerce/issues/3166998 as a duplicate of this, but I was using the patch there in production for 2 months without any problems.

The patch in #4 here triggers some deprecation warnings in D9 and just checks the mail, I added some changes from the patch I wrote in 3166998 to fix the warnings and do a more generic validation with $user->validate(), and kept the test that should still apply and the usage of #element_validate that is better than what I did there.

If someone can review this I would much appreciate it.

s.messaris’s picture

s.messaris’s picture

Can someone please review this?

Marios Anagnostopoulos’s picture

#5 works for me.

This problem seems to exist like... for ever, to the point actually that we have all gotten used to it.
I believe this is good to go and will be changing the status to rtbc.

Marios Anagnostopoulos’s picture

Status: Needs review » Reviewed & tested by the community
s.messaris’s picture

Hello, what is needed to have this committed?

jsacksick’s picture

Status: Reviewed & tested by the community » Needs work

Instead of using an #element_validate on the fieldset, let's just implement the validateForm method in OrderAddForm and OrderReassignForm.

And just call:

    $this->validateCustomerForm($form, $form_state);

Also, the other methods aren't static, so let's mimic that.

Regarding the functional javascript test, let's reuse OrderAdminTest (Either by updating testCreateOrder), or just add an additional method there (e.g testCreateOrderNewCustomer()), it's probably easier/faster to introduce a new method.

Let's also skip createUserObject. After validating the user, you can put it in form state, and retrieve it from submitForm.

So:

$form_state->set('customer', $user)... 

And from submitCustomerForm do $form_state->get('customer');.

marchuk.vitaliy made their first commit to this issue’s fork.

Rishi Kulshreshtha’s picture

Assigned: Unassigned » Rishi Kulshreshtha
jsacksick’s picture

@ Rishi Kulshreshtha: Any reason to assign this to you? @marchuk.vitaliy opened a PR that has passing tests?

Rishi Kulshreshtha’s picture

Assigned: Rishi Kulshreshtha » Unassigned
Status: Needs work » Needs review
FileSize
5.49 KB
5.65 KB

@jsacksick, actually, I found the status in needs work & hence thought of checking the same. I've tried to implement the suggestions provided by you above.

I didn't actually notice the MR in the first place as the ticket already contained some patches, my bad. But, after your message I reviewed the MR provided by @marchuk.vitaliy seems to tackle all your suggestions as well, but I can see there the use of waitForAjaxToFinish() which is already deprecated & I've tried to create the user using DI instead of directly using User::create

Attaching the updated patch.

Status: Needs review » Needs work

The last submitted patch, 16: new-customer-with-same-email-2868368-16.patch, failed testing. View results

Rishi Kulshreshtha’s picture

Status: Needs work » Needs review
FileSize
5.52 KB
635 bytes

Wow, \Drupal\Tests\user\Traits\UserCreationTrait::createUser's signature is slightly different than \Drupal\KernelTests\Core\Entity\EntityKernelTestBase::createUser :)

Nigel Cunningham’s picture

The original behaviour of the form allows the creation of a user by entering an email address only. With the version of the patch in comment 18, all validations on a newly created user are applied, potentially preventing the creation of the user because of constraints related to fields that aren't on this form.

I'm attaching a version of the patch that only applies the email address constraint, to keep the behaviour the same as it was originally.

  • jsacksick committed 7ae3356 on 8.x-2.x
    Issue #2868368 by Rishi Kulshreshtha, ericchew, czigor, marchuk.vitaliy...

  • jsacksick committed f6e559e on 3.0.x
    Issue #2868368 by Rishi Kulshreshtha, ericchew, czigor, marchuk.vitaliy...
jsacksick’s picture

Status: Needs review » Fixed

Committed! Thanks everyone!!

Status: Fixed » Closed (fixed)

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