Adding custom fields to Ubercart checkout panes

Profile picture for user Phil Frilling
By Phil Frilling, 16 September, 2016
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:


/**
 * Implements hook_uc_checkout_pane().
 */
function MYMODULE_uc_checkout_pane() {
  $panes['MYMODULE_customer'] = array(
    'callback' => 'MYMODULE_customer_information_pane',
    'title' => t('Customer Information'),
    'desc' => t('Get the necessary information to create a customer on the site.'),
    'weight' => 1,
    'process' => FALSE,
    'collapsible' => FALSE,
  );
  return $panes;
}
2. Then, I wrote a custom function that mimicked the original customer information pane function.


/**
 * Custom callback for the customer information.
 */
function MYMODULE_customer_information_pane($op, $order, $form = NULL, &$form_state = NULL) {

  switch ($op) {
    case 'view':
      $content = uc_checkout_pane_customer($op, $order, $form, $form_state);

      $content['contents']['MYMODULE_customer_school'] = uc_textfield(t('School name'), $order->data['MYMODULE_customer_school'], TRUE, NULL, 64);

      return $content;
    break;

    case 'review':
      $review = uc_checkout_pane_customer($op, $order, $form, $form_state);
      $review[] = array('title' => t('School name'), 'data' => check_plain($order->data['MYMODULE_customer_school']));

      return $review;
      break;

    case 'process':
      // Set the original customer pane to the new one.
      $form_state['values']['panes']['customer'] = $form_state['values']['panes']['MYMODULE_customer'];

      $pane = uc_checkout_pane_customer($op, $order, $form, $form_state);

      $order->data['MYMODULE_customer_school'] = $form_state['values']['panes']['MYMODULE_customer']['MYMODULE_customer_school'];

      return $pane;

      break;
  }
}
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():

/**
 * Implements hook_uc_order_pane_alter().
 */
function MYMODULE_uc_order_pane_alter(&$panes) {
  $panes['customer']['callback'] = 'MYMODULE_order_pane_customer';
}
4. Add the custom order pane callback function. Similar to step 2, we utilize the original function and add in our customizations.


/**
 * Customer callback for the customer pane.
 */
function MYMODULE_order_pane_customer($op, $order, &$form = NULL, &$form_state = NULL) {
  switch ($op) {
    case 'view':
      $build = uc_order_pane_customer($op, $order, $form, $form_state);

      if (isset($order->data['MYMODULE_customer_school'])) {
        $build['MYMODULE_customer_school'] = array('#markup' => '' . t('School name:') . '' . check_plain($order->data['MYMODULE_customer_school']) . '');
      }

      return $build;
    
    case 'edit-form':
      $form = uc_order_pane_customer($op, $order, $form, $form_state);

      if (isset($order->data['MYMODULE_customer_school'])) {
        $form['customer']['MYMODULE_customer_school'] = array(
          '#type' => 'textfield',
          '#title' => t('School name'),
          '#default_value' => $order->data['MYMODULE_customer_school'],
          '#maxlength' => 64,
          '#size' => 32,
        );  
      }

      return $form;

    case 'edit-theme':
      $output = uc_order_pane_customer($op, $order, $form, $form_state); 

      return $output;

    case 'edit-process':
      $changes = uc_order_pane_customer($op, $order, $form, $form_state);

      if (isset($form_state['values']['MYMODULE_customer_school'])) {
        $order->data['MYMODULE_customer_school'] = $form_state['values']['MYMODULE_customer_school'];        
      }

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