Commit 1f5140e3fc335243be23c29e329fb27c01c3d476
1 parent
c240a19c
Brand
Showing
3 changed files
with
105 additions
and
1 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; |
@@ -181,4 +183,95 @@ | @@ -181,4 +183,95 @@ | ||
181 | { | 183 | { |
182 | return \Yii::$app->session->get('last_category_id'); | 184 | return \Yii::$app->session->get('last_category_id'); |
183 | } | 185 | } |
186 | + | ||
187 | + /** | ||
188 | + * Group Product[] by Categories | ||
189 | + * Array of categories with 2 levels: | ||
190 | + * <code> | ||
191 | + * [ | ||
192 | + * 'parent_id' => [ | ||
193 | + * 'id', | ||
194 | + * 'name', | ||
195 | + * 'count', | ||
196 | + * 'children' => [ | ||
197 | + * 'id' => [ | ||
198 | + * 'id', | ||
199 | + * 'name', | ||
200 | + * 'count', | ||
201 | + * ] | ||
202 | + * ] | ||
203 | + * ] | ||
204 | + * ] | ||
205 | + * </code> | ||
206 | + * | ||
207 | + * @param Product[] $products | ||
208 | + * | ||
209 | + * @return array | ||
210 | + * @throws InvalidConfigException | ||
211 | + */ | ||
212 | + public static function groupByCategories(array $products): array | ||
213 | + { | ||
214 | + $categoryList = []; | ||
215 | + foreach ($products as $product) { | ||
216 | + if (!( $product instanceof Product )) { | ||
217 | + throw new InvalidParamException('$products must be array of ' . Product::className()); | ||
218 | + } | ||
219 | + foreach ($product->categories as $category) { | ||
220 | + /** | ||
221 | + * @var Category|null $parentCategory | ||
222 | + */ | ||
223 | + $parentCategory = $category->parentAR; | ||
224 | + /* If category has parent add current category to parents children array, | ||
225 | + else create current category as root category */ | ||
226 | + if ($parentCategory) { | ||
227 | + /* If parent category already in $categoryList search current category in its array, | ||
228 | + else create it in $categoryList and add current category to it */ | ||
229 | + if (array_key_exists($parentCategory->id, $categoryList)) { | ||
230 | + /* If current category already in parent category array increament count by 1, | ||
231 | + else add current category to parent category children array */ | ||
232 | + if (array_key_exists($category->id, $categoryList[ $parentCategory->id ][ 'children' ])) { | ||
233 | + $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ][ 'count' ] += 1; | ||
234 | + } else { | ||
235 | + $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ] = [ | ||
236 | + 'id' => $category->id, | ||
237 | + 'name' => $category->lang->title, | ||
238 | + 'alias' => $category->lang->alias, | ||
239 | + 'count' => 1, | ||
240 | + ]; | ||
241 | + } | ||
242 | + } else { | ||
243 | + $categoryList[ $parentCategory->id ] = [ | ||
244 | + 'id' => $parentCategory->id, | ||
245 | + 'name' => $parentCategory->lang->title, | ||
246 | + 'alias' => $parentCategory->lang->alias, | ||
247 | + 'children' => [ | ||
248 | + $category->id => [ | ||
249 | + 'id' => $category->id, | ||
250 | + 'name' => $category->lang->title, | ||
251 | + 'alias' => $category->lang->alias, | ||
252 | + 'count' => 1, | ||
253 | + ], | ||
254 | + ], | ||
255 | + 'count' => 0, | ||
256 | + ]; | ||
257 | + } | ||
258 | + } else { | ||
259 | + /* If current category already in $categoryList increment its count by 1, | ||
260 | + else add it to $categoryList */ | ||
261 | + if (array_key_exists($category->id, $categoryList)) { | ||
262 | + $categoryList[ $category->id ][ 'count' ] += 1; | ||
263 | + } else { | ||
264 | + $categoryList[ $category->id ] = [ | ||
265 | + 'id' => $category->id, | ||
266 | + 'name' => $category->lang->title, | ||
267 | + 'alias' => $category->lang->alias, | ||
268 | + 'count' => 1, | ||
269 | + 'children' => [], | ||
270 | + ]; | ||
271 | + } | ||
272 | + } | ||
273 | + } | ||
274 | + } | ||
275 | + return $categoryList; | ||
276 | + } | ||
184 | } | 277 | } |
185 | \ No newline at end of file | 278 | \ 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 | ], |