Total order not updated for the custom line item

120 Views Asked by At

I am using ubercart 3 with drupal 7. I want to create custom line item for additional handling charges. Below code is working fine but the additional handling amount is not added with the total, but its added with the subtotal.

What am I doing wrong?

 function mycustom_uc_order($op, $order, $arg2) {
 switch ($op) {
 case 'save':   
  $package_lineitem_id = $ups_charges = $package_lineitem_index = '';
  $line_items = uc_order_load_line_items($order);
  foreach ($line_items as $key => $line_item) {
    if ($line_item['type'] == 'shipping' && $line_item['amount'] != '') {
      $ups_charges = $line_item['line_item_id'];
    } elseif($line_item['type'] == 'custom_package_charges'){
      $package_lineitem_id = $line_item['line_item_id'];
      $package_lineitem_index = $key;
    }
  }        
      $pack_charges = 5; 
    // If packaging charges line item exists update else create a new one
    if(empty($package_lineitem_id)){
      $order->line_items[] = uc_order_line_item_add($order->order_id, 'custom_package_charges', 'Additional Handling Charges for Packaging', $pack_charges,5);
    } else { 
      uc_order_update_line_item($package_lineitem_id, 'Additional Handling Charges for Packaging', $pack_charges);
      $order->line_items[$package_lineitem_index]['amount'] = $pack_charges;
    }   

  break;    
  }
}
1

There are 1 best solutions below

0
Shiks On

You need to use hook_uc_line_item to define your custom line item. For example:

/**
* Implements hook_uc_line_item().
*/

function mycustom_uc_line_item() {
$items[] = array(
 'id' => 'custom_package_charges',
 'title' => t('Custom text'),
 'weight' => 0,
 'default' => FALSE,
 'stored' => TRUE,
 'add_list' => TRUE,
 'calculated' => TRUE,
);
return $items;
}