Dynamic attributes

Posts: 1
Joined: 09/10/2007

Hi,
I got a product a shirt, which I want the user to be able to buy for his friends.
So I got a content type called friends, which holds the friends name.

I'd like to have attribute for the product that will be dynamically updated (probably best way is to filter using the Views - same as it is possible to node reference to a certain view).

How can I do it?

Posts: 5269
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Hmm... I don't know if product attributes can handle this at the moment. It will actually require custom code and can be done doing something like the following:

1. Create the attribute but don't give it any options.
2. Create a custom module for your site that implements Drupal's hook_form_alter(). It should modify the add_to_cart form to turn the textfield into a select box which is populated with options based on a person's friends.
3. This should save the choice as if the user had typed in the text themselves.

I'm assuming this is related to this thread:

http://www.ubercart.org/forum/support/578/single_product_several_friends

Posts: 87
Joined: 09/08/2007
Bug Finder

Oops, forgot to enter this related link myself. sorry.
Thanks for the help! I'll ask some developer to help me with this Eye-wink

Posts: 73
Joined: 12/10/2007

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.

Posts: 87
Joined: 09/08/2007
Bug Finder

Please review this patch, it's doing the job really nicely Smiling
http://drupal.org/node/271928