I found another old thread that describe parts of this issue, but not all of it, and it was marked as fixed because the original poster worked around the issue.

I'd like to strongly argue that this issue is NOT FIXED.

As it is now, if you add a new REQUIRED field to the profile, without then dinking around in the rule configuration, the checkout explodes in spectacular fashion with a PDOException error.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'name': INSERT INTO {users} (uid, created) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1); Array ( [:db_insert_placeholder_0] => 3 [:db_insert_placeholder_1] => 1435681008 ) in drupal_write_record() (line 7316 of /...../includes/common.inc).

Here is the solution suggested in Account creation fails upon checkout, which I'm going to argue is a workaround, not a solution.

Go to admin/config/workflow/rules.
Edit the "Create a new account for an anonymous order" rule.
Edit the "Create a new entity" action.
Add something (ANYTHING) into the required field you just added.
Ideally you would enter a data selector to provide a useful default value from the commerce order.
Repeat for the "Create a new account for an anonymous order (HTML)" rule if you have one.

Why is this not a solution? Because simply adding a new, required, profile field IMMEDIATELY breaks your checkout, and you won't discover this until someone is ready to complete their order. The error occurs AFTER entering their credit card information.
"Potential for loss of sale" is an understatement.

Here are some things I've tested to determine what conditions the error occurs:
* Profile Field Required, Not captured in checkout: FAIL
* Profile Field Required, Asked for but not required in checkout: FAIL
* Profile Field Required, Required and captured in checkout: FAIL!!
* Reducing weight of the "Update the User with input from User Profile Pane" rule to 1 (so it fires before user account creation): FAIL
* Adding a Default value to the profile field configuration: defaults the field in the checkout pane, but STILL FAILS
* Tried multiple data selectors in the rule field configuration, all of which failed, probably because they reference the user account which does not yet exist.
-- "commerce-order:commerce-customer-billing:user:field-MYFIELDNAME"
-- "commerce-order:owner:field-MYFIELDNAME"

It looks like the ONLY remaining solution is to put something, ANYTHING into those rule fields, to allow the user account to be created.
The "Update the User with input from User Profile Pane" rule fires later and actually does the data copying, so it doesn't matter a whole lot what the data selectors are in "Create a new account for an anonymous order"->"Create a new entity".

I propose the following:
* If any default is set for a profile field, use that in the rule default data value, so that even if it's not shown in checkout, it'll still be saved to the user.
* If no default is set, use a "0", which should work for integer and text fields. It may not work for images, node references, or other more exotic required fields with weird validation, but it'll prevent most errors and hopefully keep checkouts from bombing.
* At minimum, if the above is somehow not possible, provide an unavoidable warning on the profile field edit pages with instructions on how to default these rule values.

I had a look at some of the rules definition files, but can't figure out where it's adding the profile fields into the rule config.
Considering I've spent an entire day trying to track this issue down and solve it for good, please give it some thought.
This issue can easily, silently, completely, disable an e-commerce site.

Comments

millenniumtree’s picture

Issue summary: View changes
rszrama’s picture

Category: Bug report » Task
Priority: Major » Normal

I understand why this is concerning, but there's not a whole lot we can do here other than suggest changes to Rules or use a completely custom action for creating user accounts during checkout completion. Drupal Commerce itself isn't really responsible for the manifestation of this behavior, so it isn't a bug in Commerce itself.

While we could accommodate it perhaps with changes to the default Rule constructed by the Checkout module, as soon as that Rule is customized and then a profile field is added afterward, it would be in the broken state without us having any control over it.

I'm open to a project task managed in this queue that drives us toward a complete solution, though. Perhaps there's some insight to be gained from the "Create an entity" action's code? Technically speaking, it shouldn't require these fields just to create a new entity - Drupal itself sure doesn't mind.

millenniumtree’s picture

I tried this both on an almost-live site, and a super basic Drupal/Commerce installation with just enough code to reproduce the issue, and it happens out of the box.

You're right - I've saved many nodes/entities programmatically with blank 'required' fields, and they save fine, so something along the way is doing some validation before the entity is saved out.

If it's is a core rules issue, then maybe we can do some sort of 'automatic workaround' using a hook, or at the very least, check for the issue and alert the store owner to update the rule? Is there a system for store management alerts? Certainly the "Status Report" would be a place to start.

Thanks for your patience Ryan.

rszrama’s picture

Mmm, yeah, some sort of warning would be doable, maybe even on the Field UI itself for managing customer profiles. That would give an immediate notice that the admin needed to update the related rule at the same time.