Event Registration with Ubercart

26 Jan
Published by admin

Event Registration with Ubercart

* cck
* Media
* ubercart
* views

To begin, you're going to want to make sure you have the following modules installed and enabled (in addition to the default Drupal core modules that are enabled on a fresh install):

* Admin Menu (not required for this tutorial, but some of the screen shots below use it)
* Content
* Content: Number
* Path
* Token
* All of the core Ubercart modules (6.x-2.0-beta5 or later!)
* Ubercart: Catalog
* Ubercart: Payment
* Node Checkout
* Ubercart: Credit Card
* Ubercart: Test Gateway
* Views
* Views UI

The first thing you'll need to do is to set up a Product Class for Events under Administer|Store Administration|Products|Manage Classes (admin/store/products/classes):

setting up the event product class

We're going to keep things simple and not add an event date field - I'm only going to add fields that are relevant for this tutorial.

The next step is to set up a content type for the registrant profile. This is the information that you want each event registrant to fill out during the checkout process when they are purchasing an admission to the event. Go ahead and create a new content type (admin/content/types/add) with the following settings:

Name
Registrant Profile
Type
registrant_profile
Title field label
Name
Body field label
Bio
Default comment settings
Disabled

Then, you need to add a few fields to the new "Registrant Profile" content type. I'm going to be adding 2 fields to the content type - both will be hidden to the user, but you'll be using them behind the scenes to add some additional functionality. Go to the "edit registrant profile content type" page (admin/content/node-type/registrant-profile) and add 2 text fields:

Label
Status
Field Name
field_status
Type of data to store
Integer
Form element to edit the data
Text field

Label
Order ID
Field Name
field_orderid
Type of data to store
Integer
Form element to edit the data
Text field

The default values for each fields configuration settings don't need to be touched. Here's what the completed registrant profile content type should look like:

registrant profile content type

You're also going to want to go to your user permissions page and allow both anonymous and authorized users the "create registrant_profile content" permissions.

At this point, you have the necessary content types set up, the next step is to create an event product, and associate it with the registrant profile. Click on the "create content" menu item and select "event" as the content you'd like to create. Again, we really only need to fill out the required fields, the rest of the fields are fine the way they are.

Name
Rock Climbing Championship of the World
Description
Don't miss this event!
SKU
event01
Sell Price
100
Product and its derivatives are shippable.
not checked

rock climbing event

Next, you need to link the registrant profile with this event using the UC Node Checkout settings. Take note of the node ID of the event - assuming that the Pathauto module is not enabled, you can find the node ID in the URL of the event page - it should look something like this:

http://localhost/your-drupal-directory/node/1

In my case, since I'm using a fresh install of Drupal, the node ID of my Rock Climbing event is 1. Then, navigate to the UC Node Checkout settings via the Administration Menu:

admin menu node checkout settings

I want to associate the "Registrant Profile" content type with the "Rock Climbing Championship of the World" event I just created. This will force attendees of the event to fill out a registrant profile during the checkout process. Click the "edit" link for the Registrant Profile.

The "Product NID" is the node ID of your event - in my case, it is equal to 1. I also made both the "Status" and "Order ID" fields "restricted" - this will hide the fields from public view.

registrant profile checkout settings

You should also take a quick peek at the admin/store/settings/node-checkout/settings page - there is one setting in particular that you should pay attention to - the "Use UC Node Checkout to prevent anonymous node add access for node types it governs" - when checked, this makes it so anonymous users can't create the associated node ("registrant profile" in our case), only logged in users can - regardless of Ubercart's anonymous checkout settings. Go ahead and uncheck this box for this example to allow anonymous users to complete a registrant profile. Be warned that it will also expose a create "registrant profile" link in the "create content" menu that you may want to disable in the future.

At this point, we actually have a semi-working system. Go ahead and log out as admin, then navigate to your event product and click to add it to your cart. You'll see that you're immediately directed to a blank "registrant profile" page that you're required to fill out prior to checkout. Be sure to notice that the "Status" and "Order ID" fields are not visible.

create registrant profile

There's still one major flaw in the system - if a user completes the "registrant profile" form, but failed to complete the checkout, you'll end up with an orphaned registrant profile page - we need some way to mark a registrant profile page as paid. This is what we're going to use the "Status" and "Order ID" fields for. We'll use Ubercart's Conditional Actions to add a bit of PHP code to update the fields when the user completes the checkout process.

conditional actions menu

A conditional action is simply something that takes place when a certain condition is met. In our case, the trigger is going to be "customer completes checkout", the condition is going to be that the customer is ordering our "Rock Climbing Championship of the World" product, and the action is going to be our custom PHP code to update the "Status" and "Order ID" fields. Go ahead and click to "Add a predicate" from Ubercart's Conditional Actions page.

Title
Update registrant profile
Trigger
Custom completes checkout

Next, we want to add the condition. This page can be a little tricky - be careful about which buttons you're clicking here, I've gotten into trouble more than once on this page.

Under "available conditions", select "Check and order's products", then click "Add condition". Then, fill out the form as follows:

condition group

When finished, click "Save changes". Then click the "Actions" tab and select "Execute Custom PHP code" (this is where it is required that you're using Ubercart 6.x-2.0-beta5 or later) and click the "Add Action" button. Change the "Title" to "Update custom fields" and enter the following PHP code:

if (isset($order)) {
foreach ($order->products as $product) {
if (isset($product->data['node_checkout_nid'])) {
$node = node_load($product->data['node_checkout_nid']);
$node->field_status['0']['value'] = 1;
$node->field_orderid['0']['value'] = $order->order_id;
node_save($node);
}
}
}

When complete, click the "Save changes" button. As you can see, the code simply checks for the existence of the $order object, then loops around all of its products, when it finds one that includes a "node_checkout_nid" (from the UC Node Checkout module), it loads it and updates the "Status" and "Order ID" fields.

Once the order is complete, log back in as the site admin, and view the registrant profile node - you'll see the updated fields. You might also want to try repeating the process, but abandoning the cart to see the difference.

At this point, you can create a view to show the registrant profiles for customers who have completed their checkout as well as a listing of those who have abandoned their checkout. I'm not going to go through the process of creating the views here, but I've attached them (along with the "event" and "registrant_profile" content types) to this article.

That should do it - you now should have a fully-functional event registration system that exposes registrant profile information as fully functional nodes that you can bend to your will using any number of modules.

Designed by matt renfro