d8c1a2e0
Yarik
Big commit artbox
|
1
2
3
4
|
<?php
namespace common\modules\product\helpers;
|
d8c1a2e0
Yarik
Big commit artbox
|
5
6
|
use common\modules\product\models\Category;
use common\modules\product\models\Product;
|
d8c1a2e0
Yarik
Big commit artbox
|
7
8
9
|
use yii\base\Object;
use Yii;
use yii\db\ActiveQuery;
|
c70f24ea
Yarik
For Leha commit.
|
10
|
use yii\helpers\ArrayHelper;
|
d8c1a2e0
Yarik
Big commit artbox
|
11
12
13
14
|
class ProductHelper extends Object
{
|
c70f24ea
Yarik
For Leha commit.
|
15
16
17
18
|
/**
* @todo ArtboxTree
* @return array
*/
|
d8c1a2e0
Yarik
Big commit artbox
|
19
20
21
|
public static function getCategories()
{
return Category::find()
|
4428da8c
Yarik
Almost all databa...
|
22
|
->getTree(null, 'lang');
|
d8c1a2e0
Yarik
Big commit artbox
|
23
24
|
}
|
c70f24ea
Yarik
For Leha commit.
|
25
26
27
28
|
/**
* Add $product_id to last products in session. Limit 16 products.
*
* @param int $product_id
|
d8c1a2e0
Yarik
Big commit artbox
|
29
|
*/
|
c70f24ea
Yarik
For Leha commit.
|
30
|
public static function addLastProducts(int $product_id)
|
d8c1a2e0
Yarik
Big commit artbox
|
31
32
|
{
$last_products = self::getLastProducts();
|
4428da8c
Yarik
Almost all databa...
|
33
|
if (!in_array($product_id, $last_products)) {
|
d8c1a2e0
Yarik
Big commit artbox
|
34
|
$last_products[] = intval($product_id);
|
4428da8c
Yarik
Almost all databa...
|
35
|
if (count($last_products) > 16) {
|
d8c1a2e0
Yarik
Big commit artbox
|
36
37
38
39
40
41
|
array_shift($last_products);
}
Yii::$app->session->set('last_products', $last_products);
}
}
|
c70f24ea
Yarik
For Leha commit.
|
42
43
44
45
46
47
48
49
50
|
/**
* Get last products ids from session or last Product models with ProductVariant, which are in stock if
* $as_object is true
*
* @param bool $as_object
*
* @return array
*/
public static function getLastProducts(bool $as_object = false)
|
d8c1a2e0
Yarik
Big commit artbox
|
51
|
{
|
5c2eb7c8
Yarik
Big commit almost...
|
52
|
$last_products = Yii::$app->session->get('last_products', []);
|
4428da8c
Yarik
Almost all databa...
|
53
|
if ($as_object) {
|
d8c1a2e0
Yarik
Big commit artbox
|
54
55
|
$last_products = Product::find()
->joinWith([ 'variant' ])
|
c70f24ea
Yarik
For Leha commit.
|
56
|
->where([ 'product.id' => $last_products ])
|
4428da8c
Yarik
Almost all databa...
|
57
58
59
|
->andWhere(
[
'!=',
|
c70f24ea
Yarik
For Leha commit.
|
60
|
'product_variant.stock',
|
4428da8c
Yarik
Almost all databa...
|
61
62
63
|
0,
]
)
|
c70f24ea
Yarik
For Leha commit.
|
64
|
->indexBy('id')
|
d8c1a2e0
Yarik
Big commit artbox
|
65
66
67
68
69
|
->all();
}
return array_reverse($last_products);
}
|
c70f24ea
Yarik
For Leha commit.
|
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/**
* Get special Products array with ProductVariants, which are in stock
* Available types:
* * top
* * new
* * promo
*
* @param string $type
* @param int $count
*
* @return Product[]
*/
public static function getSpecialProducts(string $type, int $count)
|
d8c1a2e0
Yarik
Big commit artbox
|
83
|
{
|
4428da8c
Yarik
Almost all databa...
|
84
|
switch ($type) {
|
d8c1a2e0
Yarik
Big commit artbox
|
85
86
87
88
89
90
91
|
case 'top':
$data = [ 'is_top' => true ];
break;
case 'new':
$data = [ 'is_new' => true ];
break;
case 'promo':
|
4428da8c
Yarik
Almost all databa...
|
92
|
$data = [ 'is_discount' => true ];
|
d8c1a2e0
Yarik
Big commit artbox
|
93
|
break;
|
c70f24ea
Yarik
For Leha commit.
|
94
95
96
|
default:
return [];
break;
|
d8c1a2e0
Yarik
Big commit artbox
|
97
98
|
}
return Product::find()
|
5c2eb7c8
Yarik
Big commit almost...
|
99
100
|
->with('lang')
->joinWith('variants.lang')
|
d8c1a2e0
Yarik
Big commit artbox
|
101
|
->where($data)
|
4428da8c
Yarik
Almost all databa...
|
102
103
104
|
->andWhere(
[
'!=',
|
c70f24ea
Yarik
For Leha commit.
|
105
|
'productVariant.stock',
|
4428da8c
Yarik
Almost all databa...
|
106
107
108
|
0,
]
)
|
36d1807a
Yarik
Big commit.
|
109
|
->limit($count)
|
d8c1a2e0
Yarik
Big commit artbox
|
110
111
112
|
->all();
}
|
8af13427
Yarik
For leha commit.
|
113
|
/**
|
c70f24ea
Yarik
For Leha commit.
|
114
115
|
* Get ActiveQuery to get similar products to $product
*
|
8af13427
Yarik
For leha commit.
|
116
117
118
|
* @param Product $product
* @param int $count
*
|
c70f24ea
Yarik
For Leha commit.
|
119
|
* @return ActiveQuery
|
8af13427
Yarik
For leha commit.
|
120
|
*/
|
c70f24ea
Yarik
For Leha commit.
|
121
|
public static function getSimilarProducts(Product $product, $count = 10): ActiveQuery
|
d8c1a2e0
Yarik
Big commit artbox
|
122
|
{
|
c70f24ea
Yarik
For Leha commit.
|
123
124
125
126
|
$query = Product::find();
if (empty( $product->properties )) {
$query->where('0 = 1');
return $query;
|
d8c1a2e0
Yarik
Big commit artbox
|
127
|
}
|
c70f24ea
Yarik
For Leha commit.
|
128
129
130
131
132
133
134
135
136
137
138
139
140
|
$query->innerJoinWith('variants')
->joinWith('categories')
->where(
[
'!=',
'product_variant.stock',
0,
]
)
->andWhere(
[ 'product_category.category_id' => ArrayHelper::getColumn($product->categories, 'id') ]
);
$options = [];
|
4428da8c
Yarik
Almost all databa...
|
141
|
foreach ($product->properties as $group) {
|
4428da8c
Yarik
Almost all databa...
|
142
|
foreach ($group->options as $option) {
|
c70f24ea
Yarik
For Leha commit.
|
143
|
$options[] = $option->id;
|
d8c1a2e0
Yarik
Big commit artbox
|
144
|
}
|
c70f24ea
Yarik
For Leha commit.
|
145
146
147
148
149
150
151
|
}
if (!empty( $options )) {
$query->innerJoinWith('options')
->andWhere([ 'product_option.option_id' => $options ]);
} else {
$query->where('0 = 1');
return $query;
|
d8c1a2e0
Yarik
Big commit artbox
|
152
|
}
|
4428da8c
Yarik
Almost all databa...
|
153
154
155
156
157
158
159
|
$query->andWhere(
[
'!=',
'product.id',
$product->id,
]
);
|
8af13427
Yarik
For leha commit.
|
160
|
$query->groupBy('product.id');
|
d8c1a2e0
Yarik
Big commit artbox
|
161
|
$query->limit($count);
|
c70f24ea
Yarik
For Leha commit.
|
162
|
return $query;
|
d8c1a2e0
Yarik
Big commit artbox
|
163
164
165
|
}
/**
|
c70f24ea
Yarik
For Leha commit.
|
166
|
* Add last category id to session
|
8af13427
Yarik
For leha commit.
|
167
|
*
|
c70f24ea
Yarik
For Leha commit.
|
168
|
* @param int $category_id
|
d8c1a2e0
Yarik
Big commit artbox
|
169
|
*/
|
c70f24ea
Yarik
For Leha commit.
|
170
|
public static function addLastCategory(int $category_id)
|
d8c1a2e0
Yarik
Big commit artbox
|
171
|
{
|
c70f24ea
Yarik
For Leha commit.
|
172
|
\Yii::$app->session->set('last_category_id', $category_id);
|
d8c1a2e0
Yarik
Big commit artbox
|
173
|
}
|
5c2eb7c8
Yarik
Big commit almost...
|
174
|
|
36d1807a
Yarik
Big commit.
|
175
|
/**
|
c70f24ea
Yarik
For Leha commit.
|
176
|
* Get last category id from session
|
36d1807a
Yarik
Big commit.
|
177
|
*
|
c70f24ea
Yarik
For Leha commit.
|
178
|
* @return int
|
36d1807a
Yarik
Big commit.
|
179
|
*/
|
c70f24ea
Yarik
For Leha commit.
|
180
|
public static function getLastCategory(): int
|
5c2eb7c8
Yarik
Big commit almost...
|
181
|
{
|
d8c1a2e0
Yarik
Big commit artbox
|
182
183
184
|
return \Yii::$app->session->get('last_category_id');
}
}
|