How can I add a line item in Ubercart 2 that adds up the total cost of all items, not the sell price? I have tried to clone the generic line item hook, and add something like this for the callback:

for each($op->products as item){
  $cost += $item->cost;

I need this line item to appear in the cart (I'm using the ajax cart), in the order pane before the user completes checkout, and in the emails that the store owner and user get. Do I need to create a little module for this code outside of uc_order? I don't remember the code exactly as it's on my work computer, but I think I'm putting it in the wrong place. Thanks for any pointers.

1 Answer 1


I created a line item using hook_uc_line_item() then added the line item in the hook_uc_order().

Final products looks something like:

 * Implement hook_uc_line_item()
function my_module_uc_line_item() {

  $items[] = array(
    'id' => 'handling_fee',
    'title' => t('Handling Fee'),
    'weight' => 5,
    'stored' => TRUE,
    'calculated' => TRUE,
    'display_only' => FALSE,
  return $items;

 * Implement hook_uc_order()
function my_module_uc_order($op, $order, $arg2) {

  // This is the handling fee. Add only if the user is a professional and there
  // are shippable products in the cart.
  if  ($op == 'save') {
    global $user;

    if (in_array('professional', array_values($user->roles))) {

      // Determine if the fee is needed. If their are shippable items in the cart.
      $needs_fee = FALSE;
      foreach ($order->products as $pid => $product) {
        if ($product->shippable) {
          $needs_fee = TRUE;

      $line_items = uc_order_load_line_items($order);

      // Determine if the fee has already been applied.
      $has_fee = FALSE;
      foreach ($line_items as $key => $line_item) {
        if ($line_item['type'] == 'handling_fee') {
          $has_fee = $line_item['line_item_id'];

      // If the cart does not already have the fee and their are shippable items
      // add them.
      if ($has_fee === FALSE && $needs_fee) {
        uc_order_line_item_add($order->order_id, 'handling_fee', "Handling Fee", 9.95 , 5, null);
      // If it has a fee and does not need one delete the fee line item.
      elseif ($has_fee !== FALSE && !$needs_fee) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.