I had a similar situtation, offering online entries into dog show. Have CCK type dog, that has a bunch of fields that store the info needed to enter a show.
So the product is a particular show, and in the product I want the user to select which of their dogs they are entering. There are attributes in the product duplicating the fields in 'dog'.
Then when the form is submitted, it looks up what dog they are entering and fills in the attributes for the entry form.
Here is the code which I put in a module.
function uc_dog_entry_add_to_cart_data($form_values){
global $user;
if (!isset($form_values['attributes'])){
return array('attributes' => array());
}
if ($form_values['attributes']['9'] != 'None') {
$result = db_query('SELECT nid FROM {node} WHERE type="dog" AND uid=%d AND title="%s"', $user->uid, $form_values['attributes']['9']);
$obj = db_fetch_object($result);
$nid = $obj->nid;
$node = node_load($nid);
$form_values['attributes']['7'] = $node->field_place_of_birth[0]['value'];
$form_values['attributes']['8'] = $node->field_dob[0]['value'];
$form_values['attributes']['10'] = $node->field_reg_name[0]['value'];
$form_values['attributes']['6'] = $node->field_registration[0]['value'];
$form_values['attributes']['11'] = $node->field_breed[0]['value'];
$form_values['attributes']['12'] = $node->field_sex[0]['value'];
$form_values['attributes']['13'] = $node->field_height_measurement[0]['value'];
$form_values['attributes']['14'] = $node->field_breeder[0]['value'];
$form_values['attributes']['15'] = $node->field_sire[0]['value'];
$form_values['attributes']['16'] = $node->field_dam[0]['value'];
$form_values['attributes']['26'] = $node->field_call_name[0]['value'];
$form_values['attributes']['27'] = $node->field_handler[0]['value'];
$form_values['attributes']['28'] = $node->field_reg_type[0]['value'];
}
// drupal_set_message('new Attributes to add to order Array(aid => oid):<pre>'. print_r($form_values['attributes'], true) .'</pre>'); // debug code to see what going on
return array('attributes' => $form_values['attributes']);
}
/**
* Implementation of hook_form_alter().
*/
function uc_dog_entry_form_alter($form_id, &$form) {
global $user;
$node = $form['#node'];
if (strncmp($form_id, 'uc_product_add_to_cart_form_', 28) == 0) {
//$form['form_info'] = array('#value' => '<pre>'. print_r($form, TRUE) .'</pre>'); // useful for getting the ids and stuff from on a form, just uncomment it to use it.
$result = db_query('SELECT nid, title FROM {node} WHERE type="dog" AND uid=%d', $user->uid);
$dog = array();
$dog['None'] = 'None';
$dog_desc = t('You have no "dogs" in the database. Go add your dog to the database.');
$dog_data = t('Fill in all the information below to complete your entry. In the future you can skip this step by adding your dog to the database.');
while ($obj = db_fetch_object($result)) {
$dog["$obj->title"] = $obj->title;
$dog_desc = t('Select the dog to enter in is this trial.');
$dog_data = t('If you already selected a dog up above, then you can leave all these fields blank. They will be automatically filled in.');
}
//$form['attributes']['9']['#title'] = 'THE Name';
$form['attributes']['9']['#type'] = 'select';
$form['attributes']['9']['#options'] = $dog;
$form['attributes']['9']['#default_value'] = 'None';
$form['attributes']['9']['#description'] = $dog_desc;
$form['attributes']['26']['#description'] = $dog_data;
}
}This could theoretically be worked into a more general module.



Joined: 12/10/2007