0

My customers are required to provide an address at checkout (Shipping information pane). They can also create an account after checkout is complete (Guest registration after checkout pane).

But the Customer information / Address form is empty:

enter image description here

Is this normal behavior? Is it not supposed to pre-fill the form with the shipping info the customer provided during checkout? I couldn't find any option in Commerce settings.

Any idea on how I could make this happen? Do I need to implement it by myself?

  • Drupal core: 8.9.3
  • Commerce: 8.x-2.20
0
0

Not sure this would be the recommended way but I got it working using hook_form_alter. Customers start by filling their shipping address at step 1 (required) and on complete step, I grab this shipping address and copy it to the customer_information address field on guest registration, so the customers don't need to fill the same address again. In case the customer address would be different, it can be modified at this step.

/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Commerce checkout form
  if ($form_id === 'commerce_checkout_flow_multistep_default') {

    // Multistep "complete" step
    if ($form['#step_id'] === 'complete') {

      // Get shipping address and copy it to customer profile address
      $profiles = $form_state->getFormObject()->getOrder()->collectProfiles();
      $shipping_address = $profiles['shipping']->address->getValue();
      $form['completion_register']['customer_profiles']['widget'][0]['entity']['address']['widget'][0]['address']['#default_value'] = reset($shipping_address);
    }
  }
}
5
  • Hmm, I've never seen this "Customer information" fieldset on the registration form. Is that a custom field you've added to your user accounts by any chance? – Ryan Szrama Sep 16 '20 at 11:08
  • Ahh, you know why ... this field is disabled on the user "Registration" form mode by default. You must have re-enabled it somehow, but I'm not sure you really needed to. – Ryan Szrama Sep 16 '20 at 11:14
  • @RyanSzrama Do you mean in admin/config/people/accounts/form-display/register? I can't see that field or I am on the wrong form. – MrUpsidown Sep 19 '20 at 0:19
  • I can't reproduce your issue in my local env't; my checkout pane only shows the fields it ought to based on the form mode. I'd ensure you're running the latest version of Drupal Commerce and possibly resave that form to ensure there isn't some mismatch between default settings or something. – Ryan Szrama Sep 22 '20 at 15:46
  • 1
    @RyanSzrama That actually helped. I had to edit the Customer profile type in admin/config/people/profile-types/manage/customer and check Include in user registration form, save the form, uncheck, save again, and the form was gone. I can't tell how that setting got messed up though. – MrUpsidown Sep 23 '20 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.