Skip to main content

Adding custom fields to Ubercart checkout panes

Today I needed to add a custom textfield to the 'customer information' pane in Ubercart. After much searching, I wasn't able to find any modules that would allow this to happen, so I wrote my own.

I found this article, which gave me the starting block (as it was written for Drupal 6 I believe).

1. First, I added a new hook_uc_checkout_pane() function to my custom module to allow for a customized customer information pane:

  1. /**
  2.  * Implements hook_uc_checkout_pane().
  3.  */
  4. function MYMODULE_uc_checkout_pane() {
  5.   $panes['MYMODULE_customer'] = array(
  6.     'callback' => 'MYMODULE_customer_information_pane',
  7.     'title' => t('Customer Information'),
  8.     'desc' => t('Get the necessary information to create a customer on the site.'),
  9.     'weight' => 1,
  10.     'process' => FALSE,
  11.     'collapsible' => FALSE,
  12.   );
  13.   return $panes;
  14. }

2. Then, I wrote a custom function that mimicked the original customer information pane function.

  1. /**
  2.  * Custom callback for the customer information.
  3.  */
  4. function MYMODULE_customer_information_pane($op, $order, $form = NULL, &$form_state = NULL) {
  5.  
  6.   switch ($op) {
  7.     case 'view':
  8.       $content = uc_checkout_pane_customer($op, $order, $form, $form_state);
  9.  
  10.       $content['contents']['MYMODULE_customer_school'] = uc_textfield(t('School name'), $order->data['MYMODULE_customer_school'], TRUE, NULL, 64);
  11.  
  12.       return $content;
  13.     break;
  14.  
  15.     case 'review':
  16.       $review = uc_checkout_pane_customer($op, $order, $form, $form_state);
  17.       $review[] = array('title' => t('School name'), 'data' => check_plain($order->data['MYMODULE_customer_school']));
  18.  
  19.       return $review;
  20.       break;
  21.  
  22.     case 'process':
  23.       // Set the original customer pane to the new one.
  24.       $form_state['values']['panes']['customer'] = $form_state['values']['panes']['MYMODULE_customer'];
  25.  
  26.       $pane = uc_checkout_pane_customer($op, $order, $form, $form_state);
  27.  
  28.       $order->data['MYMODULE_customer_school'] = $form_state['values']['panes']['MYMODULE_customer']['MYMODULE_customer_school'];
  29.  
  30.       return $pane;
  31.  
  32.       break;
  33.   }
  34. }

The custom function utilizes the original function to keep Ubercart working normally. After we get the data from the original function, we customize the data before returning it like normal.

3. In order to view the data within our new field, we need to alter the order pane with hook_uc_order_pane_alter():

  1. /**
  2.  * Implements hook_uc_order_pane_alter().
  3.  */
  4. function MYMODULE_uc_order_pane_alter(&$panes) {
  5.   $panes['customer']['callback'] = 'MYMODULE_order_pane_customer';
  6. }

4. Add the custom order pane callback function. Similar to step 2, we utilize the original function and add in our customizations.

  1. /**
  2.  * Customer callback for the customer pane.
  3.  */
  4. function MYMODULE_order_pane_customer($op, $order, &$form = NULL, &$form_state = NULL) {
  5.   switch ($op) {
  6.     case 'view':
  7.       $build = uc_order_pane_customer($op, $order, $form, $form_state);
  8.  
  9.       if (isset($order->data['MYMODULE_customer_school'])) {
  10.         $build['MYMODULE_customer_school'] = array('#markup' => '<p><strong>' . t('School name:') . '</strong><br />' . check_plain($order->data['MYMODULE_customer_school']) . '</p>');
  11.       }
  12.  
  13.       return $build;
  14.  
  15.     case 'edit-form':
  16.       $form = uc_order_pane_customer($op, $order, $form, $form_state);
  17.  
  18.       if (isset($order->data['MYMODULE_customer_school'])) {
  19.         $form['customer']['MYMODULE_customer_school'] = array(
  20.           '#type' => 'textfield',
  21.           '#title' => t('School name'),
  22.           '#default_value' => $order->data['MYMODULE_customer_school'],
  23.           '#maxlength' => 64,
  24.           '#size' => 32,
  25.         );  
  26.       }
  27.  
  28.       return $form;
  29.  
  30.     case 'edit-theme':
  31.       $output = uc_order_pane_customer($op, $order, $form, $form_state); 
  32.  
  33.       return $output;
  34.  
  35.     case 'edit-process':
  36.       $changes = uc_order_pane_customer($op, $order, $form, $form_state);
  37.  
  38.       if (isset($form_state['values']['MYMODULE_customer_school'])) {
  39.         $order->data['MYMODULE_customer_school'] = $form_state['values']['MYMODULE_customer_school'];        
  40.       }
  41.  
  42.       return $changes;
  43.   }
  44. }

That's it. Now, you can add a custom field to the customer information pane and have it displayed in the admin area.


Comments