添加显示总费用的订单项


10

如何在Ubercart 2中添加一个订单项,该订单项总计所有项目的总成本,而不是售价?我试图克隆通用订单项挂钩,并为回调添加如下内容:

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

我需要此订单项出现在购物车(我正在使用ajax购物车),用户完成结帐之前的订单窗格中以及商店所有者和用户收到的电子邮件中。我是否需要在uc_order之外为此代码创建一个小模块?我记不清代码与工作计算机上的代码完全相同,但是我认为我将其放在错误的位置。感谢您的指导。

Answers:


1

我使用hook_uc_line_item()创建了一个订单项,然后将该订单项添加到hook_uc_order()中。

最终产品看起来像:

/*
 * 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) {
        uc_order_delete_line_item($has_fee);
      }
    }
  }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.