How to add handling fee with Ubercart 3

If you want to add handling fee to your order, there is no contributed module available for Ubercart 3 although I can see one for D6 version.

So for Drupal 7, I wrote my own small piece of code to achieve it.


Pretty easy though, see below:


function MYMODULE_uc_line_item() {
  $items[] = array(
    'id' => 'customfee',
    'title' => t('Handling Fee'),
    'weight' => -1000,
    'default' => FALSE,
    'stored' => TRUE,
    'add_list' => TRUE,
    'calculated' => TRUE,
    'callback' => 'uc_line_item_generic',
  );

  return $items;
}

function uc_line_item_generic($op, $order) {
return array(array(
          'id' => 'customfee',
          'title' => t('Handling Fee'),
          'amount' => 2.5,
          'weight' =>-1000,
        ));
}

function MYMODULE_uc_order($op, $order, $arg2) {
  switch ($op) {
    case 'save':
      $changes = array();
      $line_items = uc_line_item_generic('load', $order);
      if (is_array($order->line_items)) {
        foreach ($order->line_items as $i => $line) {
          if ($line['type'] == 'customfee') {
            $delete = TRUE;
            foreach ($line_items as $id => $new_line) {
              if ($new_line['title'] == $line['title']) {
                if ($new_line['amount'] != $line['amount']) {
                  $context['subject']['line_item'] = $new_line;
                  uc_order_update_line_item($line['line_item_id'], $new_line['title'], $new_line['amount'], $new_line['data']);
                  $order->line_items[$i]['amount'] = $new_line['amount'];
                  $changes[] = t('Changed %title to %amount.', array('%amount' => uc_currency_format($new_line['amount']), '%title' => $new_line['title']));
                }
                unset($line_items[$id]);
                $delete = FALSE;
                break;
              }
            }
            if ($delete) {
              uc_order_delete_line_item($line['line_item_id']);
              unset($order->line_items[$i]);
              $changes[] = t('Removed %title.', array('%title' => $line['title']));
            }
          }
        }
      }
      if (is_array($line_items)) {
        foreach ($line_items as $line) {
          uc_order_line_item_add($order->order_id, $line['id'], $line['title'], $line['amount'], '-100', $line['data']);
          $line['type'] = 'customfee';
          $order->line_items[] = $line;
          $context['subject']['line_item'] = $line;
          $changes[] = t('Added %amount for %title.', array('%amount' => uc_currency_format($line['amount']), '%title' => $line['title']));
        }
      }
      if (count($changes)) {
        uc_order_log_changes($order->order_id, $changes);
      }
    break;
  }
}

Share this post

Leave a comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.