Skip to main content

Ubercart: Add Unit Price to the Shopping Cart Table.

We are working on an Ubercart store running Drupal 7 and we needed to add the price per unit to the shopping cart page. I didn't see any options for enabling this in the store administration area so I began looking for some code to make this happen. I came across this post, http://pixeljets.com/blog/adding-price-field-ubercart-cart-page, which was close to what I needed, but I figured there was a theme function I could use instead of a custom module. After searching the code of the uc_cart module I finally came across the theme function the cart page uses: theme_tapir_table!

Details

The following lines of code from the complete function below accomplish task. Basically, we do the following steps:

  1. Add the unit_price column to the #columns array
  2. Change the weights of the 'quantity' and 'total' columns to push them to the far right of the table.
  3. Loop through each row in the table and add the sell_price to the unit_price column for the row. The sell_price is stored in the $variables array keyed by the position in the table.

  1. // Add the unit price to the $element['#columns'] array.
  2.   $element['#columns']['unit_price'] = array(
  3.     'cell' => t('Unit Price'),
  4.     'weight' => 3,
  5.   );
  6.  
  7.   // Change the weights of the qty and total columns.
  8.   $element['#columns']['qty']['weight'] = 4;
  9.   $element['#columns']['total']['weight'] = 5;
  10.  
  11.   // Loop through each row and if the entity exists in the variables array, 
  12.   foreach ($element['#rows'] as $position => $data) {
  13.     if (isset($element[$position]['#entity']->sell_price)) {
  14.       $element['#rows'][$position]['unit_price'] = array(
  15.         '#markup' => uc_currency_format($element[$position]['#entity']->sell_price),
  16.       );
  17.     }
  18.  
  19.   }

The final results

The Full Code

  1. function MYTHEME_tapir_table($variables) {
  2.   $element = $variables['element'];
  3.  
  4.   // Add the unit price to the $element['#columns'] array.
  5.   $element['#columns']['unit_price'] = array(
  6.     'cell' => t('Unit Price'),
  7.     'weight' => 3,
  8.   );
  9.  
  10.   // Change the weights of the qty and total columns.
  11.   $element['#columns']['qty']['weight'] = 4;
  12.   $element['#columns']['total']['weight'] = 5;
  13.  
  14.   foreach ($element['#rows'] as $position => $data) {
  15.     //$element[$position]['#entity']['sell_price']
  16.     if (isset($element[$position]['#entity']->sell_price)) {
  17.       $element['#rows'][$position]['unit_price'] = array(
  18.         '#markup' => uc_currency_format($element[$position]['#entity']->sell_price),
  19.       );
  20.     }
  21.  
  22.   }
  23.  
  24.   $header = array();
  25.   $rows = array();
  26.  
  27.   // First sort the columns by weight.
  28.   uasort($element['#columns'], 'uc_weight_sort');
  29.  
  30.   // Loop through the columns and create the header array.
  31.   foreach ($element['#columns'] as $col_id => $col_data) {
  32.     // Add the cell if available.
  33.     if (!isset($col_data['access']) || $col_data['access'] !== FALSE) {
  34.       $header[] = $col_data['cell'];
  35.     }
  36.   }
  37.  
  38.   // Loop through the row data and create rows with the data in the right order.
  39.   foreach ($element['#rows'] as $data) {
  40.     $attributes = array();
  41.     $row = array();
  42.  
  43.     // Loop through each column in the header.
  44.     foreach ($element['#columns'] as $col_id => $col_data) {
  45.       // If this row defines cell data for the current column...
  46.       if ((!isset($col_data['access']) || $col_data['access'] !== FALSE) && isset($data[$col_id])) {
  47.         $cell = array();
  48.         if (isset($data[$col_id]['#cell_attributes']) && is_array($data[$col_id]['#cell_attributes'])) {
  49.           foreach ($data[$col_id]['#cell_attributes'] as $property => $value) {
  50.             if ($property == 'colspan' && $value == 'full') {
  51.               // Extend full-width cells to the number of columns actually
  52.               // displayed.
  53.               $value = count($header);
  54.             }
  55.             $cell[$property] = $value;
  56.           }
  57.           $cell['data'] = drupal_render($data[$col_id]);
  58.         }
  59.         else {
  60.           $cell = drupal_render($data[$col_id]);
  61.         }
  62.         // Add it to the row array.
  63.         $row[] = $cell;
  64.       }
  65.     }
  66.  
  67.     // Merge the row data into a single row array along with the attributes.
  68.     if (isset($data['#attributes'])) {
  69.       $row = array_merge(array('data' => $row), (array)$data['#attributes']);
  70.     }
  71.  
  72.     // Add the current row to the table rows array.
  73.     $rows[] = $row;
  74.   }
  75.  
  76.   // Return the rendered table.
  77.   $options = array(
  78.     'header' => $header,
  79.     'rows' => $rows,
  80.   );
  81.   if (isset($element['#attributes'])) {
  82.     $options['attributes'] = (array)$element['#attributes'];
  83.   }
  84.   if (isset($element['#title'])) {
  85.     $options['caption'] = $element['#title'];
  86.   }
  87.  
  88.   return theme('table', $options) . drupal_render_children($element);
  89. }


Comments