3f2bc3d0
Administrator
first commit
|
1
|
<?php
|
a22144fa
Administrator
20.07.16
|
2
|
|
aa5c63f4
Yarik
Filter fixing
|
3
|
namespace common\modules\product\helpers;
|
a22144fa
Administrator
20.07.16
|
4
|
|
aa5c63f4
Yarik
Filter fixing
|
5
6
7
8
9
10
11
12
13
|
use common\modules\product\models\Brand;
use common\modules\product\models\Category;
use common\modules\product\models\Product;
use common\modules\product\models\ProductVariant;
use common\modules\product\models\BrandName;
use common\modules\product\models\CategoryName;
use yii\base\Object;
use Yii;
use yii\db\ActiveQuery;
|
a22144fa
Administrator
20.07.16
|
14
|
|
aa5c63f4
Yarik
Filter fixing
|
15
16
|
class ProductHelper extends Object
{
|
a22144fa
Administrator
20.07.16
|
17
|
|
aa5c63f4
Yarik
Filter fixing
|
18
19
20
|
const PRODUCT_TAX_GROUP_ID_TARGET = 20;
const PRODUCT_TAX_GROUP_ID_YEAR = 21;
const PRODUCT_TAX_GROUP_ID_SEX = 22;
|
a22144fa
Administrator
20.07.16
|
21
|
|
aa5c63f4
Yarik
Filter fixing
|
22
23
|
const PRODUCT_VARIANT_TYPE_COLOR = 1;
const PRODUCT_VARIANT_TYPE_SIZE = 2;
|
a22144fa
Administrator
20.07.16
|
24
|
|
aa5c63f4
Yarik
Filter fixing
|
25
26
27
28
29
|
public static function getCategories()
{
return Category::find()
->getTree(); // with('categoryName')->
}
|
a22144fa
Administrator
20.07.16
|
30
|
|
aa5c63f4
Yarik
Filter fixing
|
31
32
33
34
|
public static function getBrands()
{
return Brand::find(); // ->with('brandName')
}
|
a22144fa
Administrator
20.07.16
|
35
|
|
aa5c63f4
Yarik
Filter fixing
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/*
* Return custom filter-option link
* @var array $filter
* @var array $options
* @return array
*/
public static function getFilterForOption($filter, $key, $value, $remove = false)
{
$result = $filter;
if(is_array($value)) {
foreach($value as $value_key => $value_items) {
if(!is_array($value_items)) {
$value_items = [ $value_items ];
}
foreach($value_items as $value_item) {
if($remove && isset( $result[ $key ] ) && ( $i = array_search($value_item, $result[ $key ][ $value_key ]) ) !== false) {
unset( $result[ $key ][ $value_key ][ $i ] );
if(empty( $result[ $key ][ $value_key ] )) {
unset( $result[ $key ][ $value_key ] );
}
} else {
if(!isset( $result[ $key ][ $value_key ] ) || array_search($value_item, $result[ $key ][ $value_key ]) === false) {
$result[ $key ][ $value_key ][] = $value_item;
}
|
3f2bc3d0
Administrator
first commit
|
60
61
62
|
}
}
}
|
3f2bc3d0
Administrator
first commit
|
63
|
} else {
|
aa5c63f4
Yarik
Filter fixing
|
64
65
66
67
68
69
70
71
72
|
if($remove && isset( $result[ $key ] ) && ( $i = array_search($value, $result[ $key ]) ) !== false) {
unset( $result[ $key ][ $i ] );
if(empty( $result[ $key ] )) {
unset( $result[ $key ] );
}
} else {
if(!isset( $result[ $key ] ) || array_search($value, $result[ $key ]) === false) {
$result[ $key ][] = $value;
}
|
3f2bc3d0
Administrator
first commit
|
73
74
|
}
}
|
aa5c63f4
Yarik
Filter fixing
|
75
|
return $result;
|
3f2bc3d0
Administrator
first commit
|
76
|
}
|
a22144fa
Administrator
20.07.16
|
77
|
|
aa5c63f4
Yarik
Filter fixing
|
78
79
80
81
82
83
84
85
86
|
public static function addLastProsucts($product_id)
{
$last_products = self::getLastProducts();
if(!in_array($product_id, $last_products)) {
$last_products[] = intval($product_id);
if(count($last_products) > 16) {
array_shift($last_products);
}
Yii::$app->session->set('last_products', $last_products);
|
2848e106
Karnovsky A
Fix last-products...
|
87
|
}
|
3f2bc3d0
Administrator
first commit
|
88
|
}
|
a22144fa
Administrator
20.07.16
|
89
|
|
aa5c63f4
Yarik
Filter fixing
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
public static function getLastProducts($as_object = false)
{
$last_products = Yii::$app->session->get('last_products', [ ]);
if($as_object) {
$last_products = Product::find()
->joinWith([ 'variant' ])
->where([ Product::tableName() . '.product_id' => $last_products ])
->andWhere([
'!=',
ProductVariant::tableName() . '.stock',
0,
])
->all();
}
return array_reverse($last_products);
|
e9f291a5
Karnovsky A
Similar products ...
|
105
|
}
|
a22144fa
Administrator
20.07.16
|
106
|
|
aa5c63f4
Yarik
Filter fixing
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
public static function getSpecialProducts($type, $count, $sort = NULL)
{
switch($type) {
case 'top':
$data = [ 'is_top' => true ];
break;
case 'new':
$data = [ 'is_new' => true ];
break;
case 'promo':
$data = [ 'akciya' => true ];
break;
}
return Product::find()
->joinWith('variants')
->where($data)
->andWhere([
'!=',
ProductVariant::tableName() . '.stock',
0,
])
->limit($count)/*->orderBy($sort)*/
->all();
|
eaaf879f
Karnovsky A
Similar products ...
|
130
|
}
|
a22144fa
Administrator
20.07.16
|
131
|
|
aa5c63f4
Yarik
Filter fixing
|
132
133
134
135
136
137
138
|
public static function getSimilarProducts($product, $count = 10)
{
if(!is_object($product)) {
$product = Product::find()
->where([ 'product_id' => $product ])
->with('enabledVariants')
->one();
|
e9f291a5
Karnovsky A
Similar products ...
|
139
|
}
|
a22144fa
Administrator
20.07.16
|
140
|
|
aa5c63f4
Yarik
Filter fixing
|
141
142
|
if(!$product->properties) {
return [ ];
|
e9f291a5
Karnovsky A
Similar products ...
|
143
|
}
|
aa5c63f4
Yarik
Filter fixing
|
144
145
146
|
$product_categories = [ ];
foreach($product->categories as $category) {
$product_categories[] = $category->category_id;
|
a0be9a4d
Karnovsky A
30062016
|
147
|
}
|
aa5c63f4
Yarik
Filter fixing
|
148
|
$query = Product::find()
|
b929fe29
Administrator
14.09.16
|
149
|
|
aa5c63f4
Yarik
Filter fixing
|
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
->innerJoinWith('variant')
->joinWith('category')
->where([
'!=',
'product_variant.stock',
0,
])
->andWhere([ 'product_category.category_id' => $product_categories ]);
// $query->andWhere(['>=', 'product_variant.price', $product->enabledVariant->price * 0.7]);
// $query->andWhere(['<=', 'product_variant.price', $product->enabledVariant->price * 1.3]);
foreach($product->properties as $group) {
$where = [ ];
foreach($group->_options as $option) {
$where[] = $option->tax_option_id;
}
if(!$where) {
continue;
}
$query->innerJoin('product_option to' . $group->tax_group_id, 'to' . $group->tax_group_id . '.product_id = product.product_id');
$query->andWhere([ 'to' . $group->tax_group_id . '.option_id' => $where ]);
|
a0be9a4d
Karnovsky A
30062016
|
170
|
}
|
aa5c63f4
Yarik
Filter fixing
|
171
172
173
174
175
176
177
|
$query->andWhere([
'!=',
'product.product_id',
$product->product_id,
]);
$query->groupBy('product.product_id');
$query->limit($count);
|
b929fe29
Administrator
14.09.16
|
178
|
$products = $query
|
aa5c63f4
Yarik
Filter fixing
|
179
|
->all();
|
b929fe29
Administrator
14.09.16
|
180
|
|
aa5c63f4
Yarik
Filter fixing
|
181
|
return $products;
|
a0be9a4d
Karnovsky A
30062016
|
182
|
}
|
a22144fa
Administrator
20.07.16
|
183
|
|
aa5c63f4
Yarik
Filter fixing
|
184
185
186
187
188
189
190
|
/**
* @param ActiveQuery $query
* @param $params
* @param bool $setPriceLimits
*/
public static function _setQueryParams(&$query, $params, $setPriceLimits = true)
{
|
38ed47e1
Administrator
20.07.16
|
191
192
|
// print_r($params);
// die();
|
aa5c63f4
Yarik
Filter fixing
|
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
|
if(!empty( $params[ 'keywords' ] )) {
if(!is_array($params[ 'keywords' ])) {
$params[ 'keywords' ] = [ $params[ 'keywords' ] ];
}
foreach($params[ 'keywords' ] as $keyword) {
$query->orFilterWhere([
'ilike',
Product::tableName() . '.name',
$keyword,
]);
$query->orFilterWhere([
'ilike',
BrandName::tableName() . '.value',
$keyword,
]);
$query->orFilterWhere([
'ilike',
CategoryName::tableName() . '.value',
$keyword,
]);
$query->orFilterWhere([
'ilike',
ProductVariant::tableName() . '.sku',
$keyword,
]);
}
}
|
a22144fa
Administrator
20.07.16
|
220
|
|
aa5c63f4
Yarik
Filter fixing
|
221
|
foreach($params as $key => $param) {
|
a22144fa
Administrator
20.07.16
|
222
|
|
aa5c63f4
Yarik
Filter fixing
|
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
|
switch($key) {
case 'special':
foreach($param as $key => $value) {
$query->orFilterWhere([ Product::tableName() . '.' . $key => $value ]);
}
break;
case 'brands':
$query->andFilterWhere([ Product::tableName() . '.brand_id' => $param ]);
break;
case 'keywords':
break;
case 'prices':
if($param[ 'min' ] > 0) {
$query->andWhere([
'>=',
ProductVariant::tableName() . '.price',
$param[ 'min' ],
]);
}
if($param[ 'max' ] > 0) {
$query->andWhere([
'<=',
ProductVariant::tableName() . '.price',
$param[ 'max' ],
]);
}
break;
default:
$query->andWhere(
|
a22144fa
Administrator
20.07.16
|
252
|
Product::tableName() . '.product_id IN (
|
aa5c63f4
Yarik
Filter fixing
|
253
254
255
256
257
258
259
260
261
262
263
264
265
|
SELECT DISTINCT products
FROM (
SELECT product_id AS products
FROM product_option
INNER JOIN tax_option ON tax_option.tax_option_id = product_option.option_id
INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id
WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\') OR product_id IN (
(SELECT product_id AS products
FROM product_variant_option
INNER JOIN product_variant ON product_variant_option.product_variant_id = product_variant.product_variant_id
INNER JOIN tax_option ON tax_option.tax_option_id = product_variant_option.option_id
INNER JOIN tax_group ON tax_group.tax_group_id = tax_option.tax_group_id
WHERE tax_group.alias LIKE \''. $key .'\' AND tax_option.alias IN (\'' . implode('\',\'', $param) . '\'))
|
a22144fa
Administrator
20.07.16
|
266
267
|
)
) AS table_name
|
aa5c63f4
Yarik
Filter fixing
|
268
269
270
|
)'
);
}
|
a22144fa
Administrator
20.07.16
|
271
|
|
a0be9a4d
Karnovsky A
30062016
|
272
|
}
|
a22144fa
Administrator
20.07.16
|
273
|
|
a0be9a4d
Karnovsky A
30062016
|
274
|
}
|
a22144fa
Administrator
20.07.16
|
275
|
|
aa5c63f4
Yarik
Filter fixing
|
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
public static function productCountQuery($category = NULL, $params, $excludeKeys = [ ])
{
$p = [ ];
foreach($params as $key => $param) {
if(in_array($key, $excludeKeys)) {
$p[ $key ] = $param;
}
}
/** @var ActiveQuery $query */
if(!empty( $category )) {
$query = $category->getProducts();
} else {
$query = Product::find();
}
ProductHelper::_setQueryParams($query, $params);
$query->select([ 'COUNT(product.product_id)' ]);
|
a22144fa
Administrator
20.07.16
|
292
|
|
aa5c63f4
Yarik
Filter fixing
|
293
|
return $query;
|
a0be9a4d
Karnovsky A
30062016
|
294
|
}
|
a22144fa
Administrator
20.07.16
|
295
|
|
01905ec6
Yarik
Filter fixing
|
296
297
298
|
public static function addLastCategory($category_id) {
\Yii::$app->session->set('last_category_id', $category_id);
}
|
a22144fa
Administrator
20.07.16
|
299
|
|
01905ec6
Yarik
Filter fixing
|
300
301
302
|
public static function getLastCategory() {
return \Yii::$app->session->get('last_category_id');
}
|
aa5c63f4
Yarik
Filter fixing
|
303
|
}
|