Prepopulating Ubercart's address field with Content Profile values

This is a custom solution that uses hook_form_alter() to prepopulate the Ubercart Address field with fields from the logged in user's Content Profile.

<?php
/*
  * Implementation of hook_form_alter()
  * populate address field on ubercart checkout using 
  * user's content profile
  */
function grasmash_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'uc_cart_checkout_form') { 
    //load user and profile objects
    global $user;
    $profile = content_profile_load('employee_profile', $user->uid);

    //map source fields in content_profile to target fields on checkout form
    $field_matches = array (
      'billing_first_name' => $profile->field_first_name[0]['value'],
      'billing_last_name' => $profile->field_last_name[0]['value'],
      // 'billing_company' => NULL,
      'billing_street1' => $profile->field_employee_location[0]['street'],
      'billing_street2' => $profile->field_employee_location[0]['additional'],
      'billing_city' => $profile->field_employee_location[0]['city'],
      'billing_country' => $profile->field_employee_location[0]['country'],
      'billing_zone' => $profile->field_employee_location[0]['province_name'],
      'billing_postal_code' => $profile->field_employee_location[0]['postal_code'],
      'billing_phone' => $profile->field_employee_location[0]['phone'],
    );        

    //set default values
    foreach ($field_matches as $target_field => $source) {

      if ($source) {

        /* select list needs to be set differently
        determine appropriate target field key for source value */
        if ($target_field == 'billing_zone' || $target_field == 'billing_country') {
          foreach ($form['panes']['billing'][$target_field]['#options'] as $key => $value) {
            if ($source == $value) {
              $form['panes']['billing'][$target_field]['#value'] = $key;
            }
          }
        }

        //set values for normal text fields
        else {
          $form['panes']['billing'][$target_field]['#default_value'] = $source;
        }

      }
    }
  }
}
?>

You'll clearly need to change the mapping array to match your specific field names.

Note: If I were to do this again, I'd consider using the Ubercart Addresses module with the following plan:

  • Set Ubercart Addresses to not require address on registration
  • Set Content Profile to require an address CCK field on registration
  • create custom module with hook_form_alter()
  • have hook_form_alter() add a new submit handler for the user_registration form
  • have submit handler use values provided to CCK address fields to create a new db entry in the uc_addresses table

In reply to by Austin

Yes, i have used this similar code in drupal 6 but it did not work when implemented to drupal 7. still looking for reference article. ant body help.

thx
anjjriit
www.planet-drupal.com

If people wish to achieve links then they really should at least have the integrity to give a themed and readable reply. All comment spam does is hurt your SEO efforts and also creates useless links. The best way to make money online is to befriend other bloggers who have blogs of similar topic and hopefully generate totally targeted traffic by making a well thought out response. You never know future commenters might consider that prior to replying below me, Thanks.

I don't think this works for the "zone" field (i.e. state/prov) due to the ajax loading of state based on country selection - my guess would be that the poster is picking the state from the default country.

any instructions for how to do this in D7 much appreciated :)

i will really appreciate if somebody will advice me with some solution for D7 too :) subscribing !

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.