Drupal 7 添加ubercart行项目并更新订单总额
我不知道这为什么这么复杂,但我想做的就是在用户更改Ubercart中的付款方式时在我的订单中添加一个行项目。我通过在我的付款方式中添加以下行来实现这一点 uc_订单_行_项目_添加($order->order_id,'Pay at door','Pay at door',5) 这似乎添加了行项目,但订单总额没有得到更新。当我刷新页面时,我可以看到行项目被添加到那里。显然,我不想刷新屏幕,我想在我的付款方式回调期间显示新订单总额和行项目。我想这是通过调用jquery实现的,但我在Google中找不到任何有用的东西Drupal 7 添加ubercart行项目并更新订单总额,drupal-7,ubercart,Drupal 7,Ubercart,我不知道这为什么这么复杂,但我想做的就是在用户更改Ubercart中的付款方式时在我的订单中添加一个行项目。我通过在我的付款方式中添加以下行来实现这一点 uc_订单_行_项目_添加($order->order_id,'Pay at door','Pay at door',5) 这似乎添加了行项目,但订单总额没有得到更新。当我刷新页面时,我可以看到行项目被添加到那里。显然,我不想刷新屏幕,我想在我的付款方式回调期间显示新订单总额和行项目。我想这是通过调用jquery实现的,但我在Google中找不
有人能帮忙吗?希望这个答案对你来说不会太晚,但是你试过以下方法吗:
$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_订单中对我起作用,在那里我遇到了类似的问题,即直到第二次刷新后才更新税收
如果有帮助,我的完整示例代码如下:
<?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;
}