Post by bhy » Fri Mar 06, 2020 8:44 pm

Hello, my OC version is 2.3.0.2. I'm trying to put the following Google Tag javascript in the <head> tag of a successful checkout page.

Code: Select all

<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-661773975/VUAlCOOF3cgBEJe9x7sC',
      'value': '<?php echo $total; ?>',
      'currency': 'CZK',
      'transaction_id': ''
  });
</script>
I wanted to combine two solutions for a similar problem.

This one: viewtopic.php?t=202389#p715162
Puts code in the <head> tag if the page is checkout success. This works fine.

Then this one: https://stackoverflow.com/questions/199 ... javascript
Should make the 'order_id' and 'total' variables available from the template files on checkout success page.
This doesn't work for me, I keep getting a PHP undefined variable 'order_id' error. If I remove the 'order_id' condition, the undefined variable 'total' error is within the javascript (so at least the customer can't see it).

What I did so far:
In my catalog/controller/common/header.php file, in the index() method, I added:

Code: Select all

if (!empty($this->request->get['route'])) {
    $data['route'] = $this->request->get['route'];
} else {
    $data['route'] = '';
}
In my catalog/view/theme/beauty/template/common/header.tpl, I added

Code: Select all

<?php if (!empty($route) && $route == 'checkout/success') { ?>
<?php if($order_id) { ?>
<script>
  gtag('event', 'conversion', {
      'send_to': 'AW-661773975/VUAlCOOF3cgBEJe9x7sC',
      'value': '<?php echo $total; ?>',
      'currency': 'CZK',
      'transaction_id': ''
  });
</script>
<?php } ?>
<?php } ?>
In my catalog/controller/checkout/success.php I added the following to the index function:

Code: Select all

public function index() {
    $data['order_id'] = 0; // <-- NEW LINE
    $data['total'] = 0; // <-- NEW LINE

    if (isset($this->session->data['order_id'])) {
        $data['order_id'] = $this->session->data['order_id']; // <-- NEW LINE
        $data['total'] = $this->cart->getTotal(); // <-- NEW LINE
The reason why I can't do this via success.tpl is that the customer (a marketing guy) wants the script in the <head> and success.tpl pulls the <head> from header.tpl.

What am I doing wrong? I'm not very good in OC's internal matters, but is it possible that in order to make the 'order_id' and 'total' variables available from header.tpl, I need to put the lines commented as NEW LINE in header.php instead of success.php? If that's the case, can I simply find an index() function in header.php and copy those lines there the same way as for success.php, or do I need to do it differently?

Thank you very much.

Petr Břeň

bhy
New member

Posts

Joined
Fri Jul 17, 2015 7:28 pm

Post by straightlight » Sat Mar 07, 2020 6:28 am

Simply build an event which loads a JS file. This could be done with 3-5 lines of codes, including the orders. There is no need to alter core files. However, if you do not know how to accomplish this, you could always look for an extension on the Marketplace or submit a new service request in the Commercial Support section of the forum or contact me directly to get this done as a custom job.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by ADD Creative » Sun Mar 08, 2020 6:08 am

bhy wrote:
Fri Mar 06, 2020 8:44 pm
What am I doing wrong? I'm not very good in OC's internal matters, but is it possible that in order to make the 'order_id' and 'total' variables available from header.tpl, I need to put the lines commented as NEW LINE in header.php instead of success.php? If that's the case, can I simply find an index() function in header.php and copy those lines there the same way as for success.php, or do I need to do it differently?
Yes, you would need to put the code in catalog/controller/common/header.php. But you also need to check the route is checkout/success before your new code.

www.add-creative.co.uk


Expert Member

Posts

Joined
Sat Jan 14, 2012 1:02 am
Location - United Kingdom
Who is online

Users browsing this forum: No registered users and 387 guests