Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Prepopulating Ubercart's address field with Content Profile values

Parent Feed: 

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.

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
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web