17 replies [Last post]
benwei's picture
Offline
Getting busy with the Ubercode.
Joined: 09/29/2007
Juice: 47
Was this information Helpful?

Hello everyone,

I just recently discovered Ubercart and have been very impressed. I am working on a site that integrates with a legacy member database that stores all kinds of information about each member and associates it with their Drupal accounts, and I would like to be able to pre-populate the shipping address fields on the checkout screen using this data, but I'm having some trouble doing so.

Here's what I've got:

function myorg_form_alter($form_id, &$form) {
  if($form_id == 'uc_cart_checkout_form') {
    global $user;
    $user = user_load(array('uid'=>$user->uid)); // Load user data from legacy system
    if(!$user->myorg_id)
      return;
    if($user->myorg_type == 'member') {
      $dpane = $form['panes']['delivery'];
      $dpane['delivery_first_name']['#default_value'] = $user->first_name;     
      $dpane['delivery_last_name']['#default_value'] = $user->last_name;
      $dpane['delivery_street1']['#default_value'] = $user->street_address;
      $dpane['delivery_city']['#default_value'] = $user->city;
    }
  }
}

However, it's not working, the default values are never set. I can print them out as variables later in the function, and they are there correctly, but they somehow don't make it to the final form display.

Does anyone know why?

Thanks,
Ben

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15453
Re: Pre-populating the shipping address fields using hook_form_a

I'm not entirely sure on this w/o a quick test, but does the fact that &$form is a reference have anything to do with it? Have you tried this code without using your $dpane variable? Or would it work if you still used $dpane but used &= instead of =?

benwei's picture
Offline
Getting busy with the Ubercode.
Joined: 09/29/2007
Juice: 47
Re: Re: Pre-populating the shipping address fields using hook_fo

Thanks for the suggestions, Ryan. You are right about using $dpane, but not using it still doesn't fix the problem.

As a test, I tried altering the title of the first name field like this:

$form['panes']['delivery']['delivery_first_name']['#title'] = "TESTING 123";

... and that works just fine. But if I replace '#title' with '#default_value', nothing. I will keep looking.

Oh, and sorry for posting this in here -- looking more at the other posts in this forum, this probably is the wrong location, I was thinking it was similar to the 'Module Development' forum on drupal.org. Doh.

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15453
Re: Re: Re: Pre-populating the shipping address fields using hoo

Nah, this is totally the forum to do it in. Smiling

(Honestly, I don't ever really pay attention to what forum stuff gets posted in since I try to read everything that gets put up here! It's becoming less possible as we get bigger, and I'm sure it will be impossible once we hit 1.0, but it's been great for development. Eye-wink)

Now honestly, I'd expect it to work with default values just fine... I'm using #default_value to populate the panes w/ data when a user goes back from the review screen to change some of their information. Without access to a test environment, it's kinda got me stumped. Shocked

Lyle's picture
Offline
AdministratoreLiTe!
Joined: 08/07/2007
Juice: 6855
Re: Re: Re: Pre-populating the shipping address fields using hoo

I was going to suggest putting $dpane back into $form like so:

<?php
 
// ... set default values in $dpane ...
 
$form['panes']['delivery'] = $dpane;
?>

but if changing the $form variable directly didn't help, I don't have much confidence in this idea.

There may be another module with a hook_form_alter() that's overwriting what your module is doing.

StephenGWills's picture
Offline
Uber DonorBug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.Not Kulvik
Joined: 08/07/2007
Juice: 414
Re: Re: Re: Re: Pre-populating the shipping address fields using

soooooo.... am I looking at the beginning of a module to sync customer information with a legacy ERP/CRM system? *veg*. That's on the adgenda sometime after omniture is breathing right.

I can't wait!

pahariwalla's picture
Offline
Joined: 03/17/2009
Juice: 2
Here's how I did it with D6.x

using // $Id: uc_cart_checkout_pane.inc,v 1.4.2.11 2008/10/07 15:47:57 rszrama Exp $

