This is my first attempt at doing any kind of coding since 1991 (before I went into the social science), but I really wanted to figure out how to set up a registration form (for a music festival) that would send people them directly from the registration form to a cart where they could pay the registration fees. So here it is.

The registration form asks how many adults, children under 6, and children over six plan to attend. After filling out the form, the users are sent to a page telling them how much the registration fee will be and inviting them to pay for it using paypal. When they go to their cart the number of adults and children is automatically entered into the user's cart and he or she will merely have to check out.

The code below works for me (after only a little testing), but I know it is still poor. I'm submitting this to help others who might be trying to do this, but I'm also interested in whether or not I've made any horrible mistakes.

Here's my big question:

The table 'ec_cart' contains two fields that I don't really understand -- 'changed' and 'data'.

Into the field 'changed', I decided to insert $user->changed'. I'm not sure if this is what needs to go in there.

And I still haven't figured out what should be entered into the 'data' field, so I just put the same information that the field contained when I added the products in manually. This, of course, is all wrong. Any hints?

If anyone wants to build off of this, you will have to customize this: adultnid and childrennid refer to the node numbers for the two 'products' that I've already created for the registration. 'Adults', 'Children under 6', and 'Children over 6' are fields that I created through my webform, you will have different names.

Curious what other people think.

Charles

  $adultnid = '113';
  $childrennid = '114';
  global $user;
  $sid = 0;
  $adults = 0;
  $children = 0;


/**
 * Check whether or not the user has registered yet. 
 * Also get the sid number which allows us to figure out which registration form to take 
 */

$sid = db_result(db_query("SELECT sid FROM {webform_submitted_data} WHERE name = '__userid' AND data = '$user->uid'"));

if ($sid < 1 OR  $user->uid < 1) {
echo "You have not yet registered yet!  To pay for the slet, you must first login and register. Please go to the <a href='http://ksp.barashek.org/KSP/SpringSlet2006/Registration'>registration page</a>";
} else {

/**
 * Get and process necessary information out of the webform
 */
  
$adults = db_result(db_query("SELECT data FROM {webform_submitted_data} WHERE sid = $sid AND name = 'Adults'"));

  $childrenunder6 = db_result(db_query("SELECT data FROM {webform_submitted_data} WHERE sid = $sid AND name = 'Children under 6'"));
  $childrenover6 = db_result(db_query("SELECT data FROM {webform_submitted_data} WHERE sid = $sid AND name = 'Children over 6'"));
$children = $childrenunder6 + $childrenover6;

$price = $adults * 15 + $children * 5;

echo "You have registered ".$adults." adults and ".$children." children.";
echo " To complete your registration, you must pay $".$price.".";
echo "<br />";
echo "<br />";


/**
 * Add registration 'products' into their cart and send them on their way.
 */

db_query("INSERT INTO {ec_cart} (cookie_id, nid, qty, changed, data)
  VALUES ('$user->uid', '$adultnid', '$adults', '$user->changed',
  'a:0:{}')");

db_query("INSERT INTO {ec_cart} (cookie_id, nid, qty, changed, data)
  VALUES ('$user->uid', '$childrennid', '$children', '$user->changed',
  'a:0:{}')");

echo "Please send your check or money order to <em>this address</em>";
echo "<br />";
echo "<br />";
echo "Or if you would like to pay using paypal or through your credit card, move to your shopping <a href='cart'>cart</a>.";
};

Comments

trevortwining’s picture

looking at doing something similar ... where did you place this code?

Trevor Twining
emailmetrevortwininginfo

Trevor Twining
Freelance Drupal Dev
Theme/Modules/Sitebuilding

chook’s picture

I set up the registration module to go to a 'pre-payment' page, where I put this code.

The code I submitted above tells the user what the costs will be and asks them if they want to proceed to the cart. The user confirms by clicking on a link taking them to the cart. The pre-payment page automatically adds the necessary products (i.e., tickets) to their cart. Once they confirm that they want to purchase tickets through the site store, the user is sent to the cart where he or she pays for the tickets using paypal.

Hope that helps. Sorry for not checking my account for a while. I got busy.

Charles