Opencart successful order ID and Total from JavaScript

删除回忆录丶 提交于 2019-12-01 11:51:38

The problem here is that on success page all the order data is already unset (deleted) from session variables. That's why your code cannot succeed.

Look into catalog/controller/checkout/success.php and change the beginning of the index() function to this:

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');

Now you have the order_id and cart's total values stored in template variables, so just use them in your success.tpl (not header):

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

This should be enough.

FreestyleMD

The previous answer needs to be updated for later versions of Opencart for 2.2.0 it is

$data['order_id'] = 0;
$data['total'] = 0;

and
$data['order_id'] = $this->session->data['order_id'];
$data['total'] = $this->cart->getTotal();

instead of the new lines indicated previously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!