My solution uses hook_form_alter to create a new entry in the UC Address Selector box. It usually just contains addresses from previous orders, but now I've made it also include an address that I'm getting from the user's profile.

First, you need a function to get the address you want and return it in an array that UC will recognize. So, in your custom module, create a function that looks like the one below. Of course you'll need to replace the source of your data as per your requirements. The important part is that the associative keys in the array that your function returns look exactly like this one


function mymodule_get_user_profile_address_for_uc($uid) {
  if ($uid == 0) {
    return NULL;
  }

  // getting address data that is stored in users.data
  $sql = db_query("SELECT data FROM {users} where uid = %d",$uid);

  if (db_num_rows($sql) > 0) {

    $row = db_fetch_object($sql);

    $data=unserialize($row->data);

  }
 
  //build the array
  // make sure that ALL of these elements are in your array, and use empty strings, not NULL for fields you aren't using.
  $address= array(
    'first_name' =>   $data['firstname'],
    'last_name' =>   $data['lastname'],
    'phone' => $data['phone1'],
    'company' => '', // our user data does not have a field for Company. 
    'street1' =>   $data['address1'],
    'street2' =>   $data['address2'],
    'city' =>   $data['city'],
    'zone' =>   $data['state'],
    'postal_code' => $data['zipcode'],
    'country ' =>   $data['country'],
    );

  return $address;
}

Now, in order to get it into the Billing Address pane in the checkout form, I do this in form_alter:

function mymodule_form_alter($form_id, &$form) {
  if ($form_id=='uc_cart_checkout_form') {
    global $user;
    $profile_address = mymodule_get_user_profile_address_for_uc($user->uid);
    $label = t('From User Profile');
    $form['panes']['billing']['billing_address_select']['#options'][drupal_to_js($profile_address)] = $label;
    $form['panes']['billing']['billing_address_select']['#default_value'] = $label;
    // you don't need to set the #default_value .. I just needed to because that's what my client wanted
  }
}
majnoona@drupal.org's picture
Offline
Joined: 07/02/2009
Juice: 90
Drupal 6 hook_form_alter

Here's what worked for me-- I named my module "uc_address_profiles" and used profile_location to deal with state/country info:

<?php
function uc_address_profiles_get_user_profile_address_for_uc($user) {
  if (
$user->uid == 0) {
    return
NULL;
  }

 

profile_load_profile($user);

 

//build the array
  // make sure that ALL of these elements are in your array, and use empty strings, not NULL for fields you aren't using.
  // double check that you are using the fieldnames you assigned in /admin/user/profiles
 
$address= array(
   
'first_name' =>   $user->profile_firstname,
   
'last_name' =>   $user->profile_lastname,
   
'phone' => '',
   
'company' => '',
   
'street1' =>   '',
   
'street2' =>  '',
   
'city' =>   '',
   
'zone' =>   $user->profile_state,
   
'postal_code' => '',
   
'country ' =>   $user->profile_country,
    );

  return

$address;
}
function
uc_address_profiles_form_alter(&$form, $form_state, $form_id) {
  if (
$form_id=='uc_cart_checkout_form') {
    global
$user;
   
$profile_address = uc_address_profiles_get_user_profile_address_for_uc($user);
   
$label = t('From User Profile');
   
$form['panes']['billing']['billing_address_select']['#options'][drupal_to_js($profile_address)] = $label;
   
$form['panes']['billing']['billing_address_select']['#default_value'] = $label;
  }
}
?>
canajun's picture
Offline
Joined: 12/10/2009
Juice: 2
Getting Mannona's solution to work
majnoona@drupal.org wrote:

Here's what worked for me-- I named my module "uc_address_profiles" and used profile_location to deal with state/country info:

