diff --git a/helpers/ProductHelper.php b/helpers/ProductHelper.php index 2962c6a..43c90bd 100755 --- a/helpers/ProductHelper.php +++ b/helpers/ProductHelper.php @@ -4,6 +4,8 @@ use artweb\artbox\ecommerce\models\Category; use artweb\artbox\ecommerce\models\Product; + use yii\base\InvalidConfigException; + use yii\base\InvalidParamException; use yii\base\Object; use Yii; use yii\db\ActiveQuery; @@ -181,4 +183,95 @@ { return \Yii::$app->session->get('last_category_id'); } + + /** + * Group Product[] by Categories + * Array of categories with 2 levels: + * + * [ + * 'parent_id' => [ + * 'id', + * 'name', + * 'count', + * 'children' => [ + * 'id' => [ + * 'id', + * 'name', + * 'count', + * ] + * ] + * ] + * ] + * + * + * @param Product[] $products + * + * @return array + * @throws InvalidConfigException + */ + public static function groupByCategories(array $products): array + { + $categoryList = []; + foreach ($products as $product) { + if (!( $product instanceof Product )) { + throw new InvalidParamException('$products must be array of ' . Product::className()); + } + foreach ($product->categories as $category) { + /** + * @var Category|null $parentCategory + */ + $parentCategory = $category->parentAR; + /* If category has parent add current category to parents children array, + else create current category as root category */ + if ($parentCategory) { + /* If parent category already in $categoryList search current category in its array, + else create it in $categoryList and add current category to it */ + if (array_key_exists($parentCategory->id, $categoryList)) { + /* If current category already in parent category array increament count by 1, + else add current category to parent category children array */ + if (array_key_exists($category->id, $categoryList[ $parentCategory->id ][ 'children' ])) { + $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ][ 'count' ] += 1; + } else { + $categoryList[ $parentCategory->id ][ 'children' ][ $category->id ] = [ + 'id' => $category->id, + 'name' => $category->lang->title, + 'alias' => $category->lang->alias, + 'count' => 1, + ]; + } + } else { + $categoryList[ $parentCategory->id ] = [ + 'id' => $parentCategory->id, + 'name' => $parentCategory->lang->title, + 'alias' => $parentCategory->lang->alias, + 'children' => [ + $category->id => [ + 'id' => $category->id, + 'name' => $category->lang->title, + 'alias' => $category->lang->alias, + 'count' => 1, + ], + ], + 'count' => 0, + ]; + } + } else { + /* If current category already in $categoryList increment its count by 1, + else add it to $categoryList */ + if (array_key_exists($category->id, $categoryList)) { + $categoryList[ $category->id ][ 'count' ] += 1; + } else { + $categoryList[ $category->id ] = [ + 'id' => $category->id, + 'name' => $category->lang->title, + 'alias' => $category->lang->alias, + 'count' => 1, + 'children' => [], + ]; + } + } + } + } + return $categoryList; + } } \ No newline at end of file diff --git a/models/Category.php b/models/Category.php index cafc49a..f073556 100755 --- a/models/Category.php +++ b/models/Category.php @@ -31,6 +31,7 @@ * @property Brand[] $brands * @property TaxGroup[] $taxGroups * @property Category[] $siblings + * @property Category $parentAR * * From language behavior * * @property CategoryLang $lang * @property CategoryLang[] $langs @@ -334,4 +335,14 @@ ] ); } + + /** + * Return Active query to obtain parent category + * + * @return ActiveQuery + */ + public function getParentAR() + { + return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]); + } } diff --git a/models/ProductFrontendSearch.php b/models/ProductFrontendSearch.php index dd8086c..efa6dae 100755 --- a/models/ProductFrontendSearch.php +++ b/models/ProductFrontendSearch.php @@ -98,7 +98,7 @@ $dataProvider = new ArrayDataProvider( [ 'allModels' => $this->getSearchQuery($category, $params, $in_stock) - ->with('variant')->all(), + ->with('variant', 'videos')->all(), 'pagination' => [ 'pageSize' => 10, ], -- libgit2 0.21.4