4

I don't know why this is so complicated but all I want to do is to add a line item in my order when user changes payment method in Ubercart. I managed to do this by adding the following line in my payment method.

uc_order_line_item_add($order->order_id, 'Pay at door','Pay at door',5);

This seems to add the line item but the order total does not get updated. When I refresh the page, I can see the line item being added there. Obviously, I don't want to refresh the screen and I want the new order total and the line item to be displayed during callback of my payment method. I guess this is achieved by calling jquery but I could not find anything useful in Google.

Can someone help?

Mert Sevinc
  • 929
  • 1
  • 8
  • 25

1 Answers1

5

Hope this answer isn't too late for you, but have you tried the following:

$order->line_items[] = uc_order_line_item_add($order->order_id, 'Pay at door','Pay at door',5);

The issue is that uc_order_line_item_add simply updates the database not the current order object that is being passed around and is later used to calculate totals etc, thankfully uc_order_line_item_add returns a line item array that can simply be appended to the order object. I am not sure that it will work inside your payment method but it works for me in hook_uc_order, where I had the similar issue of the tax not being updated until a second refresh.

In case it helps my full example code is below:

<?php

function my_module_uc_order($op, &$order, $arg2) {
  switch ($op) {
    case 'presave':
      // using presave as taxes are calculated during save
      $line_item_id = FALSE;
      if (!is_array($order->line_items)) {
        $order->line_items = array();
      }
      else {
        foreach ($order->line_items as $index => $line_item) {
          if ($line_item['type'] == 'my_line_item') {
            $line_item_id = $line_item['line_item_id'];
            break;
          }
        }
      }

      if ($line_item_id === FALSE) {
        // Add item.
        // Dummy amount for testing.
        $amount = 3;
        // uc_order_line_item_add returns a line_item array so we can just add it to the order object.
        $order->line_items[] = uc_order_line_item_add($order->order_id, 'my_line_item', 'My Item Title (added)', $amount);
        // uc_order_line_item_add($order_id, $type, $title, $amount, $weight = NULL, $data = NULL).
      }
      else {
        // Update item.
        // Dummy amount for testing.
        $amount = 4;
        // uc_order_update_line_item($id, $title, $amount, $data = NULL).
        uc_order_update_line_item($line_item_id, 'My Item Title (updated)', $amount);
        // Manually modify amount.
        $order->line_items[$index]['amount'] = $amount;
      }
      break;
  }
}

/**
* Implements hook_uc_line_item().
*/

function my_module_uc_line_item() {
 $items[] = array(
  'id' => 'my_line_item',
  'title' => t('Custom text'),
  'weight' => 0,
  'default' => FALSE,
  'stored' => TRUE,
  'add_list' => TRUE,
  'calculated' => TRUE,
 );
 return $items;
}
Community
  • 1
  • 1
PAB
  • 51
  • 1
  • 3