Commit 439a548391f274291578ab2485f7dbe6518442b2
Merge remote-tracking branch 'origin/master'
Showing
3 changed files
with
130 additions
and
6 deletions
Show diff stats
helpers/ProductHelper.php
@@ -4,6 +4,8 @@ | @@ -4,6 +4,8 @@ | ||
4 | 4 | ||
5 | use artweb\artbox\ecommerce\models\Category; | 5 | use artweb\artbox\ecommerce\models\Category; |
6 | use artweb\artbox\ecommerce\models\Product; | 6 | use artweb\artbox\ecommerce\models\Product; |
7 | + use yii\base\InvalidConfigException; | ||
8 | + use yii\base\InvalidParamException; | ||
7 | use yii\base\Object; | 9 | use yii\base\Object; |
8 | use Yii; | 10 | use Yii; |
9 | use yii\db\ActiveQuery; | 11 | use yii\db\ActiveQuery; |
@@ -23,7 +25,7 @@ | @@ -23,7 +25,7 @@ | ||
23 | } | 25 | } |
24 | 26 | ||
25 | /** | 27 | /** |
26 | - * Add $product_id to last products in session. Limit 16 products. | 28 | + * Add $product_id to last products in session. |
27 | * | 29 | * |
28 | * @param int $product_id | 30 | * @param int $product_id |
29 | */ | 31 | */ |
@@ -32,9 +34,6 @@ | @@ -32,9 +34,6 @@ | ||
32 | $last_products = self::getLastProducts(); | 34 | $last_products = self::getLastProducts(); |
33 | if (!in_array($product_id, $last_products)) { | 35 | if (!in_array($product_id, $last_products)) { |
34 | $last_products[] = intval($product_id); | 36 | $last_products[] = intval($product_id); |
35 | - if (count($last_products) > 16) { | ||
36 | - array_shift($last_products); | ||
37 | - } | ||
38 | Yii::$app->session->set('last_products', $last_products); | 37 | Yii::$app->session->set('last_products', $last_products); |
39 | } | 38 | } |
40 | } | 39 | } |
@@ -52,7 +51,17 @@ | @@ -52,7 +51,17 @@ | ||
52 | $last_products = Yii::$app->session->get('last_products', []); | 51 | $last_products = Yii::$app->session->get('last_products', []); |
53 | if ($as_object) { | 52 | if ($as_object) { |
54 | $last_products = Product::find() | 53 | $last_products = Product::find() |
55 | - ->joinWith([ 'variant' ]) | 54 | + ->innerJoinWith( |
55 | + [ | ||
56 | + 'enabledVariants' => function ($query) { | ||
57 | + /** | ||
58 | + * @var ActiveQuery $query | ||
59 | + */ | ||
60 | + $query->joinWith('lang') | ||
61 | + ->with('images'); | ||
62 | + }, | ||
63 | + ] | ||
64 | + ) | ||
56 | ->where([ 'product.id' => $last_products ]) | 65 | ->where([ 'product.id' => $last_products ]) |
57 | ->andWhere( | 66 | ->andWhere( |
58 | [ | 67 | [ |
@@ -66,6 +75,19 @@ | @@ -66,6 +75,19 @@ | ||
66 | } | 75 | } |
67 | return array_reverse($last_products); | 76 | return array_reverse($last_products); |
68 | } | 77 | } |
78 | + | ||
79 | + | ||
80 | + public static function trueWordForm($num, $form_for_1, $form_for_2, $form_for_5){ | ||
81 | + $num = abs($num) % 100; | ||
82 | + $num_x = $num % 10; | ||
83 | + if ($num > 10 && $num < 20) | ||
84 | + return $form_for_5; | ||
85 | + if ($num_x > 1 && $num_x < 5) | ||
86 | + return $form_for_2; | ||
87 | + if ($num_x == 1) | ||
88 | + return $form_for_1; | ||
89 | + return $form_for_5; | ||
90 | + } | ||
69 | 91 | ||
70 | /** | 92 | /** |
71 | * Get special Products array with ProductVariants, which are in stock | 93 | * Get special Products array with ProductVariants, which are in stock |
@@ -181,4 +203,95 @@ | @@ -181,4 +203,95 @@ | ||
181 | { | 203 | { |
182 | return \Yii::$app->session->get('last_category_id'); | 204 | return \Yii::$app->session->get('last_category_id'); |
183 | } | 205 | } |
206 | + | ||
207 | + /** | ||
208 | + * Group Product[] by Categories | ||
209 | + * Array of categories with 2 levels: | ||
210 | + * <code> | ||
211 | + * [ | ||
212 | + * 'parent_id' => [ | ||
213 | + * 'id', | ||
214 | + * 'name', | ||
215 | + * 'count', | ||
216 | + * 'children' => [ | ||
217 | + * 'id' => [ | ||
218 | + * 'id', | ||
219 | + * 'name', | ||
220 | + * 'count', | ||
221 | + * ] | ||
222 | + * ] | ||
223 | + * ] | ||
224 | + * ] | ||
225 | + * </code> | ||
226 | + * | ||
227 | + * @param Product[] $products | ||
228 | + * | ||
229 | + * @return array | ||
230 | + * @throws InvalidConfigException | ||
231 | + */ | ||
232 | + public static function groupByCategories(array $products): array | ||
233 | + { | ||
234 | + $categoryList = []; | ||
235 | + foreach ($products as $product) { | ||
236 | + if (!( $product instanceof Product )) { | ||
237 | + throw new InvalidParamException('$products must be array of ' . Product::className()); | ||
238 | + } | ||
239 | + foreach ($product->categories as $category) { | ||
240 | + /** | ||
241 | + * @var Category|null $parentCategory | ||
242 | + */ | ||
243 | + $parentCategory = $category->parentAR; | ||
244 | + /* If category has parent add current category to parents children array, | ||
245 | + else create current category as root category */ | ||
246 | + if ($parentCategory) { | ||
247 | + /* If parent category already in $categoryList search current category in its array, | ||
248 | + else create it in $categoryList and add current category to it */ | ||
249 | + if (array_key_exists($parentCategory->id, $categoryList)) { | ||
250 | + /* If current category already in parent category array increament count by 1, | ||
251 | + else add current category to parent category children array */ | ||
252 | + if (array_key_exists($category->id, $categoryList[ $parentCategory->id ][ 'children' ])) { | ||
253 | + $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ][ 'count' ] += 1; | ||
254 | + } else { | ||
255 | + $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ] = [ | ||
256 | + 'id' => $category->id, | ||
257 | + 'name' => $category->lang->title, | ||
258 | + 'alias' => $category->lang->alias, | ||
259 | + 'count' => 1, | ||
260 | + ]; | ||
261 | + } | ||
262 | + } else { | ||
263 | + $categoryList[ $parentCategory->id ] = [ | ||
264 | + 'id' => $parentCategory->id, | ||
265 | + 'name' => $parentCategory->lang->title, | ||
266 | + 'alias' => $parentCategory->lang->alias, | ||
267 | + 'children' => [ | ||
268 | + $category->id => [ | ||
269 | + 'id' => $category->id, | ||
270 | + 'name' => $category->lang->title, | ||
271 | + 'alias' => $category->lang->alias, | ||
272 | + 'count' => 1, | ||
273 | + ], | ||
274 | + ], | ||
275 | + 'count' => 0, | ||
276 | + ]; | ||
277 | + } | ||
278 | + } else { | ||
279 | + /* If current category already in $categoryList increment its count by 1, | ||
280 | + else add it to $categoryList */ | ||
281 | + if (array_key_exists($category->id, $categoryList)) { | ||
282 | + $categoryList[ $category->id ][ 'count' ] += 1; | ||
283 | + } else { | ||
284 | + $categoryList[ $category->id ] = [ | ||
285 | + 'id' => $category->id, | ||
286 | + 'name' => $category->lang->title, | ||
287 | + 'alias' => $category->lang->alias, | ||
288 | + 'count' => 1, | ||
289 | + 'children' => [], | ||
290 | + ]; | ||
291 | + } | ||
292 | + } | ||
293 | + } | ||
294 | + } | ||
295 | + return $categoryList; | ||
296 | + } | ||
184 | } | 297 | } |
185 | \ No newline at end of file | 298 | \ No newline at end of file |
models/Category.php
@@ -31,6 +31,7 @@ | @@ -31,6 +31,7 @@ | ||
31 | * @property Brand[] $brands | 31 | * @property Brand[] $brands |
32 | * @property TaxGroup[] $taxGroups | 32 | * @property TaxGroup[] $taxGroups |
33 | * @property Category[] $siblings | 33 | * @property Category[] $siblings |
34 | + * @property Category $parentAR | ||
34 | * * From language behavior * | 35 | * * From language behavior * |
35 | * @property CategoryLang $lang | 36 | * @property CategoryLang $lang |
36 | * @property CategoryLang[] $langs | 37 | * @property CategoryLang[] $langs |
@@ -334,4 +335,14 @@ | @@ -334,4 +335,14 @@ | ||
334 | ] | 335 | ] |
335 | ); | 336 | ); |
336 | } | 337 | } |
338 | + | ||
339 | + /** | ||
340 | + * Return Active query to obtain parent category | ||
341 | + * | ||
342 | + * @return ActiveQuery | ||
343 | + */ | ||
344 | + public function getParentAR() | ||
345 | + { | ||
346 | + return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]); | ||
347 | + } | ||
337 | } | 348 | } |
models/ProductFrontendSearch.php
@@ -98,7 +98,7 @@ | @@ -98,7 +98,7 @@ | ||
98 | $dataProvider = new ArrayDataProvider( | 98 | $dataProvider = new ArrayDataProvider( |
99 | [ | 99 | [ |
100 | 'allModels' => $this->getSearchQuery($category, $params, $in_stock) | 100 | 'allModels' => $this->getSearchQuery($category, $params, $in_stock) |
101 | - ->with('variant')->all(), | 101 | + ->with('variant', 'videos')->all(), |
102 | 'pagination' => [ | 102 | 'pagination' => [ |
103 | 'pageSize' => 10, | 103 | 'pageSize' => 10, |
104 | ], | 104 | ], |