【问题标题】:Adding a ubercart line item and updating order total添加 ubercart 订单项并更新订单总额
【发布时间】:2012-11-28 07:24:48
【问题描述】:

我不知道为什么这么复杂,但我想做的就是当用户在 Ubercart 中更改付款方式时在我的订单中添加一个订单项。我设法通过在我的付款方式中添加以下行来做到这一点。

uc_order_line_item_add($order->order_id, '上门付款','上门付款',5);

这似乎添加了订单项,但订单总额没有更新。当我刷新页面时,我可以看到那里正在添加行项目。显然,我不想刷新屏幕,我希望在我的付款方式回调期间显示新订单总额和行项目。我想这是通过调用 jquery 来实现的,但我在 Google 中找不到任何有用的东西。

有人可以帮忙吗?

【问题讨论】:

    标签: drupal-7 ubercart


    【解决方案1】:

    希望这个答案对您来说还不算太晚,但是您是否尝试过以下方法:

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

    问题是 uc_order_line_item_add 只是更新数据库而不是当前传递的订单对象,后来用于计算总计等,幸运的是 uc_order_line_item_add 返回一个可以简单地附加到订单对象的行项目数组。我不确定它是否可以在您的付款方式中使用,但它在 hook_uc_order 中对我有用,在那里我遇到了类似的税收问题,直到第二次刷新才更新。

    如果它有助于我的完整示例代码如下:

    <?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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 2016-07-25
      • 2013-01-13
      • 2019-07-15
      • 1970-01-01
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多