OpenCart 3.0+ Tracking Setup

For OpenCart version v3.0 and higher, first locate the file \catalog\controller\checkout\success.php.

As always, make a backup of the file first. Once you've made a backup, look for this line in the file:

$this->cart->clear();

Above it, add the following code:

/* begin ShareASale tracking business logic */ //discounts $_sasCoupon = array(); $this->load->model('extension/total/coupon'); if(@$this->session->data['coupon']){ $_sasCoupon = $this->model_extension_total_coupon->getCoupon($this->session->data['coupon']); } //product-level tracking //product model necessary to get SKU since cart->getProducts() returns different data than product->getProducts()... $this->load->model('catalog/product'); $_sasSkulist = []; $_sasPricelist = []; $_sasQuantitylist = []; $_sasProductCount = $this->cart->countProducts(); foreach ($this->cart->getProducts() as $product) { $_sasSkulist[] = $this->model_catalog_product->getProduct($product['product_id'])['sku']; $_sasQuantitylist[] = $product['quantity']; //check if product or cart-wide discount, and check if flat (type "F") or percent (type "P"). Ignore free shipping discounts. if(!empty($_sasCoupon['product'])){ if(in_array($product['product_id'], $_sasCoupon['product'])){ $_sasProductSubtotal = ($_sasCoupon['type'] == 'P') ? $product['price'] * (1 - ($_sasCoupon['discount'] / 100)) : (($_sasCoupon['type'] == 'F') ? $product['price'] - $_sasCoupon['discount'] : $product['price']); }else{ $_sasProductSubtotal = $product['price']; } }else{ $_sasProductSubtotal = (@$_sasCoupon['type'] == 'P') ? $product['price'] * (1 - ($_sasCoupon['discount'] / 100)) : ((@$_sasCoupon['type'] == 'F') ? $product['price'] - ($_sasCoupon['discount'] / $_sasProductCount) : $product['price']); } $_sasPricelist[] = round($_sasProductSubtotal, 2); } $_sasProductSubtotal = array_sum(array_map(function($price, $quantity) { return $price * $quantity; }, $_sasPricelist, $_sasQuantitylist)); //check for store credit and apply to final balance $_sasBalance = $this->db->query("SELECT SUM(amount) AS balance FROM " . DB_PREFIX . "customer_transaction WHERE customer_id = '" . (int)$this->customer->getId() . "' AND order_id <> '" . (int)$this->session->data['order_id'] . "'")->row['balance']; if($_sasBalance){ //compare store credit to after-discount product subtotal to get percentage paid by store credit, if not its entirety $_sasShare = $_sasBalance / $_sasProductSubtotal > 1 ? 1 : $_sasBalance / $_sasProductSubtotal; $_sasPricelist = array_map(function($price) use($_sasShare) { return $price * (1 - $_sasShare); }, $_sasPricelist); $_sasTotal = $_sasProductSubtotal = array_sum(array_map(function($price, $quantity) { return $price * $quantity; }, $_sasPricelist, $_sasQuantitylist)); }else{ $_sasTotal = $_sasProductSubtotal; } $data['_sasProducts'] = [implode(',', $_sasSkulist), implode(',', $_sasPricelist), implode(',', $_sasQuantitylist)]; //customer status tracking if ($this->customer->isLogged() && $this->customer->getId() != 0) { $totalOrders = $this->db->query("SELECT COUNT(`order_id`) AS `total` FROM `" . DB_PREFIX . "order` WHERE `customer_id` = '" . (int)$this->customer->getId() . "'"); $data['_sasIsCustomerNew'] = (int)$totalOrders->row['total'] > 1 ? 0 : 1; }else { $data['_sasIsCustomerNew'] = ''; } //standard order total and order ID tracking $data['_sasTotal'] = $_sasTotal; $data['_sasOrderId'] = $this->session->data['order_id']; //$data['_sasCurrencyCode'] = $this->session->data['currency']; _sasTotal always in USD even when currency changes... $data['_sasCoupons'] = !empty($_sasCoupon) ? $_sasCoupon['code'] : ''; /* end ShareASale tracking business logic */

Then find the file \catalog\view\theme\[your theme]\template\common\success.twig. If no theme is configured, use \default\ where [your theme] is written. Make a backup of this file too, and then look for this line:

{{ footer }}

Above it, add the following code:

Replace “XXXXX” in the script below with your ShareASale Merchant ID

<!-- begin ShareASale tracking --> <img src = "https://shareasale.com/sale.cfm?amount={{ _sasTotal }}&amp;tracking={{ _sasOrderId }}&amp;transtype=sale&amp;merchantID=!!MERCHANTID!!&amp;skulist={{ _sasProducts[0] }}&amp;pricelist={{ _sasProducts[1] }}&amp;quantitylist={{ _sasProducts[2] }}&amp;v=opencart3&amp;newcustomer={{ _sasIsCustomerNew }}&amp;couponcode={{ _sasCoupons }}" > <!--- end ShareASale tracking -->

 

Copyright ShareASale 2021