Skip to main content
improved formatting and spelling
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

This code workworks 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 I 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'];
  } 
}
 
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 tryedtried to retriveretrieve the commerce_customer_profilecommerce_customer_profile field list, but I didn't find a way to generalize an action for filling every field type in the form.

This code work 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 I 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 tryed to retrive 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.

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.

Rollback to Revision 1
Source Link
Mołot
  • 21.9k
  • 8
  • 59
  • 112

This code work for me:

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

  // Check if address was already prepopulated.
  // When you click on next step and than you go back you want 
  // to see address that is saved on order. Maybe user changed it.
  if (!empty($form_state['order']->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'])) {
    return;
  }
  if (!empty($form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address'])) {
    return;
  }

  global $user;
  // Prepopulate address from user profile.
  if ( $form_id == 'commerce_checkout_form_checkout' && $user->uid) ){
    $account$user = user_load( $user->uid );
    $profile_id = $account->field_commerce[LANGUAGE_NONE][0]['profile_id'];
    $loaded_billing = commerce_customer_profile_load($profile_id$user->field_commerce['und'][0]['profile_id']);
    
    // For each field in customer profile I fill the form field with the $user value. 
    $form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address']$form['customer_profile_billing']['commerce_customer_address']['und'][0]['#address']  = $loaded_billing->commerce_customer_address[LANGUAGE_NONE][0];
    // Example with added phone field.>commerce_customer_address['und'][0];
    $form['customer_profile_billing']['field_telephone_number'][LANGUAGE_NONE][0]['value']['#default_value']$form['customer_profile_billing']['field_telephone_number']['und'][0]['value']['#default_value']  = $loaded_billing->field_telephone_number[LANGUAGE_NONE][0]['value'];>field_telephone_number['und'][0]['value'];
  } 
}
 

I triedtryed to retrive 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.

This code work for me:

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

  // Check if address was already prepopulated.
  // When you click on next step and than you go back you want 
  // to see address that is saved on order. Maybe user changed it.
  if (!empty($form_state['order']->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'])) {
    return;
  }
  if (!empty($form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address'])) {
    return;
  }

  global $user;
  // Prepopulate address from user profile.
  if ($form_id == 'commerce_checkout_form_checkout' && $user->uid) {
    $account = user_load($user->uid);
    $profile_id = $account->field_commerce[LANGUAGE_NONE][0]['profile_id'];
    $loaded_billing = commerce_customer_profile_load($profile_id);
    
    // For each field in customer profile I fill the form field with the $user value.
    $form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address']  = $loaded_billing->commerce_customer_address[LANGUAGE_NONE][0];
    // Example with added phone field.
    $form['customer_profile_billing']['field_telephone_number'][LANGUAGE_NONE][0]['value']['#default_value']  = $loaded_billing->field_telephone_number[LANGUAGE_NONE][0]['value'];
  } 
}
 

I tried to retrive 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.

This code work 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 I 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 tryed to retrive 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.

Added check if address was already prepopulated. When you click on next step and than you go back you want to see address that is saved on order. Maybe user changed it.
Source Link

This code work for me:

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

  // Check if address was already prepopulated.
  // When you click on next step and than you go back you want 
  // to see address that is saved on order. Maybe user changed it.
  if (!empty($form_state['order']->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'])) {
    return;
  }
  if (!empty($form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address'])) {
    return;
  }

  global $user;
  // Prepopulate address from user profile.
  if ($form_id == 'commerce_checkout_form_checkout' && $user->uid) {
    $account = user_load($user->uid);
    $profile_id = $account->field_commerce[LANGUAGE_NONE][0]['profile_id'];
    $loaded_billing = commerce_customer_profile_load($profile_id);
    
    // For each field in customer profile I fill the form field with the $user value.
    $form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address']  = $loaded_billing->commerce_customer_address[LANGUAGE_NONE][0];
    // Example with added phone field.
    $form['customer_profile_billing']['field_telephone_number'][LANGUAGE_NONE][0]['value']['#default_value']  = $loaded_billing->field_telephone_number[LANGUAGE_NONE][0]['value'];
  } 
}
 

I tried to retrive 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.

This code work for me:

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

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

I tried to retrive 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.

This code work for me:

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

  // Check if address was already prepopulated.
  // When you click on next step and than you go back you want 
  // to see address that is saved on order. Maybe user changed it.
  if (!empty($form_state['order']->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'])) {
    return;
  }
  if (!empty($form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address'])) {
    return;
  }

  global $user;
  // Prepopulate address from user profile.
  if ($form_id == 'commerce_checkout_form_checkout' && $user->uid) {
    $account = user_load($user->uid);
    $profile_id = $account->field_commerce[LANGUAGE_NONE][0]['profile_id'];
    $loaded_billing = commerce_customer_profile_load($profile_id);
    
    // For each field in customer profile I fill the form field with the $user value.
    $form['customer_profile_billing']['commerce_customer_address'][LANGUAGE_NONE][0]['#address']  = $loaded_billing->commerce_customer_address[LANGUAGE_NONE][0];
    // Example with added phone field.
    $form['customer_profile_billing']['field_telephone_number'][LANGUAGE_NONE][0]['value']['#default_value']  = $loaded_billing->field_telephone_number[LANGUAGE_NONE][0]['value'];
  } 
}
 

I tried to retrive 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.

Source Link
Shyghar
  • 1.1k
  • 16
  • 30
Loading