I need to autofill the billing information on checkout with Drupal commerce.

I added to drupal user a field called 'field_commerce_billing' like a reference to 'Customer profile' entity and I want to autofill the checkout with the customer profile fields values if an user is logged in.


1 Answer 1


This code works for me.

function mymodule_form_alter(&$form, &$form_state, $form_id) { 
  global $user;

  if ($form_id == 'commerce_checkout_form_checkout' && $user->uid) {
    $user = user_load($user->uid);
    $loaded_billing = commerce_customer_profile_load($user->field_commerce['und'][0]['profile_id']);
    // For each field in customer profile, fill the form field with the $user value.
    $form['customer_profile_billing']['commerce_customer_address']['und'][0]['#address'] = $loaded_billing->commerce_customer_address['und'][0];
    $form['customer_profile_billing']['field_telephone_number']['und'][0]['value']['#default_value'] = $loaded_billing->field_telephone_number['und'][0]['value'];

I tried to retrieve the commerce_customer_profile field list, but I didn't find a way to generalize an action for filling every field type in the form.

  • @Molot mind explaining what you mean here?
    – MrUpsidown
    Sep 12, 2020 at 11:48
  • @MrUpsidown The comment was for a suggested edit that made drastic changes in the code shown in this answer.
    – apaderno
    Sep 12, 2020 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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