With Commerce 2 , set default values on billing information

Created on 7 November 2017, over 7 years ago
Updated 23 December 2023, over 1 year ago

Hi,

I'm trying with no luck to prepropulate the billing information form on a Drupal Commerce 2 checkout form. I need to set default values from user fields.

As far as I understand, on this page (/checkout/*/order_information) we have a billing information form, which embeds an address form (billing information is a customer profile with an Address field).

I tried to implement hook_form_alter, but since checkout is a multistep form it turns to be more complicated than I thought. How could I set default values for the address field on this profile ?

Here is some trials I made so far :

function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  $account = \Drupal::currentUser();

  // better way to target order information form ?
  if (($form_id === 'commerce_checkout_flow_multistep_default') && ($form['#step_id'] === 'order_information')) {

    // create a billing profile so I can set it as default ?
    $profile_storage = Drupal::entityTypeManager()->getStorage('profile');
    $billing_profile = $profile_storage->create([
      'type' => 'customer',
      'uid' => $account->id()
    ]);

    // how to define an Address value and set it on $billing_profile ?
    $billing_profile->set('address', ['organization' => 'something from logged in user fields']);

    // set $billing_profile as default value ?
    $form['payment_information']['billing_information']['#default_value'] = $billing_profile;
  }
}

Any lead, advice, guidance is more than welcome

💬 Support request
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France flo81

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024