ProductHelper.php
6 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
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
<?php
namespace artweb\artbox\ecommerce\helpers;
use artweb\artbox\ecommerce\models\Category;
use artweb\artbox\ecommerce\models\Product;
use yii\base\Object;
use Yii;
use yii\db\ActiveQuery;
use yii\helpers\ArrayHelper;
class ProductHelper extends Object
{
/**
* @todo ArtboxTree
* @return array
*/
public static function getCategories()
{
return Category::find()
->getTree(null, 'lang');
}
/**
* Add $product_id to last products in session. Limit 16 products.
*
* @param int $product_id
*/
public static function addLastProducts(int $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);
}
}
/**
* 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)
{
$last_products = Yii::$app->session->get('last_products', []);
if ($as_object) {
$last_products = Product::find()
->joinWith([ 'variant' ])
->where([ 'product.id' => $last_products ])
->andWhere(
[
'!=',
'product_variant.stock',
0,
]
)
->indexBy('id')
->all();
}
return array_reverse($last_products);
}
/**
* 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)
{
switch ($type) {
case 'top':
$data = [ 'is_top' => true ];
break;
case 'new':
$data = [ 'is_new' => true ];
break;
case 'promo':
$data = [ 'is_discount' => true ];
break;
default:
return [];
break;
}
return Product::find()
->with('lang')
->joinWith('variants.lang')
->where($data)
->andWhere(
[
'!=',
'productVariant.stock',
0,
]
)
->limit($count)
->all();
}
/**
* Get ActiveQuery to get similar products to $product
*
* @param Product $product
* @param int $count
*
* @return ActiveQuery
*/
public static function getSimilarProducts(Product $product, $count = 10): ActiveQuery
{
$query = Product::find();
if (empty( $product->properties )) {
$query->where('0 = 1');
return $query;
}
$query->innerJoinWith('variants')
->joinWith('categories')
->where(
[
'!=',
'product_variant.stock',
0,
]
)
->andWhere(
[ 'product_category.category_id' => ArrayHelper::getColumn($product->categories, 'id') ]
);
$options = [];
foreach ($product->properties as $group) {
foreach ($group->options as $option) {
$options[] = $option->id;
}
}
if (!empty( $options )) {
$query->innerJoinWith('options')
->andWhere([ 'product_option.option_id' => $options ]);
} else {
$query->where('0 = 1');
return $query;
}
$query->andWhere(
[
'!=',
'product.id',
$product->id,
]
);
$query->groupBy('product.id');
$query->limit($count);
return $query;
}
/**
* Add last category id to session
*
* @param int $category_id
*/
public static function addLastCategory(int $category_id)
{
\Yii::$app->session->set('last_category_id', $category_id);
}
/**
* Get last category id from session
*
* @return int
*/
public static function getLastCategory()
{
return \Yii::$app->session->get('last_category_id');
}
}