Adding Custom Fields to Ubercart Checkout

By Paulus, 24 January, 2011

I had to add the SKU to the checkout of Ubercart, to do that, I had to do the following:

function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    ....
    case 'uc_cart_view_form':
      // Reordering existing columns
      $form['items']['#columns']['desc']['weight'] = 3;
      $form['items']['#columns']['qty']['weight'] = 4;

      // Adding SKU column
      $form['items']['#columns']['sku'] = array('cell' => 'SKU', 'weight' => 2);

      // Iterating through the items array element. Since there are more than just
      // products in this array, we have to make sure we are modifying the correct
      // arrays. I do this by checking to see if the element has an array element
      // called 'nid'
      for($i=0; $i < count($form['items']); $i++) {
	if(isset($form['items'][$i]['nid'])) {
          // Loading the node so we can retrieve the information we need.
	  $product = node_load($form['items'][$i]['nid']['#value']);

          // Adding the sku/model to the prodct that is in the user's cart.
	  $form['items'][$i]['sku']['#value'] = $product->model;
	  $form['items'][$i]['sku']['#cell_attributes'] = array('class' => 'cart-sku');
	}
      }
      break;
      ......
  }
}