<?php
function uc_address_profiles_get_user_profile_address_for_uc($user) {
  if (
$user->uid == 0) {
    return
NULL;
  }

 

profile_load_profile($user);

 

//build the array
  // make sure that ALL of these elements are in your array, and use empty strings, not NULL for fields you aren't using.
  // double check that you are using the fieldnames you assigned in /admin/user/profiles
 
$address= array(
   
'first_name' =>   $user->profile_firstname,
   
'last_name' =>   $user->profile_lastname,
   
'phone' => '',
   
'company' => '',
   
'street1' =>   '',
   
'street2' =>  '',
   
'city' =>   '',
   
'zone' =>   $user->profile_state,
   
'postal_code' => '',
   
'country ' =>   $user->profile_country,
    );

  return

$address;
}
function
uc_address_profiles_form_alter(&$form, $form_state, $form_id) {
  if (
$form_id=='uc_cart_checkout_form') {
    global
$user;
   
$profile_address = uc_address_profiles_get_user_profile_address_for_uc($user);
   
$label = t('From User Profile');
   
$form['panes']['billing']['billing_address_select']['#options'][drupal_to_js($profile_address)] = $label;
   
$form['panes']['billing']['billing_address_select']['#default_value'] = $label;
  }
}
?>

Can anyone assist me in getting this solution to work? I tried adding it as a module, but with my limited experience, was unable to make it work.

Any help would be appreciated.

Bob

FatGuyLaughing's picture
Offline
Joined: 10/30/2009
Juice: 2
benwei wrote: Hello
benwei wrote:

Hello everyone,

I just recently discovered Ubercart and have been very impressed. I am working on a site that integrates with a legacy member database that stores all kinds of information about each member and associates it with their Drupal accounts, and I would like to be able to pre-populate the shipping address fields on the checkout screen using this data, but I'm having some trouble doing so.

Here's what I've got:

function myorg_form_alter($form_id, &$form) {
  if($form_id == 'uc_cart_checkout_form') {
    global $user;
    $user = user_load(array('uid'=>$user->uid)); // Load user data from legacy system
    if(!$user->myorg_id)
      return;
    if($user->myorg_type == 'member') {
      $dpane = $form['panes']['delivery'];
      $dpane['delivery_first_name']['#default_value'] = $user->first_name;     
      $dpane['delivery_last_name']['#default_value'] = $user->last_name;
      $dpane['delivery_street1']['#default_value'] = $user->street_address;
      $dpane['delivery_city']['#default_value'] = $user->city;
    }
  }
}

However, it's not working, the default values are never set. I can print them out as variables later in the function, and they are there correctly, but they somehow don't make it to the final form display.

Does anyone know why?

Thanks,
Ben

Hey Ben I actually have this working with one small modification of your current code. Actually it's just one character. Sticking out tongue I am currently needing to do the same thing which is auto populate the billing or shipping information with the information that was provided from the user in their user profile. So lets dig in.

First off, I'm using Drupal 6. So my answer is for Drupal 6.

For some reason if you want to edit the default_value of a field you have to pass it by reference, I know you're saying well that's what hook_form_alter does. Which you are right. However, for some reason you have to do it again. So here is my code that works.

/*
* Implementation of hook_form_alter().
*/
function uc_checkout_pane_form_alter(&$form, $form_state, $form_id){
  /*
   * Global $user to get the uid of the current user. Then load that user
   * into the variable $account.
   */
  global $user;
  $account = user_load(array('uid' => $user->uid));
 
  /*
   * Run a switch statement on the field profile_country that has been
   * loaded in the $account variable. Depending on the value of
   * profile_country a numerical value will be assigned to the variable
   * $country.
   */
  switch ($account->profile_country) {
    case 'Canada':
      $country = 124;
      break;
   
    case 'United States':
      $country = 840;
      break;
  }
 
  /*
   * Run a switch statement on the $form_id. In the case of ubercarts
   * checkout form auto populate the billing pane information with the
   * information provided by the user in the billing information section
   * of their profile.
   */
  switch ($form_id) {
    case 'uc_cart_checkout_form':
      $billing_pane = &$form['panes']['billing'];
      $billing_pane['billing_first_name']['#default_value'] = $account->profile_first_name;
      $billing_pane['billing_last_name']['#default_value'] = $account->profile_last_name;
      $billing_pane['billing_company']['#default_value'] = $account->profile_company;
      $billing_pane['billing_street1']['#default_value'] = $account->profile_street_address;
      $billing_pane['billing_city']['#default_value'] = $account->profile_city;
      $billing_pane['billing_country']['#default_value'] = $country;
      $billing_pane['billing_zone']['#default_value'] = $account->profile_state_province;
      $billing_pane['billing_postal_code']['#default_value'] = $account->profile_postal_code;
      $billing_pane['billing_phone']['#default_value'] = $account->profile_phone_number;
      break;
  }
}

