ecommerce.php
8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * @copyright    OCTemplates
 * @support      https://octemplates.net/
 * @license      LICENSE.txt
 */
class ControllerOCTemplatesEventsEcommerce extends Controller {
    public function setOctEcommerceData(&$route, &$data) {
        if (isset($this->session->data['order_id'])) {
            $analytics_status = $this->config->get('analytics_oct_analytics_status');
            $analytics_google_ecommerce = $this->config->get('analytics_oct_analytics_google_ecommerce');
            if ($analytics_status && $analytics_google_ecommerce == "on") {
                $data['oct_analytics_google_ecommerce'] = $analytics_google_ecommerce;
                $this->load->model('account/order');
                $this->load->model('catalog/product');
                $data['current_route'] = $route;
                $data['oct_analytics_order_id'] = $this->session->data['order_id'];
                $order_products = $this->model_account_order->getOrderProducts($this->session->data['order_id']);
                $data['currency_id'] = $currency_id = $this->currency->getId($this->session->data['currency']);
                $data['currency_code'] = $currency_code = $this->session->data['currency'];
                $data['currency_value'] = $currency_value = $this->currency->getValue($this->session->data['currency']);
                $shipping = 0;
                $totals = 0;
                $query_total = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$this->session->data['order_id'] . "' ORDER BY sort_order ASC");
                foreach ($query_total->rows as $total) {
                    if ($total['value'] > 0) {
                        if ($total['code'] == "shipping") {
                            $shipping += $total['value'];
                        }
                        if ($total['code'] == "total") {
                            $totals += $total['value'];
                        }
                    }
                }
                $data['total'] = $totals * $currency_value;
                $data['shipping'] = $shipping * $currency_value;
                $data['affiliation'] = $this->config->get('config_name');
                foreach ($order_products as $product) {
                    $product_info = $this->model_catalog_product->getProduct($product["product_id"]);
                    if ($product_info) {
                        $i = 0;
                        $categories_data = '';
                        $query_category = $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)$this->config->get('config_language_id') . "'");
                        foreach ($query_category->rows as $category) {
                            $i++;
                            if ($i <= 5) {
                                $categories_data .= $category['name'] . '/';
                            }
                        }
                        $categories_data = rtrim($categories_data, '/');
                        $options_data = '';
                        $options_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$this->session->data['order_id'] . "' AND order_product_id = '" . (int)$product['product_id'] . "'");
                        foreach ($options_query->rows as $option) {
                            if ($option['type'] != 'file') {
                                $options_data .= $option['name'] . ': ' . (utf8_strlen($option['value']) > 20 ? utf8_substr($option['value'], 0, 20) . '..' : $option['value']) . ' - ';
                            }
                        }
                        $options_data = rtrim($options_data, ' - ');
                        $price = (((float)$product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0)) * $currency_value);
                        $data['oct_analytics_order_products'][] = [
                            'id' => $product['product_id'],
                            'name' => $product['name'],
                            'price' => $price,
                            'quantity' => $product['quantity'],
                            'brand' => $product_info['manufacturer'],
                            'category' => $categories_data,
                            'variant' => $options_data
                        ];
                    }
                }
                $google_data = [
                    'transaction_id' => $this->session->data['order_id'],
                    'affiliation' => $data['affiliation'],
                    'value' => (float)$data['total'],
                    'currency' => $data['currency_code'],
                    'shipping' => $data['shipping'],
                    'items' => $data['oct_analytics_order_products']
                ];
                $data['toGoogle'] = json_encode($google_data);
            }
            $oct_success_data = $this->config->get('theme_oct_deals_data_osucsess');
            if (isset($oct_success_data['status'])) {
                $oct_order_id = $this->session->data['order_id'];
                if ($this->customer->isLogged()) {
                    $oct_sucsess_title = isset($oct_success_data['reg']['title'][(int)$this->config->get('config_language_id')]) ? $oct_success_data['reg']['title'][(int)$this->config->get('config_language_id')] : '';
                    $oct_sucsess_text = isset($oct_success_data['reg']['text'][(int)$this->config->get('config_language_id')]) ? $oct_success_data['reg']['text'][(int)$this->config->get('config_language_id')] : '';
                    $oct_replace = [
                        '[order_id]' => (int)$oct_order_id,
                        '[contact_link]' => $this->url->link('information/contact'),
                        '[account_link]' => $this->url->link('account/account'),
                        '[account_order_link]' => $this->url->link('account/order'),
                        '[account_download_link]' => $this->url->link('account/download'),
                    ];
                    $data['heading_title'] = $oct_sucsess_title ? strip_tags(html_entity_decode(str_replace(array_keys($oct_replace), array_values($oct_replace), $oct_sucsess_title), ENT_QUOTES, 'UTF-8')) : $this->language->get('heading_title');
                    $data['text_message'] = html_entity_decode(str_replace(array_keys($oct_replace), array_values($oct_replace), $oct_sucsess_text), ENT_QUOTES, 'UTF-8');
                } else {
                    $oct_sucsess_title = isset($oct_success_data['noreg']['title'][(int)$this->config->get('config_language_id')]) ? $oct_success_data['noreg']['title'][(int)$this->config->get('config_language_id')] : '';
                    $oct_sucsess_text = isset($oct_success_data['noreg']['text'][(int)$this->config->get('config_language_id')]) ? $oct_success_data['noreg']['text'][(int)$this->config->get('config_language_id')] : '';
                    $oct_replace = [
                        '[order_id]' => (int)$oct_order_id,
                        '[contact_link]' => $this->url->link('information/contact'),
                    ];
                    $data['heading_title'] = $oct_sucsess_title ? strip_tags(html_entity_decode(str_replace(array_keys($oct_replace), array_values($oct_replace), $oct_sucsess_title), ENT_QUOTES, 'UTF-8')) : $this->language->get('heading_title');
                    $data['text_message'] = html_entity_decode(str_replace(array_keys($oct_replace), array_values($oct_replace), $oct_sucsess_text), ENT_QUOTES, 'UTF-8');
                }
            }
            $this->document->setTitle(isset($data['heading_title']) ? $data['heading_title'] : $this->language->get('heading_title'));
            $this->config->set('oct_success_order', $data);
        }
    }
    public function getOctEcommerceData(&$route, &$data) {
        $ecommerce_data = $this->config->get('oct_success_order');
        
        if ($ecommerce_data) {
            $data = array_merge($data, $ecommerce_data);
        }
    }
}