- Merge request !10Issue #2921585: With Commerce 2 , set default values on billing information → (Closed) created by darrenwh
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
Fixed
1.0
Code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.