最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Opencart successful order ID and Total from JavaScript - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Nov 13, 2013 at 17:28 Roman ShaminRoman Shamin 232 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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['ment']);
        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.

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

发布评论

评论列表(0)

  1. 暂无评论