Notice that the main difference between yours and mine is simply adding the '&' sign to the $form when assigning it to the new variable. So your statement should look like this.

$dpane = &$form['panes']['delivery'];

Not sure why this works, but if someone does please elaborate as to why it does.

Hope that helps!
FatGuyLaughing (aka Fatty)

yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 299
deiivery_zone?

what is the right way to get the state/provience/zone to have a default value?

This is not working....

<?php
/**
* @file
* This is a helper module to fill in the address in Ubercart order forms with
* information from the content profile.
* See: <a href="http://www.ubercart.org/forum/development/900/pre_populating_shipping_address_fields_using_hook_form_alter_and_default_value
" title="http://www.ubercart.org/forum/development/900/pre_populating_shipping_address_fields_using_hook_form_alter_and_default_value
">http://www.ubercart.org/forum/development/900/pre_populating_shipping_ad...</a> */

/*
* Implementation of hook_form_alter().
*/

function uc_address_contentprofile_form_alter(&$form, $form_state, $form_id){
  switch (
$form_id) {
    case
'uc_order_edit_form':
     
// use dpm to see the form array
     
$mycustid=$form['customer']['uid']['#default_value'];
     
$custinfo=content_profile_load('personalinformation', $mycustid);
     
$form['ship_to']['delivery_first_name']['#default_value']=$custinfo->field_firstname[0]['value'];
     
$form['ship_to']['delivery_last_name']['#default_value']=$custinfo->field_lastname[0]['value'];
     
$form['ship_to']['delivery_phone']['#default_value']=$custinfo->field_phone_adminview[0]['value'];

     

$form['ship_to']['delivery_company']['#title']="Group";

     

$custloc=location_load_location($custinfo->field_homeaddress_adminview[0]['lid']);
     
$form['ship_to']['delivery_street1']['#default_value']=$custloc['street'];
     
$form['ship_to']['delivery_street2']['#default_value']=$custloc['additional'];
     
$form['ship_to']['delivery_city']['#default_value']=$custloc['city'];
     
$form['ship_to']['delivery_postal_code']['#default_value']=$custloc['postal_code'];

      switch (

$custloc['province_name']) {
        case
"Illinois":
         
$form['ship_to']['delivery_zone']['#default_value']=23;
          break;
        case
"Indiana":
         
$form['ship_to']['delivery_zone']['#default_value']=24;
          break;
        case
"Michigan":
         
$form['ship_to']['delivery_zone']['#default_value']=32;
          break;
        case
"Minnesota":
         
$form['ship_to']['delivery_zone']['#default_value']=34;
          break;
        case
"Wisconsin":
         
$form['ship_to']['delivery_zone']['#default_value']=64;
          break;
      }
         
$form['ship_to']['delivery_zone']['#default_value']=intval(32);
         
$form['ship_to']['delivery_postal_code']['#default_value']=intval(32);

     

dpm($form);
     
dpm($custloc);
      return
dpm($custinfo);
      break;
  }
}
?>
yesct@drupal.org's picture
Offline
Uber Donor
Joined: 11/18/2008
Juice: 299
Re: deiivery_zone?

AH. you know. I think I have the code right, it's just while testing it, doing a page reload is not enough to reset the drop down to the default value, I had to test by doing a new order.

majnoona@drupal.org's picture
Offline
Joined: 07/02/2009
Juice: 90
Re: deiivery_zone?

Is there really no algorithmic way to map the profile zone & country to the ubercart values? Like selecting the new ID based on name or iso code or something? There's no way I can do a case block to cover all the countries/provinces/zones in the world and our site has customers all over the place...

