-
Notifications
You must be signed in to change notification settings - Fork 70
/
func_catalog.inc.php
292 lines (227 loc) · 15.7 KB
/
func_catalog.inc.php
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
function catalog_category_trail($category_id=0, $language_code='') {
trigger_error('catalog_category_trail() is deprecated. Use reference::category(id)->path', E_USER_DEPRECATED);
if (empty($language_code)) $language_code = language::$selected['code'];
$trail = [];
foreach (reference::category($category_id, $language_code)->path as $category) {
$trail[$category->id] = $category->name;
}
return $trail;
}
function catalog_category_descendants($category_id=0, $language_code='') {
trigger_error('catalog_category_descendants() is deprecated. Use reference::category(id)->descendants', E_USER_DEPRECATED);
$descendants = [];
foreach (reference::category($category_id, $language_code)->path as $category) {
$descendants[$category->id] = $category->name;
}
return $descendants;
}
function catalog_categories_query($parent_id=0) {
$categories_query = database::query(
"select c.id, c.parent_id, c.image, ci.name, ci.short_description, c.priority, c.date_updated from ". DB_TABLE_PREFIX ."categories c
left join ". DB_TABLE_PREFIX ."categories_info ci on (ci.category_id = c.id and ci.language_code = '". database::input(language::$selected['code']) ."')
where c.status
and c.parent_id = ". (int)$parent_id ."
order by c.priority asc, ci.name asc;"
);
return $categories_query;
}
// Filter function using AND syntax
function catalog_products_query($filter=[]) {
if (!is_array($filter)) trigger_error('Invalid array filter for products query', E_USER_ERROR);
if (!empty($filter['categories'])) $filter['categories'] = array_filter($filter['categories']);
if (!empty($filter['manufacturers'])) $filter['manufacturers'] = array_filter($filter['manufacturers']);
if (!empty($filter['attributes'])) $filter['attributes'] = array_filter($filter['attributes']);
if (!empty($filter['products'])) $filter['products'] = array_filter($filter['products']);
if (!empty($filter['exclude_products'])) $filter['exclude_products'] = array_filter($filter['exclude_products']);
if (empty($filter['sort'])) $filter['sort'] = 'popularity';
$sql_inner_sort = [];
$sql_outer_sort = [];
if (!empty($filter['campaigns_first'])) {
$sql_outer_sort[] = "if(pc.campaign_price, 0, 1)";
}
switch ($filter['sort']) {
case 'name':
$sql_outer_sort[] = "name asc";
break;
case 'price':
$sql_outer_sort[] = "final_price asc";
break;
case 'date':
$sql_inner_sort[] = "p.date_created desc";
$sql_outer_sort[] = "p.date_created desc";
break;
case 'popularity':
$sql_inner_sort[] = "(p.purchases / ceil(datediff(now(), p.date_created)/7)) desc, (p.views / ceil(datediff(now(), p.date_created)/7)) desc";
$sql_outer_sort[] = "(p.purchases / ceil(datediff(now(), p.date_created)/7)) desc, (p.views / ceil(datediff(now(), p.date_created)/7)) desc";
break;
case 'products':
if (empty($filter['products'])) break;
$sql_inner_sort[] = "Field(p.id, '". implode("', '", $filter['products']) ."')";
$sql_outer_sort[] = "Field(p.id, '". implode("', '", $filter['products']) ."')";
break;
case 'random':
$sql_outer_sort[] = "rand()";
break;
}
$sql_where_categories = '';
if (!empty($filter['categories'])) {
$sql_where_categories = (
"and p.id in (
select product_id from ". DB_TABLE_PREFIX ."products_to_categories
where category_id in ('". implode("', '", database::input($filter['categories'])) ."')
)"
);
}
$sql_where_attributes = [];
if (!empty($filter['attributes']) && is_array($filter['attributes'])) {
foreach ($filter['attributes'] as $group_id => $values) {
$sql_where_attributes[] =
"and p.id in (
select distinct product_id from ". DB_TABLE_PREFIX ."products_attributes
where (group_id = ". (int)$group_id ." and (value_id in ('". implode("', '", database::input($values)) ."') or custom_value in ('". implode("', '", database::input($values)) ."')))
)";
}
$sql_where_attributes = implode(PHP_EOL, $sql_where_attributes);
}
$sql_where_prices = [];
if (!empty($filter['price_ranges']) && is_array($filter['price_ranges'])) {
foreach ($filter['price_ranges'] as $price_range) {
list($min,$max) = explode('-', $price_range);
$sql_where_prices[] = "(if(pc.campaign_price, pc.campaign_price, pp.price) >= ". (float)$min ." and if(pc.campaign_price, pc.campaign_price, pp.price) <= ". (float)$max .")";
}
$sql_where_prices = "and (". implode(" or ", $sql_where_prices) .")";
}
$query = (
"select p.*, pi.name, pi.short_description, m.name as manufacturer_name, pp.price, pc.campaign_price, if(pc.campaign_price, pc.campaign_price, pp.price) as final_price, pa.attributes
from (
select p.id, p.sold_out_status_id, p.code, p.sku, p.mpn, p.gtin, p.manufacturer_id, p.keywords, p.image, p.recommended_price, p.tax_class_id, p.quantity, p.views, p.purchases, p.date_created
from ". DB_TABLE_PREFIX ."products p
left join ". DB_TABLE_PREFIX ."sold_out_statuses ss on (p.sold_out_status_id = ss.id)
where p.status
". (!empty($filter['products']) ? "and p.id in ('". implode("', '", database::input($filter['products'])) ."')" : null) ."
". (!empty($sql_where_categories) ? $sql_where_categories : null) ."
". (!empty($sql_where_attributes) ? $sql_where_attributes : null) ."
". (!empty($filter['manufacturers']) ? "and p.manufacturer_id in ('". implode("', '", database::input($filter['manufacturers'])) ."')" : null) ."
". (!empty($filter['keywords']) ? "and (find_in_set('". implode("', p.keywords) or find_in_set('", database::input($filter['keywords'])) ."', p.keywords))" : null) ."
and (p.quantity > 0 or ss.hidden != 1)
and (p.date_valid_from is null or p.date_valid_from <= '". date('Y-m-d H:i:s') ."')
and (p.date_valid_to is null or year(p.date_valid_to) < '1971' or p.date_valid_to >= '". date('Y-m-d H:i:s') ."')
". (!empty($filter['purchased']) ? "and p.purchases" : null) ."
". (!empty($filter['exclude_products']) ? "and p.id not in ('". implode("', '", $filter['exclude_products']) ."')" : null) ."
". ((!empty($sql_inner_sort) && !empty($filter['limit'])) ? "order by " . implode(",", $sql_inner_sort) : null) ."
". ((!empty($filter['limit']) && empty($filter['sql_where']) && empty($filter['product_name']) && empty($filter['product_name']) && empty($filter['campaign']) && empty($sql_where_prices)) ? "limit ". (!empty($filter['offset']) ? (int)$filter['offset'] . ", " : null) . (int)$filter['limit'] : "") ."
) p
left join ". DB_TABLE_PREFIX ."products_info pi on (pi.product_id = p.id and pi.language_code = '". database::input(language::$selected['code']) ."')
left join ". DB_TABLE_PREFIX ."manufacturers m on (m.id = p.manufacturer_id)
left join (
select product_id, group_concat(concat(group_id, '-', if(custom_value != '', custom_value, value_id)) separator ',') as attributes
from ". DB_TABLE_PREFIX ."products_attributes
group by product_id
order by id
) pa on (p.id = pa.product_id)
left join (
select product_id, if(`". database::input(currency::$selected['code']) ."`, `". database::input(currency::$selected['code']) ."` * ". (float)currency::$selected['value'] .", `". database::input(settings::get('store_currency_code')) ."`) as price
from ". DB_TABLE_PREFIX ."products_prices
) pp on (pp.product_id = p.id)
left join (
select product_id, min(if(`". database::input(currency::$selected['code']) ."`, `". database::input(currency::$selected['code']) ."` * ". (float)currency::$selected['value'] .", `". database::input(settings::get('store_currency_code')) ."`)) as campaign_price
from ". DB_TABLE_PREFIX ."products_campaigns
where (start_date is null or start_date <= '". date('Y-m-d H:i:s') ."')
and (end_date is null or year(end_date) < '1971' or end_date >= '". date('Y-m-d H:i:s') ."')
group by product_id
) pc on (pc.product_id = p.id)
where (p.id
". (!empty($filter['sql_where']) ? "and (". $filter['sql_where'] .")" : null) ."
". (!empty($filter['product_name']) ? "and pi.name like '%". database::input($filter['product_name']) ."%'" : null) ."
". (!empty($filter['campaign']) ? "and campaign_price > 0" : null) ."
". (!empty($sql_where_prices) ? $sql_where_prices : null) ."
)
group by p.id
". (!empty($sql_outer_sort) ? "order by ". implode(",", $sql_outer_sort) : "") ."
". (!empty($filter['limit']) && (!empty($filter['sql_where']) || !empty($filter['product_name']) || !empty($filter['campaign']) || !empty($sql_where_prices)) ? "limit ". (!empty($filter['offset']) ? (int)$filter['offset'] . ", " : null) . (int)$filter['limit'] : null) .";"
);
$products_query = database::query($query);
return $products_query;
}
// Search function using OR syntax
function catalog_products_search_query($filter=[]) {
if (!is_array($filter)) trigger_error('Invalid array filter for products query', E_USER_ERROR);
if (!empty($filter['categories'])) $filter['categories'] = array_filter($filter['categories']);
if (!empty($filter['manufacturers'])) $filter['manufacturers'] = array_filter($filter['manufacturers']);
if (!empty($filter['products'])) $filter['products'] = array_filter($filter['products']);
if (!empty($filter['exclude_products'])) $filter['exclude_products'] = array_filter($filter['exclude_products']);
$sql_where_prices = "";
if (!empty($filter['price_ranges'])) {
foreach ($filter['price_ranges'] as $price_range) {
list($min,$max) = explode('-', $price_range);
$sql_where_prices .= " or (if(pc.campaign_price, pc.campaign_price, pp.price) >= ". (float)$min ." and if(pc.campaign_price, pc.campaign_price, pp.price) <= ". (float)$max .")";
}
$sql_where_prices = "or (". ltrim($sql_where_prices, " or ") .")";
}
$query = (
"select p.*, pi.name, pi.short_description, m.name as manufacturer_name, pp.price, pc.campaign_price, if(pc.campaign_price, pc.campaign_price, pp.price) as final_price, pa.attributes, (0
". (!empty($filter['product_name']) ? "+ if(pi.name like '%". database::input($filter['product_name']) ."%', 1, 0)" : false) ."
". (!empty($filter['sql_where']) ? "+ if(". $filter['sql_where'] .", 1, 0)" : false) ."
". (!empty($filter['categories']) ? "+ if(find_in_set('". implode("', categories), 1, 0) + if(find_in_set('", database::input($filter['categories'])) ."', categories), 1, 0)" : false) ."
". (!empty($filter['manufacturers']) ? "+ if(p.manufacturer_id and p.manufacturer_id in ('". implode("', '", database::input($filter['manufacturers'])) ."'), 1, 0)" : false) ."
". (!empty($filter['keywords']) ? "+ if(find_in_set('". implode("', p.keywords), 1, 0) + if(find_in_set('", database::input($filter['keywords'])) ."', p.keywords), 1, 0)" : false) ."
". (!empty($filter['products']) ? "+ if(p.id in ('". implode("', '", database::input($filter['products'])) ."'), 1, 0)" : false) ."
) as occurrences
from (
select p.id, p.sold_out_status_id, p.code, p.sku, p.mpn, p.gtin, p.manufacturer_id, group_concat(ptc.category_id separator ',') as categories, p.keywords, p.image, p.recommended_price, p.tax_class_id, p.quantity, p.views, p.purchases, p.date_created
from ". DB_TABLE_PREFIX ."products p
left join ". DB_TABLE_PREFIX ."products_to_categories ptc on (p.id = ptc.product_id)
left join ". DB_TABLE_PREFIX ."sold_out_statuses ss on (p.sold_out_status_id = ss.id)
where p.status
and (p.id
". (!empty($filter['products']) ? "or p.id in ('". implode("', '", database::input($filter['products'])) ."')" : null) ."
". (!empty($filter['categories']) ? "or ptc.category_id in (". implode(",", database::input($filter['categories'])) .")" : null) ."
". (!empty($filter['manufacturers']) ? "or manufacturer_id in ('". implode("', '", database::input($filter['manufacturers'])) ."')" : null) ."
". (!empty($filter['keywords']) ? "or (find_in_set('". implode("', p.keywords) or find_in_set('", database::input($filter['keywords'])) ."', p.keywords))" : null) ."
)
and (p.quantity > 0 or ss.hidden != 1)
and (p.date_valid_from is null or p.date_valid_from <= '". date('Y-m-d H:i:s') ."')
and (p.date_valid_to is null or year(p.date_valid_to) < '1971' or p.date_valid_to >= '". date('Y-m-d H:i:s') ."')
". (!empty($filter['purchased']) ? "and p.purchases" : null) ."
". (!empty($filter['exclude_products']) ? "and p.id not in ('". implode("', '", $filter['exclude_products']) ."')" : null) ."
group by ptc.product_id
". ((!empty($filter['limit']) && empty($filter['sql_where']) && empty($filter['product_name']) && empty($filter['product_name']) && empty($filter['campaign']) && empty($sql_where_prices)) ? "limit ". (!empty($filter['offset']) ? (int)$filter['offset'] . ", " : null) . (int)$filter['limit'] : "") ."
) p
left join ". DB_TABLE_PREFIX ."products_info pi on (pi.product_id = p.id and pi.language_code = '". database::input(language::$selected['code']) ."')
left join ". DB_TABLE_PREFIX ."manufacturers m on (m.id = p.manufacturer_id)
left join (
select product_id, group_concat(concat(group_id, '-', if(custom_value != '', custom_value, value_id)) separator ',') as attributes
from ". DB_TABLE_PREFIX ."products_attributes
group by product_id
order by id
) pa on (p.id = pa.product_id)
left join (
select product_id, if(`". database::input(currency::$selected['code']) ."`, `". database::input(currency::$selected['code']) ."` * ". (float)currency::$selected['value'] .", `". database::input(settings::get('store_currency_code')) ."`) as price
from ". DB_TABLE_PREFIX ."products_prices
) pp on (pp.product_id = p.id)
left join (
select product_id, min(if(`". database::input(currency::$selected['code']) ."`, `". database::input(currency::$selected['code']) ."` * ". (float)currency::$selected['value'] .", `". database::input(settings::get('store_currency_code')) ."`)) as campaign_price
from ". DB_TABLE_PREFIX ."products_campaigns
where (start_date is null or start_date <= '". date('Y-m-d H:i:s') ."')
and (end_date is null or year(end_date) < '1971' or end_date >= '". date('Y-m-d H:i:s') ."')
group by product_id
) pc on (pc.product_id = p.id)
where (p.id
". (!empty($filter['sql_where']) ? "or (". $filter['sql_where'] .")" : null) ."
". (!empty($filter['product_name']) ? "or pi.name like '%". database::input($filter['product_name']) ."%'" : null) ."
". (!empty($filter['campaign']) ? "or campaign_price > 0" : null) ."
". (!empty($sql_where_prices) ? $sql_where_prices : null) ."
)
group by p.id
order by occurrences desc
". (!empty($filter['limit']) && (!empty($filter['sql_where']) || !empty($filter['product_name']) || !empty($filter['campaign']) || !empty($sql_where_prices)) ? "limit ". (!empty($filter['offset']) ? (int)$filter['offset'] . ", " : null) . (int)$filter['limit'] : null) .";"
);
$products_query = database::query($query);
return $products_query;
}
function catalog_stock_adjust($product_id, $combination, $quantity) {
trigger_error('catalog_stock_adjust() is deprecated. Use $ent_product->adjust_quantity()', E_USER_DEPRECATED);
$product = new ent_product($product_id);
return $product->adjust_quantity($quantity, $combination);
}