Add Fields to Checkout Form

Posts: 21
Joined: 01/13/2008

Hi,

I was wondering if someone could help guide me in the right direction to adding 9 more fields to the checkout, which will be included in the order history. I downloaded uc_lead to try and guide me in the right direction. (I'm not overly skilled in php and quite new to ubercart). Anyways, I've got as far as adding the new pane, and all four fields within the pane on the checkout screen. However, once the user is directed to the review page the fields are blank, therefore nothing is stored to the database. Can someone please guide me in the right direction for adding these fields and the exact steps I would need to take in modifying (uc_lead) and/or starting from scratch to include these fields?

Fields I want to add:

Player Name1
Email Address1
Position1

Player Name2
Email Address2
Position2

Player Name3
Email Address3
Position3

Thank You!

Sincerely,

Kristy

Posts: 21
Joined: 01/13/2008

Okay, I'm just going to take a shut in the dark, in hopes someone will be able to point out my errors. Here is the code that right now is adding a pane:

Tournament Roster
Fields: Player Name, Position, Email Address

However, nothing is being written to the database:
Once this gets figured out, I need to repeat the three fields several times to allow for someone to enter their entire team. Advice?

/*******************************************************************************
* Hook Functions (Ubercart)
******************************************************************************/

/**
* Implementation of hook_checkout_pane().
*/
function uc_lead_checkout_pane() {
$panes[] = array(
'id' => 'lead',
'callback' => 'uc_checkout_pane_lead',
'title' => t('Tournament Roster'),
'desc' => t('Tournament roster is required upon registering for a tournament.'),
'weight' => 8,
);

return $panes;
}

/**
* Implementation of hook_order().
*/
function uc_lead_order($op, &$arg1, $arg2) {
switch ($op) {
// Save the lead to the database.
case 'save':
if (!empty($arg1->lead['name'])) {
db_query("UPDATE {uc_leads} SET lead_name = '%s', lead_position = '%s', lead_email = 's' "
."WHERE order_id = %d", $arg1->lead['name'],
$arg1->lead['position'], $arg1->lead['email'], $arg1->order_id);
if (db_affected_rows() == 0) {
db_query("INSERT INTO {uc_leads} (lead_id, order_id, lead_name, lead_position, lead_email) VALUES (%d, %d, '%s', '%s', '%s')",
db_next_id('{uc_leads}_lead_id'), $arg1->order_id,
$arg1->lead['name'], $arg1->lead['position'], $arg1->lead['email']);
}
}
break;

// Load the lead from the database.
case 'load':
$result = db_query("SELECT * FROM {uc_leads} WHERE order_id = %d", $arg1->order_id);
if ($data = db_fetch_object($result)) {
$arg1->lead['name'] = $data->lead_name;
$arg1->lead['position'] = $data->lead_position;
$arg1->lead['email'] = $data->lead_email;
}
break;

// Delete the lead from the database.
case 'delete':
db_query("DELETE FROM {uc_leads} WHERE order_id = %d", $arg1->order_id);
break;
}
}

/**
* Implementation of hook_order_pane().
*/
function uc_lead_order_pane() {
$panes[] = array(
'id' => 'lead',
'callback' => 'uc_order_pane_lead',
'title' => t('Tourname Roster'),
'desc' => t(''),
'class' => 'abs-left',
'weight' => 7,
'show' => array('view'),
);

return $panes;
}

/*******************************************************************************
* Callback Functions, Forms, and Tables
******************************************************************************/

function uc_checkout_pane_lead($op, &$arg1, $arg2) {
switch ($op) {
case 'view':
$description = t('Tournament roster is required upon registering for Pro Hockey Tournaments');

$contents['lead_name'] = array(
'#type' => 'textfield',
'#title' => t('Player Name'),
'#default_value' => $arg1->lead['name'],
);

$contents['lead_position'] = array(
'#type' => 'textfield',
'#title' => t('Position'),
'#default_value' => $arg1->lead['position'],
);

$contents['lead_email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#default_value' => $arg1->lead['email'],
);

return array('description' => $description, 'contents' => $contents);

case 'review':
{
$review[1] = array('title' => t('Roster'), $arg1->lead['name']);
$review[2] = array('title' => t('Position'), $arg1->lead['position']);
$review[3] = array('title' => t('Email'), $arg1->lead['email']);
}
return $review;

}
}

function uc_order_pane_lead($op, $arg1) {
switch ($op) {
case 'view':
if (empty($arg1->lead['name'])) {
$lead = t('None specified');
}
else {
$lead = $arg1->lead['name'];
}
return t('Roster: !lead', array('!lead' => $lead));
}
}

Posts: 290
Joined: 11/19/2007
Bug FinderGetting busy with the Ubercode.

I'm not sure your exact situation, but it seems like you might be able to use the attribute system to achieve what you need.

You can setup 9 attributes for your product, with the labels you listed, and leave them without any options setup for the attributes. This will add 9 text fields to the product to be entered by the customer, which will show up throughout the cart process and save in the database.

Posts: 2102
Joined: 08/07/2007
AdministratoreLiTe!

I think your checkout pane function needs to do stuff in the 'process' $op. That's how the data from 'view' gets put into 'review'. The cart checkout panes probably do the most similar thing to you, and they mostly don't do funny things with JavaScript.

Posts: 21
Joined: 01/13/2008

Thank you everyone for your help. I have solved this issue and have been able to add a total of 60 text fields to the uc_lead form (now Team Roster Pane). If anyone would like to see this, please contact me.

However, I was wondering if anyone knows how I can restrict this pane to display only if "Tournament and/or Team" displays in the product title. Originally I added custom fields to the billing pane which if "Tournament" was not found in the title it displayed individual player fields. If Tournament was found in the title it displayed a file upload to upload roster. However, with the Uc_Lead pane now collecting the roster, I have removed the file upload and need to modify the pane to only display if Tournament and/or Team is displayed in the title. The code I used on the billing pane was:

$showUpload=falase;
$items = uc_cart_get_contents ( );
for each ($items as $item) {
if( strpos(strtoupper{$item->title), 'TOURNAMENT') !== false ) {
$showUpload = true;
}
}

if( $showUpload ) {

}

else {

}

Posts: 21
Joined: 01/13/2008

I've been getting a few questions regarding this code, so I thought I would provide you with the full module file that I created for one of the panes. I ended up removing the File upload and instead created Two separate modules to control the panes for "Individual Player" and "Team" Registrations. I checked to see what values were in the SKU field to determine which pane to display. The conditional code is in one of my previous posts. Anyways my custom module for "Individual Player" ended up looking like the following. Hope this helps others. FYI.. I added "Select and Text" fields in my panes.

<?php
// $Id$

/**
* @file
* Defines a checkout pane that lets customers specify how they heard about
* your site (and so functions as a very basic player tracker).
*
* This is a demo module showing people a simple way to add a checkout pane,
* save and load the data collected to orders, and display the collected data
* on the order view screen.  Presentation on the order screen is very basic,
* and there is currently no way to modify the data in this module.
*
* When you install this module, you should go to the Checkout panes settings
* page and add player options to appear during checkout.
*
* Development sponsored by the Ubercart project: http://www.ubercart.org
*/


/*******************************************************************************
* Hook Functions (Ubercart)
******************************************************************************/


/**
* Implementation of hook_order().
*/
function uc_player_order($op, &$arg1, $arg2) {
  switch ($op) {
    // Save the player to the database.
    case 'save':
      if (!empty($arg1->player['name'])) {
        db_query("UPDATE {uc_player} SET player_name = '%s', player_dob = '%s', player_position = '%s', player_team = '%s', player_division = '%s', player_sweater = '%s'"
                ."WHERE order_id = %d", $arg1->player['name'], $arg1->player['dob'], $arg1->player['position'],$arg1->player['team'], $arg1->player['division'], $arg1->player['sweater'], $arg1->order_id);
        if (db_affected_rows() == 0) {
          db_query("INSERT INTO {uc_player} (player_id, order_id, player_name, "
                  ."player_dob, player_position, player_team, player_division, player_sweater) VALUES (%d, %d, '%s', '%s','%s', '%s','%s', '%s')",
                   db_next_id('{uc_player}_player_id'), $arg1->order_id,
                   $arg1->player['name'], $arg1->player['dob'], $arg1->player['position'], $arg1->player['team'], $arg1->player['division'], $arg1->player['sweater']);
        }
      }
      break;

    // Load the player from the database.
    case 'load':
      $result = db_query("SELECT * FROM {uc_player} WHERE order_id = %d", $arg1->order_id);
      if ($data = db_fetch_object($result)) {
        $arg1->player['name'] = $data->player_name;
$arg1->player['dob'] = $data->player_dob;
$arg1->player['position'] = $data->player_position;
$arg1->player['team'] = $data->player_team;
$arg1->player['division'] = $data->player_division;
$arg1->player['sweater'] = $data->player_sweater;
      }
      break;

    // Delete the player from the database.
    case 'delete':
      db_query("DELETE FROM {uc_player} WHERE order_id = %d", $arg1->order_id);
      break;
  }
}

/**
* Implementation of hook_order_pane().
*/
function uc_player_order_pane() {
  $panes[] = array(
    'id' => 'player',
    'callback' => 'uc_order_pane_player',
    'title' => t('Player Details'),
    'desc' => t('Collect registration details on athlete.'),
    'class' => 'abs-left',
    'weight' => 7,
    'show' => array('view'),
  );

  return $panes;
}


/*******************************************************************************
* Callback Functions, Forms, and Tables
******************************************************************************/

function uc_checkout_pane_player($op, &$arg1, $arg2) {
  switch ($op) {
    case 'view':
      $description = t('Please complete the following fields for the player you wish to register.');
/*
      $options = preg_split('/[\r\n]+/', variable_get('uc_player_options', ''));
      for ($i = 0; $i < count($options); $i++) {
        if (empty($options[$i])) {
          unset($options[$i]);
        }
      }
      $options = array_merge(array(t('Please select one...')), $options,
                             array(t('Other source'))); */
      $contents['player_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Player Name'),
'#description' => t('Enter Players Name'),
        '#required' => true,
      );
       $contents['player_dob'] = array(
        '#type' => 'textfield',
        '#title' => t('Date of Birth'),
'#description' => t('Format mm/dd/yyyy'),
        '#required' => true,
      );
   $contents['player_position'] = array(
        '#type' => 'select',
        '#title' => t('Position'),
'#options' => array (
t('Forward') => t('Forward'),
t('Defense') => t('Defense'),
t('Goalie') => t('Goalie'),),
'#description' => t('Ex. Forward, Defense, Goalie'),
        '#required' => true,
      );
  $contents['player_team'] = array(
        '#type' => 'textfield',
        '#title' => t('Team'),
'#description' => t('Enter current or previous team'),
        '#required' => true,
      );
   $contents['player_division'] = array(
        '#type' => 'textfield',
        '#title' => t('Division'),
'#description' => t('Enter team division'),
        '#required' => true,
      );
  $contents['player_sweater'] = array(
        '#type' => 'select',
        '#title' => t('Sweater Size'),
'#options' => array (
t('Youth Small') => t('Youth Small'),
t('Youth Medium') => t('Youth Medium'),
t('Youth Large') => t('Youth Large'),
t('Adult Small') => t('Adult Small'),
t('Adult Medium') => t('Adult Medium'),
t('Adult Large') => t('Adult Large'),
t('Adult X-Large') => t('Adult X-Large'),
),
'#description' => t('Select sweater size'),
        '#required' => true,
      );
      return array('description' => $description, 'contents' => $contents);

    case 'process':
       $arg1->player['name'] = $arg2['player_name'];
   $arg1->player['dob'] = $arg2['player_dob'];
   $arg1->player['position'] = $arg2['player_position'];
   $arg1->player['team'] = $arg2['player_team'];
   $arg1->player['division'] = $arg2['player_division'];
   $arg1->player['sweater'] = $arg2['player_sweater'];
     
      return TRUE;

    case 'review':
             $review[] = array('title' => t('Player Name'), 'data' => $arg1->player['name']);
$review[] = array('title' => t('Date of Birth'), 'data' => $arg1->player['dob']);
$review[] = array('title' => t('Position'), 'data' => $arg1->player['position']);
$review[] = array('title' => t('Team'), 'data' => $arg1->player['team']);
$review[] = array('title' => t('Division'), 'data' => $arg1->player['division']);
$review[] = array('title' => t('Sweater Size'), 'data' => $arg1->player['sweater']);   
      return $review;

   
  }
}

function uc_order_pane_player($op, $arg1) {
  switch ($op) {
    case 'view':
    $output =
'<table width="100%" cellpadding="5px" cellspacing="0"><tr><td algin="center" style="font-weight:bold;">Player Name</td><td algin="center" style="font-weight:bold;">DOB</td><td algin="center" style="font-weight:bold;">Position</td><td algin="center" style="font-weight:bold;">Team</td><td algin="center" style="font-weight:bold;">Division</td><td algin="center" style="font-weight:bold;">Sweater Size</td></tr>'.
'<tr><td>' .$arg1->player['name'] .'</td>'.
'<td> ' .$arg1->player['dob'] .'</td>'.
'<td> ' .$arg1->player['position'] .'</td>'.
'<td> ' .$arg1->player['team'] .'</td>'.
'<td> ' .$arg1->player['division'] .'</td>'.
'<td> ' .$arg1->player['sweater'] .'</td>'. 
'</tr></table>'
;
      return $output;
  }
}