Skip to content

Instantly share code, notes, and snippets.

@herrquark
Created February 27, 2019 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herrquark/7548ccfe8a76d47f668402210a9dca2a to your computer and use it in GitHub Desktop.
Save herrquark/7548ccfe8a76d47f668402210a9dca2a to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Google Analytics Ecommerce</name>
<code>google-analytics-ecommerce</code>
<version>4.0.0</version>
<author>OpenCart Brasil</author>
<link>http://www.opencartbrasil.com.br</link>
<file path="catalog/model/checkout/order.php">
<operation>
<search><![CDATA[public function getOrder($order_id) {]]></search>
<add position="before"><![CDATA[
public function getGTAG($order_id) {
$order_query = $this->db->query("SELECT o.order_id, o.store_name, o.language_id, o.currency_code, o.currency_value, o.total FROM `" . DB_PREFIX . "order` o WHERE o.order_id = '" . (int)$order_id . "'");
if ($order_query->num_rows) {
$language_id = $order_query->row['language_id'];
$currency_value = $order_query->row['currency_value'];
$order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");
foreach ($order_product_query->rows as $product) {
$i = 0;
$category_data = '';
$category_query = $this->db->query("SELECT cd.name FROM `" . DB_PREFIX . "product_to_category` pc INNER JOIN `" . DB_PREFIX . "category_description` cd ON pc.category_id = cd.category_id WHERE pc.product_id = '" . (int)$product['product_id'] . "' AND cd.language_id = '" . (int)$language_id . "'");
foreach ($category_query->rows as $category) { $i++;
if ($i <= 5) {
$category_data .= $category['name'] . '/';
}
}
$category_data = rtrim($category_data, '/');
$option_data = '';
$order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");
foreach ($order_option_query->rows as $option) {
if ($option['type'] != 'file') {
$option_data .= $option['name'] . ': ' . (utf8_strlen($option['value']) > 20 ? utf8_substr($option['value'], 0, 20) . '..' : $option['value']) . ' - ';
}
}
$option_data = rtrim($option_data, ' - ');
$products[] = array(
'id' => $product['model'],
'name' => $product['name'],
'category' => $category_data,
'variant' => $option_data,
'price' => (($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0)) * $currency_value),
'quantity' => $product['quantity']
);
}
$order_shipping = 0;
$order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");
foreach ($order_total_query->rows as $total) {
if ($total['value'] > 0) {
if ($total['code'] == "shipping") {
$order_shipping += $total['value'];
}
}
}
return array(
'transaction_id' => $order_query->row['order_id'],
'affiliation' => $order_query->row['store_name'],
'value' => $order_query->row['total'] * $currency_value,
'currency' => $order_query->row['currency_code'],
'shipping' => $order_shipping * $currency_value,
'items' => $products
);
} else {
return false;
}
}
]]></add>
</operation>
</file>
<file path="catalog/controller/checkout/success.php">
<operation>
<search><![CDATA[public function index() {]]></search>
<add position="after"><![CDATA[
$data['gtag_event'] = '';
if (isset($this->session->data['order_id'])) {
if ($this->config->get('analytics_google_status')) {
$this->load->model('checkout/order');
$event_info = $this->model_checkout_order->getGTAG($this->session->data['order_id']);
if ($event_info) {
$data['gtag_event'] .= '<script>'."\n";
$data['gtag_event'] .= 'gtag(\'event\', \'purchase\', {'."\n";
$data['gtag_event'] .= sprintf('"transaction_id": "%s", "affiliation": "%s", "value": %s, "currency": "%s", "shipping": %s,', $event_info['transaction_id'], $event_info['affiliation'], $event_info['value'], $event_info['currency'], $event_info['shipping'])."\n";
if (isset($event_info['items']) && is_array($event_info['items'])) {
$data['gtag_event'] .= '"items": [ '."\n";
foreach ($event_info['items'] as $item) {
$data['gtag_event'] .= sprintf('{ "id": "%s", "name": "%s", "category": "%s", "variant": "%s", "quantity": %s, "price": "%s" },', $item['id'], $item['name'], $item['category'], $item['variant'], $item['quantity'], $item['price'])."\n";
}
$data['gtag_event'] .= ' ]'."\n";
}
$data['gtag_event'] .= ' });'."\n";
$data['gtag_event'] .= '</script>';
}
}
}
]]></add>
</operation>
</file>
<file path="catalog/view/theme/*/template/common/success.twig">
<operation>
<search><![CDATA[{{ footer }}]]></search>
<add position="before"><![CDATA[
{% if gtag_event %}{{ gtag_event }}{% endif %}
]]></add>
</operation>
</file>
</modification>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment