Here's the modification we ended up using for OpenCart version 2.0.1.1 (we are using a custom theme, as well):
Code: Select all
<file name="catalog/controller/checkout/success.php">
<operation>
<search position="before"><![CDATA[
$this->cart->clear();
]]></search>
<add><![CDATA[
$this->load->model('account/order');
$data['orderDetails'] = $this->model_account_order->getOrder($this->session->data['order_id']);
$data['orderProducts'] = $this->model_account_order->getOrderProducts($this->session->data['order_id']);
$data['orderTotals'] = $this->model_account_order->getOrderTotals($this->session->data['order_id']);
]]></add>
</operation>
</file>
And
Code: Select all
<file name="catalog/view/theme/pavilion/template/common/success.tpl">
<operation>
<search position="before"><![CDATA[
<div class="tb_submit clearfix">
]]></search>
<add><![CDATA[
<?php if( isset($orderDetails) && isset($orderProducts) && isset($orderTotals) ) { ?>
<script>
var tsOrderDetails = <?= json_encode($orderDetails) ?>;
var tsorderProducts = <?= json_encode($orderProducts) ?>;
var tsorderTotals = <?= json_encode($orderTotals) ?>;
</script>
<script src="catalog/view/javascript/ecommerce.js"></script>
<?php } ?>
]]></add>
</operation>
</file>
On the ecommerce.js file:
Code: Select all
// shipping
if ( jQuery.grep(tsorderTotals, function(e){ return e.code == 'shipping'}).length == 1 ) { tsShipping = jQuery.grep(tsorderTotals, function(e){ return e.code == 'shipping'})[0].value; }
else { tsShipping = 0; }
//tax
if ( jQuery.grep(tsorderTotals, function(e){ return e.code == 'tax'}).length == 1 ) { tsTax = jQuery.grep(tsorderTotals, function(e){ return e.code == 'tax'})[0].value; }
else { tsTax = 0; }
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
'id': tsOrderDetails.order_id,
'affiliation': 'NAME OF COMPANY',
'revenue': tsOrderDetails.total,
'shipping': tsShipping,
'tax': tsTax
});
for(var i = 0; i < tsorderProducts.length; i++) {
ga('ecommerce:addItem', {
'id': tsOrderDetails.order_id,
'name': tsorderProducts[i]['name'],
'sku': tsorderProducts[i]['product_id'],
'category': '',
'price': tsorderProducts[i]['price'],
'quantity': tsorderProducts[i]['quantity']
});
}
ga('ecommerce:send');
Hopefully this helps someone!