My client's been complaining about this for ages (especially as it trigger the bug in uc_coupon (http://drupal.org/node/565628) when users fail to select a state).

Sorry to grumble -- ubercart is still the best!

Bärlina's picture
Offline
Joined: 01/14/2011
Juice: 11
Hello, I've tried to

Hello,
I've tried to prepopulate the delivery pane fields with my custom profile fields but it doesn't work for me.

Here's what I have put in a new adress_profile.module

<?php
/*
* Implementation of hook_form_alter().
*/
function uc_checkout_pane_form_alter(&$form, $form_state, $form_id){
 
/*
   * Global $user to get the uid of the current user. Then load that user
   * into the variable $account.
   */
 
global $user;
 
$account = user_load(array('uid' => $user->uid));
 
 
 
/*
   * Run a switch statement on the $form_id. In the case of ubercarts
   * checkout form auto populate the billing pane information with the
   * information provided by the user in the billing information section
   * of their profile.
   */
 
switch ($form_id) {
    case
'uc_cart_checkout_form':
     
$dpane = &$form['panes']['delivery'];
     
$dpane['delivery_first_name']['#default_value'] = $account->profile_prenom;
     
$dpane['delivery_last_name']['#default_value'] = $account->profile_nom;

      break;
  }
}

?>

I've tried all the solutions provided on this thread, unfortunately, nothing works for me. As I am not good with creating modules etc. I guess I'm doing something wrong...
First, I thought that the problem could be caused by the UC_location module but now it's disabled.

Could you please help me ?

Thank you very much,

PS : I'm using Drupal 6.2 and the latest releases of Ubercart and extra modules.

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Hello, I've tried to

The function needs to be named the same as your module

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

ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Pre-populating the shipping address fields using hook_form_a

Maybe this will save someone some time

<?php
switch ($profile_state) {
    case
'AL':
       
$state = 1;
        break;
    case
'AK':
       
$state = 2;
        break;
    case
'AZ':
       
$state = 4;
        break;
    case
'AR':
       
$state = 5;
        break;
    case
'CA':
       
$state = 12;
        break;
    case
'CO':
       
$state = 13;
        break;
    case
'CT':
       
$state = 14;
        break;
    case
'DE':
       
$state = 15;
        break;
    case
'FL':
       
$state = 18;
        break;
    case
'GA':
       
$state = 19;
        break;
    case
'HI':
       
$state = 21;
        break;
    case
'ID':
       
$state = 22;
        break;
    case
'IL':
       
$state = 23;
        break;
    case
'IN':
       
$state = 24;
        break;
    case
'IA':
       
$state = 25;
        break;
    case
'KS':
       
$state = 26;
        break;
    case
'KY':
       
$state = 27;
        break;
    case
'LA':
       
$state = 28;
        break;
    case
'ME':
       
$state = 29;
        break;
    case
'MD':
       
$state = 31;
        break;
    case
'MA':
       
$state = 32;
        break;
    case
'MI':
       
$state = 33;
        break;
    case
'MN':
       
$state = 34;
        break;
    case
'MS':
       
$state = 35;
        break;
    case
'MO':
       
$state = 36;
        break;
    case
'MT':
       
$state = 37;
        break;
    case
'NE':
       
$state = 38;
        break;
    case
'NV':
       
$state = 39;
        break;
    case
'NH':
       
$state = 40;
        break;
    case
'NJ':
       
$state = 41;
        break;
    case
'NM':
       
$state = 42;
        break;
    case
'NY':
       
$state = 43;
        break;
    case
'NC':
       
$state = 44;
        break;
    case
'ND':
       
$state = 45;
        break;
    case
'OH':
       
$state = 47;
        break;
    case
'OK':
       
$state = 48;
        break;
    case
'OR':
       
$state = 49;
        break;
    case
'PA':
       
$state = 51;
        break;
    case
'RI':
       
$state = 53;
        break;
    case
'SC':
       
$state = 54;
        break;
    case
'SD':
       
$state = 55;
        break;
    case
'TN':
       
$state = 56;
        break;
    case
'TX':
       
$state = 57;
        break;
    case
'UT':
       
$state = 58;
        break;
    case
'VT':
       
$state = 59;
        break;
    case
'VA':
       
$state = 61;
        break;
    case
'WA':
       
$state = 62;
        break;
    case
'WV':
       
$state = 63;
        break;
    case
'WI':
       
$state = 64;
        break;
    case
'WY':
       
$state = 65;
        break;
}
?>
pxwise's picture
Offline
Joined: 06/24/2013
Juice: 7
$order->delivery_zone conversion for packing slip

