1

I need to run JavaScript on the successful order's page and get two things: order ID and total order amount. The code looks like:

<script type="text/javascript">
    // Some code here
    arr.push([
        "create_order",
        {order_id: "*order_id*", sum: *sum*}
    ]);
</script>

Questions

  1. Where should I paste my script? If into success.tpl than where exactly? If into header.tpl than how to run it only on the page of successful order?
  2. Which variables I should to use? I have tried this, it did not work:
{order_id: "<?php echo $order_id; ?>", sum: <?php echo $product_total; ?>}

P. S. Opencart version is 1.5.6

4

2 回答 2

7

这里的问题是,在成功页面上,所有订单数据都已从会话变量中取消设置(删除)。这就是您的代码无法成功的原因。

查看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_idand cart 的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 } ?>

这应该足够了。

于 2013-11-14T08:54:02.773 回答
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();

而不是之前指出的新行

于 2017-01-23T15:38:30.500 回答