我需要在成功的订单页面上运行JavaScript并获得两件事:订单ID和总订单金额。代码如下:
<script type="text/javascript">
// Some code here
arr.push([
"create_order",
{order_id: "*order_id*", sum: *sum*}
]);
</script>
问题
{order_id: "<?php echo $order_id; ?>", sum: <?php echo $product_total; ?>}
P上。 S. Opencart版本是1.5.6
答案 0 :(得分:8)
这里的问题是,在成功页面上,所有订单数据都已从会话变量中取消设置(删除)。这就是你的代码无法成功的原因。
查看catalog/controller/checkout/success.php
并将index()
函数的开头更改为:
public function index() {
$this->data['order_id'] = 0; // <-- NEW LINE
$this->data['total'] = 0; // <-- NEW LINE
if (isset($this->session->data['order_id'])) {
$this->data['order_id'] = $this->session->data['order_id']; // <-- NEW LINE
$this->data['total'] = $this->cart->getTotal(); // <-- NEW LINE
$this->cart->clear();
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['guest']);
unset($this->session->data['comment']);
unset($this->session->data['order_id']);
unset($this->session->data['coupon']);
unset($this->session->data['reward']);
unset($this->session->data['voucher']);
unset($this->session->data['vouchers']);
}
$this->language->load('checkout/success');
现在您已将order_id
和购物车的total
值存储在模板变量中,因此只需在success.tpl
(非标题)中使用它们:
<?php if($order_id) { ?>
<script type="text/javascript">
// Some code here
arr.push([
"create_order",
{order_id: '<?php echo $order_id; ?>', sum: '<?php echo $total; ?>'}
]);
</script>
<?php } ?>
这应该足够了。
答案 1 :(得分:1)
以前的答案需要针对更高版本的Opencart进行更新
对于2.2.0
,它是
$data['order_id'] = 0;
$data['total'] = 0;
and
$data['order_id'] = $this->session->data['order_id'];
$data['total'] = $this->cart->getTotal();
而不是之前指出的新行