gpt4 book ai didi

drupal-7 - 添加 ubercart 订单项并更新订单总额

转载 作者:行者123 更新时间:2023-12-05 01:15:14 24 4
gpt4 key购买 nike

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

uc_order_line_item_add($order->order_id, '上门付款','上门付款',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_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;
}

关于drupal-7 - 添加 ubercart 订单项并更新订单总额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13808447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com