Is there presently a system to pass name/address information from the webform to Ubercart? I'm not seeing one, so if not, I'd like to make this a feature request. My clients like to have all the collected information in a single webform report rather than having to pair up webform submissions with Ubercart transactions. This seems like a logical feature for this module.

You can do this using the Ubercart ECO and Webform PHP modules, but Webform PHP puts PHP in the database, which is obviously not recommended. So it would be great to be able to do this in a safer way through this module. If anyone has done this and has a custom module code snippet or a patch for this module, I'd appreciate it.

Comments

brightbold’s picture

For what it's worth, here's code that works with ECO and Webform PHP:

  setcookie("customer[first_name]", $form_state["values"]["submitted_tree"]["sponsor"]["first_name"], time()+2592000, "/");
  setcookie("customer[last_name]", $form_state["values"]["submitted_tree"]["sponsor"]["last_name"], time()+2592000, "/");
  setcookie("customer[company]", $form_state["values"]["submitted_tree"]["sponsor"]["company"], time()+2592000, "/");
  setcookie("customer[street]", $form_state["values"]["submitted_tree"]["sponsor"]["address"], time()+2592000, "/");
  if ($form_state["values"]["submitted_tree"]["sponsor"]["address_2"]) {
    setcookie("customer[street2]", $form_state["values"]["submitted_tree"]["sponsor"]["address_2"], time()+2592000, "/");
  }
  setcookie("customer[city]", $form_state["values"]["submitted_tree"]["sponsor"]["city"], time()+2592000, "/");
  setcookie("customer[state]", $form_state["values"]["submitted_tree"]["sponsor"]["state"], time()+2592000, "/");
  setcookie("customer[zipcode]", $form_state["values"]["submitted_tree"]["sponsor"]["zip"], time()+2592000, "/");
  setcookie("customer[email]", $form_state["values"]["submitted_tree"]["sponsor"]["email"], time()+2592000, "/");
  setcookie("customer[phone]", $form_state["values"]["submitted_tree"]["sponsor"]["telephone"], time()+259200, "/");

where "sponsor" is the name of a webform fieldset and the item after that is the field key.

andyf’s picture

Status: Postponed » Active

No that's not currently possible. I've actually had this functionality working on a site since before the module was stable, but nobody ever requested it, so I never cleaned it up (it doesn't have a UI). I guess what we'd want is a way to map a component name to an address line in the UI. It might be a good idea to put this in a separate module (already uc_event_registration's grown larger than I'd like).

I'm very busy at the mo but should be freer in a month or so. If anyone wants to supply a patch (or make a new module) here's the code I've been using (slightly cleaned up, might need more):

/**
 * Implements hook_form_FORM_ID_alter(). Prepopulates address fields from
 * the webform (where possible).
 */
function shophelper_form_uc_cart_checkout_form_alter(&$form, &$form_state) {
  
  $cart = unserialize($form['cart_contents']['#value']);
  foreach ($cart as $product) {
    if (isset($product->data['uc_event_registration']['sid'])) {
      $sid = $product->data['uc_event_registration']['sid'];
      break;
    }
  }
  
  // Early out if there's no paidevent
  if (!isset($sid)) {
    return;
  }
  
 // The mapping still needs a prefix to be applied, eg. delivery_first_name
  $mapping = array(
    'first_name' =>         '_first_name',
    'surname' =>            '_last_name',
    'address_line_1' =>     '_street1',
    'address_line_2' =>     '_street2',
    'town' =>               '_city',
    'postcode' =>           '_postal_code',
    'telephone' =>          '_phone',
    'mobile' =>             '_phone',
  );

  // If the user only orders a paidevent then there's no delivery address as
  // there's no shipping, and so we want to fill in the billing address. If
  // there's a 'real' product in the cart as well, then we want to fill out the
  // delivery address, as there's an option to automatically populate the
  // billing address from the delivery anyway.
  
  if (isset($form['panes']['delivery'])) {
    $checkout_address =& $form['panes']['delivery'];
    foreach ($mapping as &$field_name) {
      $field_name = 'delivery' . $field_name;
    }
  }
  else {
    $checkout_address =& $form['panes']['billing'];
    foreach ($mapping as &$field_name) {
      $field_name = 'billing' . $field_name;
    }
  }
  shophelper_webform_checkout_field_mapper($checkout_address, $sid, $mapping);
  if ($form['panes']['customer']['primary_email']['#type'] === 'textfield') {
    shophelper_webform_checkout_field_mapper(
      $form['panes']['customer'],
      $sid,
      array('email' => 'primary_email')
    );
  }
}

/**
 * Populates form elements from webform submissions.
 *
 * @param &$form
 *   The form to populate. This function doesn't descend any trees to find
 *   elements.
 * @param $sid
 *   The sid of the relevant webform submission.
 * @param $mapping
 *   An array keyed by webform component form keys, with values being the
 *   corresponding form keys of $form.
 */
function shophelper_webform_checkout_field_mapper(&$form, $sid, $mapping) {
  
  $form_keys = array_keys($mapping);
  $form_key_placeholders = db_placeholders($form_keys, 'varchar');

  $sql = <<<SQL
    SELECT data, form_key
      FROM {webform_submitted_data} wsd
      JOIN {webform_component} wc
        ON
          wsd.nid = wc.nid AND
          wsd.cid = wc.cid AND
          wsd.sid = %d AND
          wc.form_key IN ($form_key_placeholders)
SQL;

  $args = array_merge(array($sid), $form_keys);

  $res = db_query($sql, $args);
  while ($record = db_fetch_array($res)) {
    $form_key = $record['form_key'];
    $element =& $form[$mapping[$form_key]];
    if (isset($element) && empty($element['#default_value'])) {
      $element['#default_value'] = $record['data'];
    }
  }

  return;
}
andyf’s picture

Status: Active » Postponed
brightbold’s picture

Status: Active » Postponed

Thanks. I don't have the capability to create a UI and patch the module, but I will definitely try out your custom module code on my site — that's incredibly helpful.

andyf’s picture

Glad it's of use! No worries re UI. Just wanna put the code out there; if someone codes a UI fantastic, if not no great shakes.

If you have any issues using it, do please post back here.

Thanks