Thank you very much for this! I am using the inverse relationship to get the state within the packing slip template:

<?php
$delivery_zone_code
= $order->delivery_zone;

switch (

$delivery_zone_code) {
    case
1:
       
$state = 'AL';
        break;
    case
2:
       
$state = 'AK';
        break;
    case
4:
       
$state = 'AZ';
        break;
    case
5:
       
$state = 'AR';
        break;
    case
12:
       
$state = 'CA';
        break;
    case
13:
       
$state = 'CO';
        break;
    case
14:
       
$state = 'CT';
        break;
    case
15:
       
$state = 'DE';
        break;
    case
18:
       
$state = 'FL';
        break;
    case
19:
       
$state = 'GA';
        break;
    case
21:
       
$state = 'HI';
        break;
    case
22:
       
$state = 'ID';
        break;
    case
23:
       
$state = 'IL';
        break;
    case
24:
       
$state = 'IN';
        break;
    case
25:
       
$state = 'IA';
        break;
    case
26:
       
$state = 'KS';
        break;
    case
27:
       
$state = 'KY';
        break;
    case
28:
       
$state = 'LA';
        break;
    case
29:
       
$state = 'ME';
        break;
    case
31:
       
$state = 'MD';
        break;
    case
32:
       
$state = 'MA';
        break;
    case
33:
       
$state = 'MI';
        break;
    case
34:
       
$state = 'MN';
        break;
    case
35:
       
$state = 'MS';
        break;
    case
36:
       
$state = 'MO';
        break;
    case
37:
       
$state = 'MT';
        break;
    case
38:
       
$state = 'NE';
        break;
    case
39:
       
$state = 'NV';
        break;
    case
40:
       
$state = 'NH';
        break;
    case
41:
       
$state = 'NJ';
        break;
    case
42:
       
$state = 'NM';
        break;
    case
43:
       
$state = 'NY';
        break;
    case
44:
       
$state = 'NC';
        break;
    case
45:
       
$state = 'ND';
        break;
    case
47:
       
$state = 'OH';
        break;
    case
48:
       
$state = 'OK';
        break;
    case
49:
       
$state = 'OR';
        break;
    case
51:
       
$state = 'PA';
        break;
    case
53:
       
$state = 'RI';
        break;
    case
54:
       
$state = 'SC';
        break;
    case
55:
       
$state = 'SD';
        break;
    case
56:
       
$state = 'TN';
        break;
    case
57:
       
$state = 'TX';
        break;
    case
58:
       
$state = 'UT';
        break;
    case
59:
       
$state = 'VT';
        break;
    case
61:
       
$state = 'VA';
        break;
    case
62:
       
$state = 'WA';
        break;
    case
63:
       
$state = 'WV';
        break;
    case
64:
       
$state = 'WI';
        break;
    case
65:
       
$state = 'WY';
        break;
}
?>
mayank2008's picture
Offline
Joined: 04/01/2011
Juice: 6
Pre-populating the Default product pickup address of product

Hi,
I want to Pre-populating the Default product pickup address at the time new product is added from the admin.

I have created the one Profile (Dealer) and using content profile module they have entered there pickup address (city, zip , country...) .

Now, I want to load(pre-populate) content profile pickup address when Dealer creates a new product that is on the 'product_node_form'

Please check the attached file to understand more.

AttachmentSize
Create Product - forklifts.com_1302549153381.png 17.73 KB