1<?php
2
3class ModelExtensionAdvertiseGoogle extends Model {
4    public function getHumanReadableCategory($product_id, $store_id) {
5        $this->load->config('googleshopping/googleshopping');
6
7        $google_category_result = $this->db->query("SELECT google_product_category FROM `" . DB_PREFIX . "googleshopping_product` pag WHERE pag.product_id = " . (int)$product_id . " AND pag.store_id = " . (int)$store_id);
8
9        if ($google_category_result->num_rows > 0) {
10            $google_category_id = $google_category_result->row['google_product_category'];
11            $google_categories = $this->config->get('advertise_google_google_product_categories');
12
13            if (!empty($google_category_id) && isset($google_categories[$google_category_id])) {
14                return $google_categories[$google_category_id];
15            }
16        }
17
18        $oc_category_result = $this->db->query("SELECT c.category_id FROM `" . DB_PREFIX . "product_to_category` p2c LEFT JOIN `" . DB_PREFIX . "category` c ON (c.category_id = p2c.category_id) WHERE p2c.product_id=" . (int)$product_id . " LIMIT 0,1");
19
20        if ($oc_category_result->num_rows > 0) {
21            return $this->getHumanReadableOpenCartCategory((int)$oc_category_result->row['category_id']);
22        }
23
24        return "";
25    }
26
27    public function getHumanReadableOpenCartCategory($category_id) {
28        $sql = "SELECT GROUP_CONCAT(cd.name ORDER BY cp.level SEPARATOR ' &gt; ') AS path FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category_description cd ON (cp.path_id = cd.category_id) WHERE cd.language_id=" . (int)$this->config->get('config_language_id') . " AND cp.category_id=" . (int)$category_id;
29
30        $result = $this->db->query($sql);
31
32        if ($result->num_rows > 0) {
33            return $result->row['path'];
34        }
35
36        return "";
37    }
38
39    public function getSizeAndColorOptionMap($product_id, $store_id) {
40        $color_id = $this->getOptionId($product_id, $store_id, 'color');
41        $size_id = $this->getOptionId($product_id, $store_id, 'size');
42
43        $groups = $this->googleshopping->getGroups($product_id, $this->config->get('config_language_id'), $color_id, $size_id);
44
45        $colors = $this->googleshopping->getProductOptionValueNames($product_id, $this->config->get('config_language_id'), $color_id);
46        $sizes = $this->googleshopping->getProductOptionValueNames($product_id, $this->config->get('config_language_id'), $size_id);
47
48        $map = array(
49            'groups' => $groups,
50            'colors' => count($colors) > 1 ? $colors : null,
51            'sizes' => count($sizes) > 1 ? $sizes : null,
52        );
53
54        return $map;
55    }
56
57    public function getCoupon($order_id) {
58        $sql = "SELECT c.code FROM `" . DB_PREFIX . "coupon_history` ch LEFT JOIN `" . DB_PREFIX . "coupon` c ON (c.coupon_id = ch.coupon_id) WHERE ch.order_id=" . (int)$order_id;
59
60        $result = $this->db->query($sql);
61
62        if ($result->num_rows > 0) {
63            return $result->row['code'];
64        }
65
66        return null;
67    }
68
69    public function getRemarketingProductIds($products, $store_id) {
70        $ecomm_prodid = array();
71
72        foreach ($products as $product) {
73            if (null !== $id = $this->getRemarketingProductId($product, $store_id)) {
74                $ecomm_prodid[] = $id;
75            }
76        }
77
78        return $ecomm_prodid;
79    }
80
81    public function getRemarketingItems($products, $store_id) {
82        $items = array();
83
84        foreach ($products as $product) {
85            if (null !== $id = $this->getRemarketingProductId($product, $store_id)) {
86                $items[] = array(
87                    'google_business_vertical' => 'retail',
88                    'id' => (string)$id,
89                    'name' => (string)$product['name'],
90                    'quantity' => (int)$product['quantity']
91                );
92            }
93        }
94
95        return $items;
96    }
97
98    protected function getRemarketingProductId($product, $store_id) {
99        $option_map = $this->getSizeAndColorOptionMap($product['product_id'], $store_id);
100        $found_color = "";
101        $found_size = "";
102
103        foreach ($product['option'] as $option) {
104            if (is_array($option_map['colors'])) {
105                foreach ($option_map['colors'] as $product_option_value_id => $color) {
106                    if ($option['product_option_value_id'] == $product_option_value_id) {
107                        $found_color = $color;
108                    }
109                }
110            }
111
112            if (is_array($option_map['sizes'])) {
113                foreach ($option_map['sizes'] as $product_option_value_id => $size) {
114                    if ($option['product_option_value_id'] == $product_option_value_id) {
115                        $found_size = $size;
116                    }
117                }
118            }
119        }
120
121        foreach ($option_map['groups'] as $id => $group) {
122            if ($group['color'] === $found_color && $group['size'] === $found_size) {
123                return $id;
124            }
125        }
126
127        return null;
128    }
129
130    protected function getOptionId($product_id, $store_id, $type) {
131        $sql = "SELECT pag." . $type . " FROM `" . DB_PREFIX . "googleshopping_product` pag WHERE product_id=" . (int)$product_id . " AND store_id=" . (int)$store_id;
132
133        $result = $this->db->query($sql);
134
135        if ($result->num_rows > 0) {
136            return (int)$result->row[$type];
137        }
138
139        return 0;
140    }
141}